在ggplot中使用facet

编程入门 行业动态 更新时间:2024-10-09 16:31:33
本文介绍了在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

本文发布于:2023-10-31 06:48:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545335.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ggplot   facet

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!