R语言堆叠面积图

library(gcookbook) # Load gcookbook for the uspopage data set
library(ggplot2)
# 使用 ggplot2:以 uspopage 为数据源,设置美学映射
ggplot(
uspopage,
aes(
x = Year, # x轴:年份
y = Thousands, # y轴:Thousands
fill = AgeGroup, # 用 AgeGroup 决定填充颜色
order = dplyr::desc(AgeGroup) # 设置堆叠/绘制顺序:AgeGroup 倒序
)
) +
# 画堆叠面积图
# colour = NA:不画面积边界线(避免边界干扰)
# alpha = .4:填充透明度 0.4
geom_area(colour = NA, alpha = .4) +
# 使用 RColorBrewer 的 Blues 配色方案为 fill 上色
scale_fill_brewer(palette = "Blues") +
# 在“堆叠结构一致”的基础上叠加折线
# position = "stack":线的位置按堆叠方式计算
# size = .2:线宽很细
geom_line(position = "stack", size = .2)

评论

发表回复