R-ggplot2-并列的Barplot如何绘制Errorbar

作者: TroyShen | 来源:发表于2020-01-10 12:55 被阅读0次

目录

  • 0.问题导入
  • 1.示例数据
  • 2.导入示例数据
  • 3.绘图数据框(data.frame)构建
  • 4.Barplot绘制并添加Errorbar(图1)
  • 5.Barplot中Errorbar重合Debug(图2)
  • 6.绘制有置信区间的Lineplot
  • 7.将Barplot 与Lineplot 合并
  • 8.总结
  • 9.本篇所使用的软件包(没有的需要用install.packages('')进行安装)
  • 10.致谢

0. 问题导入

之前有讲过如何在Barplot 中添加Errorbar, 但是示例只列举了单列Bar的绘图方式,那么并列的多个Barplot如何绘制Errorbar图呢?

此外,我们在处理过程中可能会面临如总GDP与人均GDP,或是总样本指标与人均指标在同一幅图中分别通过Barplot 与Lineplot进行分别展示?如何操作呢?

本篇给出以上两个问题的解决方案~

1. 示例数据

感谢西安交大刘博士提供的示例数据
点我下载示例数据

2. 导入示例数据

本步完成数据导入及总量(Number)与人均(Rate)的分组

setwd('/Users/jerseyshen/Documents/JianShu_Project/20200110')
files = list.files(pattern = '.csv$')
data = read.csv(files,header = T)
colnames(data) = c('measure','gender','age','type',
                   'val','upper','lower')

data = as.data.frame(data)

g1 = which(data$type == 'Number')
g2 = which(data$type == 'Rate')

pl_df1 = data[g1,]
pl_df2 = data[g2,]

3. 绘图数据框(data.frame)构建

本步完成总量数据框pl_df1与pl_df2的融合,至此完成绘图前的数据预处理。

pl_df1$age_id = rep(1:16,each = 2)
pl_df1$Rate = pl_df2$val
pl_df1$Rate_u = pl_df2$upper
pl_df1$Rate_l = pl_df2$lower

4.Barplot绘制并添加Errorbar(图1)

根据图1,我们可以看到两种类型的Errorbar居中重合了?这咋整?小编尝试过调试position,然并卵!那么如何De这个Bug,就接着往下看吧,一个参数搞定~

p1 = ggplot()+
  geom_bar(data = pl_df1,aes(x = age_id, y = val, fill = gender),
           position = 'dodge',stat = 'identity',width = 0.5)+
  geom_errorbar(data = pl_df1, aes(x = age_id, ymin = lower,ymax = upper),
                width = 0.4, 
                linetype = 'solid',
                position = position_dodge(0.5),color = 'black',size = 0.6)+
  scale_x_continuous(breaks = unique(pl_df1$age_id),labels = unique(pl_df1$age))+
  scale_color_lancet()+
  scale_fill_lancet()+
  theme_bw()+
  theme(
    axis.text.x =  element_text(face = 'bold',color = 'black',size = 12,angle = 45,vjust = 0.5),
    axis.text.y =  element_text(face = 'bold',color = 'black',size = 12),
    axis.title =  element_text(face = 'bold',color = 'black',size = 14, hjust = .5),
    legend.text = element_text(face = 'bold',color = 'black',size = 12),
    legend.title = element_blank(),
    legend.position = 'bottom',
    legend.direction = 'horizontal'
  )+
  xlab('Age')+
  ylab("Total")

png('plot1.png',
    height = 20,
    width = 20,
    units = 'cm',
    res = 800)
print(p1)
dev.off()
图1 Barplot绘制并添加Errorbar

5.Barplot中Errorbar重合Debug(图2)

是的,就是添加一行group = gender,成功Debug。

p2 = ggplot()+
  geom_bar(data = pl_df1,aes(x = age_id, y = val/1600, fill = gender),
           position = 'dodge',stat = 'identity',width = 0.5)+
  geom_errorbar(data = pl_df1, aes(x = age_id, ymin = lower/1600,ymax = upper/1600),
                width = 0.4, 
                linetype = 'solid',
                position = position_dodge(0.5),color = 'black',size = 0.6)+
  scale_x_continuous(breaks = unique(pl_df1$age_id),labels = unique(pl_df1$age))+
  scale_color_lancet()+
  scale_fill_lancet()+
  theme_bw()+
  theme(
    axis.text.x =  element_text(face = 'bold',color = 'black',size = 12,angle = 45,vjust = 0.5),
    axis.text.y =  element_text(face = 'bold',color = 'black',size = 12),
    axis.title =  element_text(face = 'bold',color = 'black',size = 14, hjust = .5),
    legend.text = element_text(face = 'bold',color = 'black',size = 12),
    legend.title = element_blank(),
    legend.position = 'bottom',
    legend.direction = 'horizontal'
  )+
  xlab('Age')+
  ylab("Total")
  
png('plot2.png',
      height = 20,
      width = 20,
      units = 'cm',
      res = 800)
print(p2)
dev.off()
图2 Barplot中Errorbar重合Debug

至此,本篇第一个小目标成功实现~
~来瓶快乐水happy一下吧~
接下来,我们将实现barplot与lineplot的实现


6. 绘制有置信区间的Lineplot

p3 = ggplot()+
  geom_line(data = pl_df1,aes(x = age_id,y = Rate,color = gender),size = 1)+
  geom_ribbon(data = pl_df1,aes(x = age_id,ymax = Rate_u, ymin = Rate_l,fill = gender),
              alpha = 0.2)+
  scale_x_continuous(breaks = unique(pl_df1$age_id),labels = unique(pl_df1$age))+
  scale_color_lancet()+
  scale_fill_lancet()+
  theme_bw()+
  theme(
    axis.text.x =  element_text(face = 'bold',color = 'black',size = 12,angle = 45,vjust = 0.5),
    axis.text.y =  element_text(face = 'bold',color = 'black',size = 12),
    axis.title =  element_text(face = 'bold',color = 'black',size = 14, hjust = .5),
    legend.text = element_text(face = 'bold',color = 'black',size = 12),
    legend.title = element_blank(),
    legend.position = 'bottom',
    legend.direction = 'horizontal'
  )+
  xlab('Age')+
  ylab('Total')

png('plot3.png',
    height = 20,
    width = 20,
    units = 'cm',
    res = 800)
print(p3)
dev.off()
图3 绘制有置信区间的Lineplot

7. 将Barplot 与Lineplot 合并

p4 = ggplot()+
  
  geom_bar(data = pl_df1,aes(x = age_id, y = val/1600, fill = gender),
           position = 'dodge',stat = 'identity',width = 0.5,alpha = 0.8)+
  geom_errorbar(data = pl_df1, aes(x = age_id, ymin = lower/1600,ymax = upper/1600),
                width = 0.4, 
                linetype = 'solid',
                position = position_dodge(0.5),color = 'black',size = 0.6)+
  
  geom_line(data = pl_df1,aes(x = age_id,y = Rate,color = gender),size = 1)+
  geom_ribbon(data = pl_df1,aes(x = age_id,ymax = Rate_u, ymin = Rate_l,fill = gender),
              alpha = 0.2)+
  scale_x_continuous(breaks = unique(pl_df1$age_id),labels = unique(pl_df1$age))+
  scale_y_continuous(breaks = c(250,500,750,1000,1250,1500),name = 'Total',
                     labels = format(as.character(c(250,500,750,1000,1250,1500)*1600),scientific = F),
                                sec.axis = sec_axis( ~. +0, breaks = seq(200,900,100),name = "Rate"))+
  scale_color_lancet()+
  scale_fill_lancet()+
  theme_bw()+
  theme(
    axis.text.x =  element_text(face = 'bold',color = 'black',size = 12,angle = 45,vjust = 0.5),
    axis.text.y =  element_text(face = 'bold',color = 'black',size = 12),
    axis.title =  element_text(face = 'bold',color = 'black',size = 14, hjust = .5),
    legend.text = element_text(face = 'bold',color = 'black',size = 12),
    legend.title = element_blank(),
    legend.position = 'bottom',
    legend.direction = 'horizontal'
  )+
  xlab('Age')
  
  png('plot4.png',
      height = 20,
      width = 20,
      units = 'cm',
      res = 800)
  print(p4)
  dev.off()
图4 将Barplot 与Lineplot 合并

8. 总结

本篇主要解决了以下几个问题:

  1. 如何解决并列Barplot中Errorline 重叠的问题?
  2. 如何绘制带有置信区间的lineplot?
  3. 如何将Barplot 与Lineplot 叠加绘制?

9. 本篇所使用的软件包(没有的需要用install.packages('')进行安装)

library(ggsci)
library(reshape2)
library(ggplot2)

10. 致谢

首先,感谢大家的持续关注,小编会继续努力,持续更新下去的!

大家如果觉得有帮助啊,还麻烦大家关注点赞,也可以扩散到朋友圈,多多引导朋友加入咱们这个简书技术平台, 代码共享推动科研进程, 多谢大家啦~

大家如果在使用本代码的过程有遇到问题的,可以留言评论,也可以私信我哈~~

小编联系方式

相关文章

网友评论

    本文标题:R-ggplot2-并列的Barplot如何绘制Errorbar

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