向R中的水平条形图添加误差线

阿龙

我非常感谢来自社区的小R问题的帮助。我到处搜索,但尚未找到解决方案。

我的数据如下所示:

trait2 <- c('A','B','C','D')
rg <- c (0.5480, 0.4801, 0.2805, -0.2480)
se <- c(0.0495, 0.0908, 0.0548, 0.0957)

trait2   rg      se
A       0.5480  0.0495
B       0.4801  0.0908
C       0.2805  0.0548
D       -0.2480  0.0957

我使用以下代码绘制了一个基本条形图:

barplot1 <- barplot(data$rg,
main="correlation between traits",
xlab="rG",  
border="blue", 
las=1, 
horiz=TRUE, 
names.arg=data$trait2, 
cex.names=0.5,
xlim=range(-0.4,0.6,0.1) )

正常工作: 条形图

但是我在使用以下代码的错误栏方面遇到了麻烦:

arrows(barplot1, 
   data$rg- data$se,
   data$rg+ data$se,
   lwd= 1.5,angle=90,code=3,length=0.05)

错误栏会出现,但不会出现在应该出现的地方: bar

这可能很简单,但是如果有人可以帮助我,我将非常感激。最好,阿伦

hpesoj626

如果为horiz = FALSE,则您的arrows代码块应为

arrows(x0 = barplot1, 
       y0 = data$rg - data$se,
       x1 = barplot1,
       y1 = data$rg + data$se,
       lwd= 1.5,angle=90,code=3,length=0.05)

但是,因为horiz = TRUE,你需要切换的位置x0y0x1y1在R中执行此操作的完整代码为:

barplot1 <- barplot(data$rg,
                    main="correlation between traits",
                    xlab="rG",  
                    border="blue", 
                    las=1, 
                    horiz = TRUE,
                    names.arg=data$trait2, 
                    cex.names=0.5,
                    xlim=range(-0.4,0.6,0.1))


segments(data$rg - data$se, barplot1, data$rg + data$se , barplot1,
         lwd = 1.5)

arrows(data$rg - data$se, barplot1, data$rg + data$se, barplot1, 
       lwd = 1.5, angle = 90,
       code = 3, length = 0.05)

在此处输入图片说明


在中更容易做到这一点ggplot2

ggplot(data, aes(trait2, rg)) + geom_col(color = "blue") + 
  geom_errorbar(aes(ymin = rg - se, ymax = rg + se), width = 0.3) + 
  coord_flip() + 
  theme_bw()

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章