R语言:ggplot2画带误差棒的组合柱状图教程。

1.cowplotR包的安装,输入代码install.packages(“cowplot”),安装成功后,用library(“cowplot”)调用该R包。

2.以单个柱状图为元素,做出柱状图,原始数据如表1,代码如图1:

表1 原始数据表

suppdoeslenSDSE
OJ0.513.24.52.1
OJ122.73.92
OJ226.12.71.6
VC0.582.81.7
VC116.82.51.6
VC226.14.82.2
library("ggplot2")
read.csv("C:\\Users\\26869\\Desktop\\柱状图.csv")
df2<-read.csv("C:\\Users\\26869\\Desktop\\r语言.csv")
df2
ggplot(df2, aes(x=does, y=len, fill=supp)) + 
  geom_bar(position=position_dodge(), stat="identity") +
  geom_errorbar(aes(ymin=len-SE, ymax=len+SE),size=0.75,width=0.08,position=position_dodge(0.45))+
  scale_y_continuous(breaks=0:20*4) +
  scale_x_continuous(breaks=0:10*0.5)+
  theme_bw()+
  theme(panel.grid.major=element_line(colour=NA),
          panel.background = element_rect(fill = "transparent",colour = NA),
          plot.background = element_rect(fill = "transparent",colour = NA),
          panel.grid.minor = element_blank(),legend.justification=c(0.85,0.1),legend.position=c(0.85,0.1))
  labs(x=NULL,y="长度",title=NULL)

图1 单个柱状图代码

3.以相同的代码套用不同数据,得到若干个单个柱状图(这里我们直接用相同的数据)。

4.将上面单个柱状图代码记为p1,然后调用cowplotR包,代码如图2.

 p1<-ggplot(df2, aes(x=does, y=len, fill=supp)) + 
    geom_bar(position=position_dodge(), stat="identity") +
    geom_errorbar(aes(ymin=len-SE, ymax=len+SE),size=0.75,width=0.08,position=position_dodge(0.45))+
    scale_y_continuous(breaks=0:20*4) +
    scale_x_continuous(breaks=0:10*0.5)+
  theme_bw()+
    theme(panel.grid.major=element_line(colour=NA),
          panel.background = element_rect(fill = "transparent",colour = NA),
          plot.background = element_rect(fill = "transparent",colour = NA),
          panel.grid.minor = element_blank(),legend.justification=c(0.85,0.1),legend.position=c(0.85,0.1))
  labs(x=NULL,y="长度",title=NULL)
  library(cowplot)
  plot_grid(p1,p1,p1,p1,ncol=2,nrow=2)

图2 组合柱状图代码

5.运行代码。结果如图3:

 

 


 

  • 5
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

达尔不是贝吉塔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值