分组比较柱形图(含误差线)

欢迎关注微信公众号(医学生物信息学),医学生的生信笔记,记录学习过程。

带误差线的柱形图

# 使用每个类别的均值作为柱形的高度,再根据每个类别的标准差绘制误差线
library(ggplot2)
library(RColorBrewer)
library(gcookbook) 
ggplot(heightweight, aes(x = sex, y = heightIn))+ 
  stat_summary(mapping=aes(fill = sex),fun=mean, fun.args = list(mult=1),geom='bar',colour="black",width=.7) +
  stat_summary(fun.data = mean_sdl, fun.args = list(mult=1),geom='errorbar', color='black',width=.2)

ggplot(heightweight, aes(x = sex, y = heightIn))+ 
  stat_summary(mapping=aes(fill = sex),fun=mean, fun.args = list(mult=1),geom='bar',colour="black",width=.7) +
  stat_summary(fun.data = mean_sdl, fun.args = list(mult=1),geom='errorbar', color='black',width=.2) +    
  #scale_fill_manual(values=c(brewer.pal(7,"Set2")[c(1,2)]))+
  ylim(0,80)+
  theme_classic()

ggplot(heightweight, aes(x = sex, y = heightIn))+ 
  stat_summary(mapping=aes(fill = sex),fun=mean, fun.args = list(mult=1),geom='bar',colour="black",width=.7) +
  stat_summary(fun.data = mean_sdl, fun.args = list(mult=1),geom='errorbar', color='black',width=.2) +    
  #scale_fill_manual(values=c(brewer.pal(7,"Set2")[c(1,2)]))+
  ylim(0,80)+
  theme_classic()+
  theme(panel.background=element_rect(fill="white",colour="black",linewidth=0.25),
        axis.line=element_line(colour="black",linewidth=0.25),
        axis.title=element_text(size=13,face="plain",color="black"),
        axis.text = element_text(size=12,face="plain",color="black"),
        legend.position="none"
  )

带误差线合并点抖动柱形图

library(ggplot2)
library(RColorBrewer)

data("ToothGrowth")
ToothGrowth$dose<-factor(ToothGrowth$dose)

ggplot(ToothGrowth, aes(x = dose, y = len))+ 
  #添加柱形图
  stat_summary(fun.y=mean, geom='bar', fun.args = list(mult=1),colour="black",fill="white",width=.7) +
  #添加误差线
  stat_summary(fun.data = mean_sdl, fun.args = list(mult=1),geom='errorbar', color='black',width=.2) + 
  #添加抖动散点图
  geom_jitter(aes(fill = dose),position = position_jitter(0.2),shape=21, size = 2,alpha=0.9)+
  #scale_fill_manual(values=c(brewer.pal(7,"Set2")[c(1,2)]))+
  theme_light()

library(ggplot2)
library(RColorBrewer)
library(gcookbook) 

ggplot(heightweight, aes(x = sex, y = heightIn))+ 
  #添加柱形图
  stat_summary(fun.y=mean, geom='bar', fun.args = list(mult=1),colour="black",fill="white",width=.7) +
  #添加误差线
  stat_summary(fun.data = mean_sdl, fun.args = list(mult=1),geom='errorbar', color='black',width=.2) + 
  #添加抖动散点图
  geom_jitter(aes(fill = sex),position = position_jitter(0.2),shape=21, size = 2,alpha=0.9)+
  #scale_fill_manual(values=c(brewer.pal(7,"Set2")[c(1,2)]))+
  theme_light()

ggplot(heightweight, aes(x = sex, y = heightIn))+ 
  stat_summary(fun.y=mean, fun.args = list(mult=1),geom='bar',colour="black",fill="white",width=.7) +
  stat_summary(fun.data = mean_sdl,fun.args = list(mult=1), geom='errorbar', color='black',width=.2) +    
  geom_jitter(aes(fill = sex),position = position_jitter(0.2),shape=21, size = 2,alpha=0.9)+
  #scale_fill_manual(values=c(brewer.pal(7,"Set2")[c(1,2)]))+
  theme_classic()+
  theme(panel.background=element_rect(fill="white",colour="black",size=0.25),
        axis.line=element_line(colour="black",size=0.25),
        axis.title=element_text(size=13,face="plain",color="black"),
        axis.text = element_text(size=12,face="plain",color="black"),
        legend.position="none"
  )

参考资料

[1] https://github.com/EasyChart/Beautiful-Visualization-with-R

[2] R语言数据可视化之美:专业图表绘制指南(增强版) (张杰)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值