使用 ggplot2 绘制聚合后的时间序列
聚合数据有助于发现总体模式和趋势,但往往会丢失细节与上下文。不过,结合使用 ggplot2 的方法,能够在可视化中为聚合数据补充一定的上下文。
在本练习中,您将把每周聚合的温度读数 weekly_avg 与原始的未聚合时间序列 hourly_temperature 一起绘制。后者表示整整一年的逐小时温度读数。
时间序列 hourly_temperature 和 weekly_avg,以及 ggplot2 与 zoo 包都已为您准备好。
本练习是课程的一部分
R 中的时间序列数据处理
练习说明
使用
ggplot()函数,将hourly_temperature时间序列绘制为折线图。添加 y 轴 标签
"Degrees Celsius"和标题"Temperature Readings"。完成对
geom_line()和aes()的第二次调用,将weekly_avg时间序列叠加到图上。将每周聚合线条的颜色改为红色,线条粗细设为
2。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Create a plot of the hourly_temperature time series
ggplot(___, aes(___)) +
___ +
scale_y_continuous() +
# Add axis label and title
labs(___) +
# Add a line plot for the weekly aggregated time series
geom_line(data = ___, aes(___),
# Color the aggregated line in red, with a size of 2
___)