使用 ggplot2 绘制时间序列图
Time Series Plot using ggplot2
如何更改此图表中的以下内容?
- 更改趋势线的颜色(当前为蓝色)
- 更改峰值点的颜色(目前为橙色)
- 更改时间序列模式线的颜色(当前为黑色)
- 添加图例来表示以上三者的颜色
Here's the Plot I got from the code source
代码来源:
# 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))
可以使用以下语法进行前三个更改:
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))
通过在对 geom_smooth
的调用中指定 color = "green"
来更改趋势线的颜色。 geom_point
中的峰值点也是如此。最后,在这种情况下,autoplot
似乎不知道 color
,但它与指定 fill = "red"
一起工作(这会引发警告,但仍然有效)。
我不清楚你希望你的图例是什么样子,你能在评论中澄清一下吗?
如何更改此图表中的以下内容?
- 更改趋势线的颜色(当前为蓝色)
- 更改峰值点的颜色(目前为橙色)
- 更改时间序列模式线的颜色(当前为黑色)
- 添加图例来表示以上三者的颜色
Here's the Plot I got from the code source
代码来源:
# 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))
可以使用以下语法进行前三个更改:
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))
通过在对 geom_smooth
的调用中指定 color = "green"
来更改趋势线的颜色。 geom_point
中的峰值点也是如此。最后,在这种情况下,autoplot
似乎不知道 color
,但它与指定 fill = "red"
一起工作(这会引发警告,但仍然有效)。
我不清楚你希望你的图例是什么样子,你能在评论中澄清一下吗?