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

时间:2018-07-13 18:17:27

标签: r ggplot2 facet-wrap

我正在使用此代码从yahoo finance下载数据,并在将adjusted价格标准化后,将一些股票与S&P500进行比较。

以下代码返回;

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

该数字(这是Netflix股票相对于S&P500的标准化价格):

enter image description here

对我来说,这看起来很直观,很正确,都从原点0开始,但是当我尝试添加其他股票AMZNFBGOOG和{ {1}},也取消了NFLX的注释,我再也没有得到相同的情节了。我使用相同的代码,它给了我两个不同的输出。

facet_wrap(~ symbol, ncol = 2, scales = "free_y") +

给我以下内容;

enter image description here

现在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() 为负,给了我不同的情节。

1 个答案:

答案 0 :(得分:1)

要回答我自己的问题,多亏@Noah对这个问题的评论,以及@MrFlick对我发布的here问题的指导。

以下代码似乎可以得到我想要的。

perl test.pl | tee mid.log | sed 's/\x1b\[[0-9;]*m//g' > test.log

这是哪个输出:

enter image description here

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() 现在与原始消息中的第一个情节相同。