user113156
user113156

Reputation: 7107

Obtaining different results when using `facet_wrap()` in `ggplot`

I am using this code to download data from yahoo finance and plot some stocks against the S&P500 after normalising the adjusted prices.

The following code returns;

Ra <- c("NFLX") %>%
  tq_get(get = "stock.prices",
         from = "2013-01-01",
         to = "2016-12-31")

Rb <- "SPY" %>%
  tq_get(get = "stock.prices",
         from = "2013-01-01",
         to = "2016-12-31")

stock_returns_daily <- Ra
benchmark_returns_daily <- Rb  

RaRb <- left_join(stock_returns_daily, benchmark_returns_daily, by = c("date" = "date"))
normalise_series <- function(xdat) xdat / coredata(xdat)[1]


RaRb %>%
  ggplot(aes(x = date)) +
  geom_line(aes(y = normalise_series(adjusted.x)-1), linetype = "dashed") +
  geom_line(aes(y = normalise_series(adjusted.y)-1), color = "red") +
  labs(title = "Daily Stock Prices",
       x = "", y = "Adjusted Prices", color = "") +
 #facet_wrap(~ symbol, ncol = 2, scales = "free_y") +
  scale_y_continuous(labels = scales::dollar) +
  theme_tq() +
  scale_color_tq()

This figure (Which is the normalised Netflix stock price against the S&P500) :

enter image description here

This looks intuitive and correct to me, both start at the origin 0, however when I try to add in other stocks AMZN, FB, GOOG and NFLX and also uncommenting the facet_wrap(~ symbol, ncol = 2, scales = "free_y") + I do not get the same plot any more. I use the same code and it gives me two different outputs.

Ra <- c("AMZN","FB","GOOG", "NFLX") %>%
  tq_get(get = "stock.prices",
         from = "2013-01-01",
         to = "2016-12-31")


Rb <- "SPY" %>%
  tq_get(get = "stock.prices",
         from = "2013-01-01",
         to = "2016-12-31")

stock_returns_daily <- Ra
benchmark_returns_daily <- Rb  

RaRb <- left_join(stock_returns_daily, benchmark_returns_daily, by = c("date" = "date"))
normalise_series <- function(xdat) xdat / coredata(xdat)[1]


RaRb %>%
  ggplot(aes(x = date)) +
  geom_line(aes(y = normalise_series(adjusted.x) -1), color = "red") +
  geom_line(aes(y = normalise_series(adjusted.y) -1), linetype = "dashed") +
  labs(title = "Daily Stock Prices",
       x = "", y = "Adjusted Prices", color = "") +
  facet_wrap(~ symbol, ncol = 2, scales = "free_y") +
  scale_y_continuous(labels = scales::dollar) +
  theme_tq() +
  scale_color_tq()

Giving me the following;

enter image description here

Now NFLX is negative and gives me a different plot.

Upvotes: 0

Views: 171

Answers (1)

user113156
user113156

Reputation: 7107

To answer my own question thanks to a comment from @Noah in this question and to some guidance from @MrFlick in a question I posted here.

The following code seems to get what I want.

Ra <- c("AMZN","FB","GOOG", "NFLX") %>%
  tq_get(get = "stock.prices",
         from = "2013-01-01",
         to = "2016-12-31")


Rb <- "SPY" %>%
  tq_get(get = "stock.prices",
         from = "2013-01-01",
         to = "2016-12-31")

stock_returns_daily <- Ra
benchmark_returns_daily <- Rb  

RaRb <- left_join(stock_returns_daily, benchmark_returns_daily, by = c("date" = "date"))
normalise_series <- function(xdat) xdat / coredata(xdat)[1]

RaRb <- RaRb %>% 
  group_by(symbol) %>%
  select(symbol, date, adjusted.x, adjusted.y) %>%
  mutate(adj.x = normalise_series(adjusted.x)) %>%
  mutate(adj.y = normalise_series(adjusted.y))


RaRb %>%
  ggplot(aes(x = date)) +
  geom_line(aes(y = adj.x -1), color = "red") +
  geom_line(aes(y = adj.y -1), linetype = "dashed") +
  labs(title = "Daily Stock Prices",
       x = "", y = "Adjusted Prices", color = "") +
  facet_wrap(~ symbol, ncol = 2, scales = "free_y") +
  scale_y_continuous(labels = scales::dollar) +
  theme_tq() +
  scale_color_tq()

Which is this output:

enter image description here

The NFLX is now identical to the first plot in the original message.

Upvotes: 1

Related Questions