一、前言
饼图是最简单的局部整体型图(之一),也是百分比堆积柱形图的变体,学术论文中经常使用的饼图主要有1个作用:
- 展示不同类别变量之间的占比情况。
1.1 饼图-示例
文献来源 原始图片1.2 圆环图-示例
文献来源 原始图片1.3 复合饼图-示例
文献来源 原始图片二、R包
本期使用的R包有3个:
- tidyverse包:绘制饼图;
- dlookr包:计算描述性统计量;
- scatterpie包:绘制复合饼图;
- gWQS包:提供演示数据。
# load "tidyverse" package
library(tidyverse)
# load "dlookr" package
library(dlookr)
# load "scatterpie" package
library(scatterpie)
# load "gWQS" package
library(gWQS)
三、演示数据
3.1 原始数据
gWQS包中有一个内置数据集,内置数据集的名称叫wqs_data,wqs_data数据集有34种多环芳烃暴露数据、25种邻苯二甲酸酯暴露数据和其他类型数据。
本期仅使用wqs_data数据集的前5种多环芳烃暴露数据和性别。
# PCBs name
PCBs_name <- c("LBX074LA","LBX099LA","LBX105LA","LBX118LA","LBX138LA")
# get the first 5 PCBs exposure data and sex
PCBs <- wqs_data[c(PCBs_name,"sex")]
# get PCBs' absolute value
PCBs[PCBs_name] <- abs(PCBs[PCBs_name])
# view PCBs data
head(PCBs)
3.2 计算统计量和标签
PCBs_pie = PCBs[1:5] %>%
# calculate describtive statistics
describe(statistics=c("mean","sd"))
# create pie label
PCBs_pie$label = paste0(PCBs_pie$described_variables,
" (",
round(PCBs_pie$mean/sum(PCBs_pie$mean)*100,2),
"%)")
# view data
head(PCBs_pie)
3.3 复合饼图模拟数据
PCBs_composite_pie = PCBs %>%
# group by sex
group_by(sex) %>%
# calculate describtive statistics
describe(statistics=c("mean","sd")) %>%
# select colume
select(c("described_variables","sex","mean")) %>%
# long to wider
pivot_wider(names_from=described_variables,values_from=mean)
# create x
PCBs_composite_pie$x = c(50,100)
# create y
PCBs_composite_pie$y = c(50,100)
# create ridius
PCBs_composite_pie$ridius = c(5,10)
四、R语言实现
4.1 饼图
# draw plot
ggplot()+
# geometric layer
geom_bar(data=PCBs_pie,mapping=aes(x="",y=mean,fill=label),
color="black",
stat="identity")+
# coordinate adjustment
coord_polar(theta="y")+
# theme adjustment
theme_void()
4.2 圆环图
# draw plot
ggplot()+
# geometric layer
geom_bar(data=PCBs_pie,mapping=aes(x="",y=mean,fill=label),
color="black",
stat="identity")+
# coordinate adjustment
scale_x_discrete(expand=expansion(mult=c(1,0)))+
coord_polar(theta="y")+
# theme adjustment
theme_void()
4.3 复合饼图
散点复合饼图
# draw plot
ggplot()+
# geometric layer
geom_scatterpie(data=PCBs_composite_pie,mapping=aes(x,y),
cols=PCBs_name)+
# coordinate adjustment
coord_equal()+
# theme adjustment
theme_light()+
theme(axis.text=element_text(color="black"))
气泡复合饼图
# draw plot
ggplot()+
# geometric layer
geom_scatterpie(data=PCBs_composite_pie,mapping=aes(x=x,y=y,r=ridius),
cols=PCBs_name)+
# coordinate adjustment
coord_equal()+
# legend adjustment
geom_scatterpie_legend(PCBs_composite_pie$ridius,x=100,y=50,n=2)+
# theme adjustment
theme_light()+
theme(axis.text=element_text(color="black"))
五、结果分析
NHANES数据库中多环芳烃的编码与对应名称。
编码 | 多环芳烃 |
---|---|
LBX074LA | PCB74 |
LBX099LA | PCB99 |
LBX105LA | PCB105 |
LBX118LA | PCB118 |
LBX138LA | PCB138 |
- PCB74是人体尿液中含量最高的PCBs,其次是PCB138、PCB105和PCB99,PCB118在人体尿液中的含量最低。
本文由mdnice多平台发布
网友评论