简体   繁体   English

向条形图添加误差线

[英]Add error bars to a barplot

I have two vectors. 我有两个向量。 I want to make a barplot of the first vector (simple enough, right). 我想制作第一个向量的小样图(足够简单,正确)。 The twist is that every element of the second vector is the standard deviation for every element of the first vector (which itself is the average of 4 other values). 扭曲之处在于,第二个向量的每个元素都是第一个向量的每个元素的标准偏差(它本身是其他4个值的平均值)。 How can I do that? 我怎样才能做到这一点?

The vectors in question: 有问题的向量:

-4.6521175 0.145839723
 1.1744100 0.342278694
-0.2581400 0.003776341
-0.3452675 0.073241199
-2.3823650 0.095008502
 0.5625125 0.021627196

Ie, how can I add the elements of the second column vector as error bars to the corresponding elements in the first column vector? 即,如何将第二列向量的元素作为误差线添加到第一列向量中的相应元素?

在此处输入图片说明

Note: Before you ask, yes I did search extensively on this site and did a lot of googling, but my problem is a bit more specific, ie what I found didn't match what I needed. 注意:是的,在您问之前,是的,我确实在该站点上进行了广泛的搜索,并且进行了大量的谷歌搜索,但是我的问题是更具体的,即我发现的内容与我的需求不符。

I personally like arrows() best for this kind of graphic: 我个人最喜欢这类图形的arrows()

df <- data.frame(bar = c(-4.6521175, 1.1744100, -0.2581400,  -0.3452675, -2.3823650, 0.5625125),
error = c(0.145839723, 0.342278694, 0.003776341, 0.073241199, 0.095008502, 0.021627196))

foo <- barplot(df$bar,ylim=c(-6,2),border=NA)
arrows(x0=foo,y0=df$bar+df$error,y1=df$bar-df$error,angle=90,code=3,length=0.1)

在此处输入图片说明

Two details: 两个细节:

  1. border=NA in barplot() removes the borders around the bars, so you can actually see the error whiskers around the third bar. barplot()中的border=NA会删除小节周围的边框,因此您实际上可以在第三小节周围看到错误信息。 Since the third error is so small, the whisker lies pretty much on top of the bar border. 由于第三个误差非常小,因此晶须几乎位于条形边框的顶部。

  2. I used the length parameter in arrows() to reduce the width of the horizontal whiskers, which is especially relevant if we have larger numbers of bars. 我在arrows()使用了length参数来减小水平晶须的宽度,这在我们拥有更多条形的情况下尤其重要。 The default is length=0.25 . 默认值为length=0.25

However, note that "dynamite plots" have major disadvantages . 但是,请注意, “炸药地块”具有主要缺点 You write that your data come from just four raw points for each bar. 您写道,每个条形图的数据仅来自四个原始点。 In such a case it would almost certainly be better to just plot a (jittered) dotplot of your raw data. 在这种情况下,仅绘制原始数据的(抖动)点图几乎可以肯定会更好。

An implementation with geom_bar and geom_errorbar of ggplot2 : geom_bargeom_errorbarggplot2

library(ggplot2)
ggplot(df, aes(x=row.names(df), y=V1)) +
  geom_bar(stat="identity", fill="grey") +
  geom_errorbar(aes(ymin = V1 - V2, ymax = V1 + V2), width=0.6) +
  theme_classic() 

this results in: 结果是:

在此处输入图片说明

If you want to remove the numbers on the x-axis, you can add: 如果要删除x轴上的数字,可以添加:

  theme(axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank())

to your ggplot code. 到您的ggplot代码。


Used data: 使用的数据:

df <- read.table(text="-4.6521175 0.145839723
 1.1744100 0.342278694
-0.2581400 0.003776341
-0.3452675 0.073241199
-2.3823650 0.095008502
 0.5625125 0.021627196", header=FALSE)

In response to your comment , two possible solution when you want plot such a large number of bars: 为了回应您的评论 ,当您要绘制大量条形图时,有两种可能的解决方案:

1: Only include a selection of the axis-labels: 1:仅包括轴标签的选择:

ggplot(df2, aes(x=as.numeric(row.names(df2)), y=V1)) +
  geom_bar(stat="identity", fill="grey", width=0.7) +
  geom_errorbar(aes(ymin = V1 - V2, ymax = V1 + V2), width=0.5) +
  scale_x_continuous(breaks=c(1,seq(10,200,10)), expand=c(0,0)) +
  theme_classic() +
  theme(axis.text.x=element_text(size = 6, angle = 90, vjust = 0.5))

this gives: 这给出了:

在此处输入图片说明

As can be seen, it is not ideal to cram so many bars in a plot. 可以看出,在情节中填充如此多的条形图并不理想。 See therefore alternative 2. 因此,见备选案文2。

2: Create a grouping variable which you can use for creating facets: 2:创建可用于创建构面的分组变量:

df2$id <- rep(letters[1:20], each=10)

ggplot(df2, aes(x=as.numeric(row.names(df2)), y=V1)) +
  geom_bar(stat="identity", fill="grey", width=0.7) +
  geom_errorbar(aes(ymin = V1 - V2, ymax = V1 + V2), width=0.5) +
  scale_x_continuous(breaks=as.numeric(row.names(df2))) +
  facet_wrap(~ id, scales = "free_x") +
  theme_bw() +
  theme(axis.text.x=element_text(angle = 90, vjust = 0.5))

this gives: 这给出了:

在此处输入图片说明

Used data for the two last examples: 最后两个示例使用的数据:

df2 <- data.frame(V1=sample(df$V1, 200, replace=TRUE),
                  V2=sample(df$V2, 200, replace=TRUE))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM