ZL1023

V1

2023/01/19阅读:28主题:全栈蓝

柱形图系列

一、前言

柱形图是最简单的类别比较型图(之一),学术论文中经常使用的柱形图主要有2个作用:

  1. 展示不同类别变量的数值大小;
  2. 比较不同类别变量之间的大小关系。

1.1 单数据系列柱形图

文献来源
文献来源
原始图片
原始图片

1.2 多数据系列柱形图

文献来源
文献来源
原始图片
原始图片

1.3 堆积柱形图

文献来源
文献来源
原始图片
原始图片

1.4 百分比堆积柱形图

文献来源
文献来源
原始图片
原始图片

二、R包

本期使用的R包主要有3个:

  • gWQS包:提供演示数据集;
  • dlookr包:计算描述性统计量;
  • tidyverse包:用于绘制柱形图。

tidyverse包是一个集成包,包括

  1. ggplot2包:用于数据可视化;
  2. dplyr包:用于数据操作;
  3. tidyr包:用于数据整理;
  4. readr包:用于数据导入;
  5. purrr包:用于函数式编程;
  6. tibble:用于一种新型数据框;
  7. stringr包:用于字符串;
  8. forcats包:用于因子。
# load "gWQS" package
library(gWQS)
# load "dlookr" package
library(dlookr)
# load "tidyverse" package
library(tidyverse)

三、演示数据

演示数据简介:gWQS包中有一个内置数据集,内置数据集的名称叫wqs_datawqs_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)

四、R语言实现

4.1 单数据系列柱形图

PCBs %>%
 # calculate describe statistics
 describe(statistics=c("mean","sd")) %>%
 # draw bar plot
 ggplot() +
 # geometry layer
 geom_bar(aes(x=described_variables,y=mean),
      stat="identity",
      # visual channel mapping
      fill="blue")+
 # coordinate adjustment
  scale_x_discrete(name="PCBs") +
  scale_y_continuous(name="Concentration of PCBs in urine (mg/kg)") +
 # theme adjustment
 theme_light()

4.2 多数据系列柱形图

PCBs %>%
  # group by sex
  group_by(sex) %>%
  # calculate describe statistics
  describe(statistics=c("mean","sd")) %>%
  # draw bar plot
  ggplot() +
  # geometry layer
  geom_bar(aes(x=described_variables,y=mean,fill=sex),
           stat="identity",
           # visual channel mapping
           position="dodge",
           width=0.6) +
  # coordinate adjustment
  scale_x_discrete(name="PCBs") +
  scale_y_continuous(name="Concentration of PCBs in urine (mg/kg)") +
  # legend adjustment
  scale_fill_manual(name="Sex",values=c("red","blue"),labels=c("Male","Female")) +
  # theme adustment
  theme_light() +
  theme(legend.position="top")

4.3 堆积柱形图

PCBs %>%
  # group by sex
  group_by(sex) %>%
  # calculate describe statistics
  describe(statistics=c("mean","sd")) %>%
  # draw bar plot
  ggplot() +
  # geometry layer
  geom_bar(aes(x=described_variables,y=mean,fill=sex),
           stat="identity",
           # visual channel mapping
           position="stack",
           width=0.6) +
  # coordinate adjustment
  scale_x_discrete(name="PCBs") +
  scale_y_continuous(name="Concentration of PCBs in urine (mg/kg)") +
  # legend adjustment
  scale_fill_manual(name="Sex",values=c("red","blue"),labels=c("Male","Female")) +
  # theme adustment
  theme_light() +
  theme(legend.position="top")

4.4 百分比堆积柱形图

PCBs %>%
  # group by sex
  group_by(sex) %>%
  # calculate describe statistics
  describe(statistics=c("mean","sd")) %>%
  # draw bar plot
  ggplot() +
  # geometry layer
  geom_bar(aes(x=described_variables,y=mean,fill=sex),
           stat="identity",
           # visual channel mapping
           position="fill",
           width=0.6) +
  # coordinate adjustment
  scale_x_discrete(name="PCBs") +
  scale_y_continuous(name="Concentration of PCBs in urine (mg/kg)") +
  # legend adjustment
  scale_fill_manual(name="Sex",values=c("red","blue"),labels=c("Male","Female")) +
  # theme adustment
  theme_light() +
  theme(legend.position="top")

五、结果解读

NHANES数据库中多环芳烃的编码与对应名称。

编码 多环芳烃
LBX074LA PCB74
LBX099LA PCB99
LBX105LA PCB105
LBX118LA PCB118
LBX138LA PCB138
  1. PCB74是人体尿液中含量最高的PCBs,其次是PCB138、PCB105和PCB99,PCB118在人体尿液中的含量最低。
  2. 除了PCB74,女性尿液中PCBs含量一般高于男性。

分类:

其他

标签:

教育

作者介绍

ZL1023
V1