在ggplot中使用facet_wrap()获得不同的结果 [英] Obtaining different results when using `facet_wrap()` in `ggplot`

查看:67
本文介绍了在ggplot中使用facet_wrap()获得不同的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码从 yahoo财务下载数据,并在对调整后的价格进行归一化之后,将一些股票与S& P500进行比较.

以下代码返回;

  Ra<-c("NFLX")%>%tq_get(get ="stock.prices",来自="2013-01-01",到="2016-12-31")Rb<-"SPY"%&%;%tq_get(get ="stock.prices",来自="2013-01-01",到="2016-12-31")stock_returns_daily<-RaBenchmark_returns_daily<-RbRaRb<-left_join(stock_returns_daily,benchmark_returns_daily,= c("date" ="date"))normalise_series<-函数(xdat)xdat/coredata(xdat)[1]RaRb%>%ggplot(aes(x =日期))+geom_line(aes(ys(y = normalise_series(adjusted.x)-1),线型=虚线")+geom_line(aes(y = normalise_series(adjusted.y)-1),颜色=红色")+实验室(标题=每日股票价格",x =",y =调整后的价格",颜色=")+#facet_wrap(〜symbol,ncol = 2,scales ="free_y")+scale_y_continuous(labels = scales :: dollar)+theme_tq()+scale_color_tq() 

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

这对我来说看起来很直观且正确,都从原点 0 开始,但是当我尝试添加其他股票时, AMZN FB GOOG NFLX ,并且也取消注释 facet_wrap(〜符号,ncol = 2,比例="free_y")+ 相同的情节了.我使用相同的代码,它给了我两个不同的输出.

  Ra<-c("AMZN","FB","GOOG","NFLX")%&%;%tq_get(get ="stock.prices",来自="2013-01-01",到="2016-12-31")Rb<-"SPY"%&%;%tq_get(get ="stock.prices",来自="2013-01-01",到="2016-12-31")stock_returns_daily<-RaBenchmark_returns_daily<-RbRaRb<-left_join(stock_returns_daily,benchmark_returns_daily,= c("date" ="date"))normalise_series<-函数(xdat)xdat/coredata(xdat)[1]RaRb%>%ggplot(aes(x =日期))+geom_line(aes(ys(y = normalise_series(adjusted.x)-1),color ="red")+geom_line(aes(y = normalise_series(adjusted.y)-1),线型=虚线")+实验室(标题=每日股票价格",x =",y =调整后的价格",颜色=")+facet_wrap(〜符号,ncol = 2,比例="free_y")+scale_y_continuous(labels = scales :: dollar)+theme_tq()+scale_color_tq() 

给我以下内容;

现在 NFLX 为负,给了我不同的情节.

解决方案

要回答我自己的问题,要感谢

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

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

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;

Now NFLX is negative and gives me a different plot.

解决方案

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:

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

这篇关于在ggplot中使用facet_wrap()获得不同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆