在 `ggplot` 中使用 `facet_wrap()` 时获得不同的结果

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

我正在使用此代码从 yahoo finance 下载数据,并在 adjusted 价格正常化后根据标准普尔 500 指数绘制一些股票。

下面的代码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()

这个数字(这是标准普尔 500 指数的标准化 Netflix 股价):

这对我来说看起来直观且正确,两者都从原点 0 开始,但是当我尝试添加其他股票时 AMZNFBGOOGNFLX 并取消注释 facet_wrap(~ symbol, ncol = 2, scales = "free_y") + 我不再得到相同的情节。我使用相同的代码,它给了我两个不同的输出。

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()

给我以下信息;

现在NFLX是负的,给了我一个不同的情节。

根据 @Noah in this question and to some guidance from @MrFlick in a question I posted 的评论回答我自己的问题。

下面的代码似乎得到了我想要的。

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()

这是什么输出:

NFLX 现在与原始消息中的第一个情节相同。