美文网首页
条形图系列

条形图系列

作者: 鲨瓜 | 来源:发表于2023-01-20 09:14 被阅读0次

一、前言

条形图与柱形图类似,在柱形图的基础上进行了横纵坐标变换。条形图也是最简单的类别比较型图(之一),学术论文中经常使用的条形图主要有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=reorder(described_variables,mean),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)") +
    coord_flip() +
    # 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=reorder(described_variables,mean),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)") +
    coord_flip() +
  # 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=reorder(described_variables,mean),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)") +
  coord_flip() +
    # 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=reorder(described_variables,mean),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)") +
    coord_flip() +
  # 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含量一般高于男性。

本文由mdnice多平台发布

相关文章

  • recharts 条形/柱形图

    一、参数和用法 二、图表展现 1. 单系列条形图 数据处理: 作图: 2. 多系列条形图 3. 堆积条形图 设置t...

  • 22.Excel甘特图、旋风图

    知识点 一、制作双向条形图 选中原始数据-插入簇状条形图 右击条形图的一条图-设置数据系列格式-次坐标 右击上方次...

  • 条形图系列

    一、前言 条形图与柱形图类似,在柱形图的基础上进行了横纵坐标变换。条形图也是最简单的类别比较型图(之一),学术论文...

  • python条形竞赛图

    看到同事用tableau做了条形竞赛图,想用python实现一下 什么是条形图竞赛? 条形图竞赛是一系列动画的条形...

  • 第二部分 第6章 基本图形

    6.1 条形图 简单条形图 barplot() 堆砌条形图和分组条形图 均值条形图 条形图的微调 par()函数 ...

  • R语言绘制条形图

    数据 简单条形图 最基本用法:barplot() 水平条形图 数据 堆砌条形图 棘状图 分组条形图 数据 均值条形图

  • 第二章 条形图

    二、条形图 1,简单的条形图 2,绘制簇状条形图 3,绘制频数分布条形图 4,条形图着色 5,对正负条形图分别着色...

  • R语言:图形

    常用的图形,这里给出案例: barplot 条形图、单向量条形图 堆砌与分组条形图 添加标签 均值条形图 棘状图 ...

  • 第二章 选择有效的图表

    12个: 简单文本、散点图、表格、折线图、热力图、斜率图、竖直条形图、水平条形图、堆叠竖直条形图、堆叠水平条形图、...

  • R可视化之美之科研绘图-13.条形图绘制

    本内容为【科研私家菜】R可视化之美之科研绘图系列课程快来收藏关注【科研私家菜】 01 单数据系列条形图 效果如下:...

网友评论

      本文标题:条形图系列

      本文链接:https://www.haomeiwen.com/subject/cxhchdtx.html