|
金字塔图,由两套柱形图或条形图相对组合而成,因形似金字塔而得名,常用于表示不同性别的人口年龄构成等,在社会学、流行病学等领域中都被广泛使用。
(nature communications,2022)
当然,应用情景不止人口性别,上图这个案例中就分别展示了按年龄(a)以及按接种日期(b)划分的两类不同COVID-19疫苗的接种者分布情况。
今天我们就来学习一下这类特殊的组合柱形图,包括堆叠型的金字塔图如何绘制。
- #相关R包安装与载入:
- install.packages("apyramid") #绘制金字塔图
- install.packages('outbreaks') #含所需测试数据集
- library(outbreaks) #测试数据
- library(apyramid) #金字塔绘图包
- library(ggplot2)
- library(cols4all) #配色包
复制代码
1. 对未预先统计汇总的数据绘制金字塔图
- #调入测试数据集:
- ##2013年中国136例甲型H7N9流感病例
- flu <- outbreaks::fluH7N9_china_2013
- flu$age <- as.integer(flu$age)
- head(flu) #每行为一个患者的相关数据
复制代码
- #根据年龄段划分年龄分组:
- autocut <- function(x) {
- cut(x, breaks = pretty(x), right = TRUE, include.lowest = TRUE)
- }
- flu$age_group <- autocut(flu$age)
- levels(flu$gender) <- c("Female", "Male")
- head(flu)
复制代码
- #绘制人口金字塔图:
- p <- age_pyramid(
- flu,
- show_midpoint = TRUE, #是否用虚线展示每个组别中点
- proportional = FALSE, #默认展示案例数,如果为TRUE则展示百分比
- age_group = age_group, #年龄分组
- split_by = gender #性别
- )
- p
复制代码
对于处在同一个年龄区间的总案例数,绘图时会自动帮我们完成统计,非常便捷!
- #基于ggplot2,我们可以自由美化和定制主题
- #提取配色:
- c4a_gui()
- mycol <- c4a('pastel1',2)
- mycol
- #自定义配色/主题/标签:
- p1 <- p +
- scale_fill_manual(values=rev(mycol)) +
- theme_classic() +
- labs(
- x = "Age group (years)", #在图中为纵轴对应标签
- y = "Number of cases", #在图中为横轴对应标签
- fill = "Gender", #图例名
- title = "134 cases of influenza A H7N9 in China" #图表主标题
- )
- p1
复制代码
- #本案例中,存在两个性别缺失的数据行(绘图时默认剔除),如果想要展示缺失数据:
- mycol2 <- c4a('pastel1',3)
- mycol2
- p2 <- age_pyramid(flu, age_group = age_group, split_by = gender,
- na.rm = FALSE) + #展示缺失数据:na.rm = FALSE
- scale_fill_manual(values=rev(mycol2)) +
- theme_classic() +
- labs(
- x = "Age group (years)",
- y = "Number of cases",
- fill = "Gender",
- title = "136 cases of influenza A H7N9 in China"
- )
- p2
复制代码
2. 对已汇总的数据绘制金字塔图
如果我们的数据已经提前完成了每个区段的统计汇总,也可以直接调用该R包绘图。
- #调入测试数据集:
- ##美国2018年人口普查数据
- dt <- us_2018
- head(dt) #该数据集已根据年龄段对人口数(count)进行了汇总
复制代码
- p3 <- age_pyramid(us_2018,
- age_group = age,
- split_by = gender,
- count = count) #使用已有的汇总数据绘图
- p3
复制代码
- #主题美化:
- mycol3 <- c4a('bright',2)
- mycol3
- p4 <- p3 +
- scale_fill_manual(values=rev(mycol3)) +
- theme_classic() +
- labs(
- x = "Age group",
- y = "Thousands of people",
- fill = "Gender",
- title = "US Cenus Data 2018"
- )
- p4
复制代码
3. 堆叠金字塔图绘制
- #额外添加其它因素作为堆叠分层
- #3.1 按性别和健康保险状况分层绘制堆叠人口金字塔图
- dt2 <- us_ins_2018
- head(dt2)
复制代码
- p5 <- age_pyramid(dt2,
- age_group = age,
- split_by = gender,
- stack_by = insured, #按保险分层
- count = count) +
- scale_fill_manual(values=rev(mycol)) +
- theme_classic() +
- labs(
- x = "Age group",
- y = "Thousands of people",
- fill = "insured",
- title = "US Cenus Data 2018"
- )
- p5
复制代码
4. 使用ggplot2直接绘制金字塔图
更喜欢直接用ggplot2绘制也ok,我们只需要根据二分类将数据调整为一正一负后再绘制即可。
- #数据处理:
- df <- dt
- df[df$gender == "female",]$count <- -df[df$gender == "female",]$count
- head(df) #男-正;女-负
复制代码
- #ggplot2绘制金字塔图:
- p6 <- ggplot(data = df,
- aes(x = age , y = count, fill = gender)) +
- geom_bar(stat = "identity",position = "identity",
- color="black", size = 0.25) +
- scale_y_continuous(labels = abs,#显示绝对值
- limits = c(-12000, 12000),
- breaks = seq(-12000, 12000, 2000))
- p6
复制代码
- #配色/主题等美化:
- p7 <- p6 +
- scale_fill_manual(values=rev(mycol)) +
- theme_bw() +
- theme(axis.text.x = element_text(angle=60, hjust=1)) + #调整下X轴标签角度,避免重叠
- labs(
- x = "Age group",
- y = "Thousands of people",
- fill = "Gender",
- title = "US Cenus Data 2018"
- )
- p7
复制代码
- #也可以自由翻转
- p7 + coord_flip()
复制代码
好啦,我们今天的分享就到这里!
参考资料
https://www.rdocumentation.org/packages/apyramid/versions/0.1.2
参考文献
Burn, E., Li, X., Delmestri, A. et al. Thrombosis and thrombocytopenia after vaccination against and infection with SARS-CoV-2 in the United Kingdom. Nat Commun 13, 7167 (2022).
*未经许可,不得以任何方式复制或抄袭本篇文章之部分或全部内容。版权所有,侵权必究。
本文作者:基迪奥-喵酱
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|