使用ggplot2的时间序列图

时间:2018-06-20 07:59:29

标签: r ggplot2 time-series trendline ggfortify

如何更改此图中的以下内容?

  1. 更改趋势线的颜色(当前为蓝色)
  2. 更改峰点的颜色(当前为橙色)
  3. 更改时间序列图案线的颜色(当前为黑色)
  4. 添加图例以指示以上所有三个颜色

Here's the Plot I got from the code source

enter image description here

代码来源:

# Libraries used: 
library(ggplot2)
library(timeSeries)
library(ggfortify)
library(ggthemes)
library(dplyr)
library(strucchange)

strucchange::breakpoints(AirPassengers ~ 1) %>%
  autoplot(ts.linetype = 'solid', ts.size = 1.1, ts.geom = 'line')+
  geom_smooth(aes(y=AirPassengers), 
              method = "loess", se= F,
              lwd = 1.2) +
  geom_point(aes(y = AirPassengers), size = 1.5, shape = 16, color = "orange3")+
  scale_x_date(date_breaks = "1 years", date_labels = "%Y")+
  labs(title = "Yearly Time Series", 
       subtitle="Sales trend from Year 2001 to 2017", 
       caption="Source: Airpassengers Time Series Analysis", 
       y="Sales", x = "Years")+
  theme_economist_white()+
  theme(axis.text.x = element_text(angle = 90, hjust = -.1))

1 个答案:

答案 0 :(得分:0)

可以使用以下语法进行前三个更改:

rewrite ^ $scheme://example.com$uri?from-old=true permanent;

enter image description here

通过在对strucchange::breakpoints(AirPassengers ~ 1) %>% autoplot(ts.linetype = 'solid', ts.size = 1.1, ts.geom = 'line', fill = "red")+ geom_smooth(aes(y=AirPassengers), method = "loess", se= F, lwd = 1.2, color = "green") + geom_point(aes(y = AirPassengers), size = 1.5, shape = 16, color = "blue")+ scale_x_date(date_breaks = "1 years", date_labels = "%Y")+ labs(title = "Yearly Time Series", subtitle="Sales trend from Year 2001 to 2017", caption="Source: Airpassengers Time Series Analysis", y="Sales", x = "Years")+ theme_economist_white()+ theme(axis.text.x = element_text(angle = 90, hjust = -.1)) 的调用中指定color = "green"来更改趋势线的颜色。对geom_smooth中的峰值执行相同的操作。最后,在这种情况下,geom_point似乎不知道autoplot,但是它可以与指定color一起使用(这会发出警告,但仍然可以工作)。

我不清楚您希望图例如何显示,您能否在评论中澄清?