误差条形图基 R

Error bar chart base R

我是 R 的新手。对于一项作业,我必须创建一个分组条形图,其中包含基于 R 的误差线(因此不允许使用包)、格子和 ggplot2。对于基本 R 图,我编写了一些数据,并尝试制作一个像这样的简单条形图:

San_Diego <- c(65,20,74)
Rosarito <- c(34,35,23)
La_Paz <- c(21,71,28)
Mating_strategy <- c("Ultradominant","Dominant","Sneaker")
col <- c("darkorange1","skyblue3","gold2")        

lizards <- data.frame(row.names=Mating_strategy, San_Diego, 
                  Rosarito, La_Paz)
lizards.matrix <- as.matrix(lizards)

barplot(lizards.matrix,
        beside=T,
        col=col,
        ylim=c(0,80),
        xlab="Site",ylab="Frequency",
        legend.text=row.names(lizards.matrix),
        args.legend=list(x="top",bty="n"),
        las=1,
        cex.axis=1.2)

但现在我无法尝试向我的图表添加误差线。我尝试按照此处 (http://sickel.net/blogg/?p=1284) but I don't really understand what they are doing and when I tried it, it produced an entirely different, unusable graph. I also found this solution (http://imgur.com/126hJSI) 在线描述的方式进行操作,但我不明白应该从哪里获取数据的 ucl 和 lcl 值,因此效果也不是很好。

恐怕我还有很多东西要学,但我希望这里有人能帮助我。

提前致谢,

马利斯

http://sickel.net/blogg/?p=1284 之后,我将误差线添加到您的条形图中,如下所示。

首先,我 运行 定义示例数据的代码(即定义 lizard.matrix 行之前的所有内容)。之后,可以通过以下代码创建绘图:

# create bar plot
bp <- barplot(lizards.matrix,
              beside=T,
              col=col,
              ylim=c(0,100),
              xlab="Site",ylab="Frequency",
              legend.text=row.names(lizards.matrix),
              args.legend=list(x="top",bty="n"),
              las=1,
              cex.axis=1.2)

# create matrix of errors
lizards.error = matrix(c(10, 5, 12, 10, 8, 6, 12, 28, 3), ncol = 3)

# add vertical part of error bars
segments(bp, lizards.matrix - lizards.error, bp, lizards.matrix + lizards.error)

# horizontal parts of error bars
ew <- (bp[2,1]-bp[1,1])/4
segments(bp - ew, lizards.matrix - lizards.error, bp + ew, lizards.matrix - lizards.error)
segments(bp - ew, lizards.matrix + lizards.error, bp + ew, lizards.matrix + lizards.error)

代码的工作原理如下:

  • 我利用了 barplot() returns 包含条形水平坐标的矩阵这一事实。因此,我将 barplot() 的输出存储在变量 bp 中以备后用。另请注意,我更改了 ylim() 的范围,以确保图中有足够的空间放置误差线。

  • 然后我定义 lizards.error,其中包含图中每个条形的误差。它的结构遵循 lizards.matrix 的结构。所以 lizards.error[1, 1] 包含高度为 `lizards.matrix[1, 1].

  • 的错误
  • 然后使用函数 segments() 绘制误差线。与 base R 中的许多绘图函数一样,此函数为现有绘图添加了一些内容。它的四个相关参数是 x0 y0x1y1,它们定义了连接由 (x0y0 定义的点对的线段) 和 (x1, y1)。如果这些参数是向量,向量的每个分量定义一个点对,这样线段连接点 (x0[i], y0[i]) 和 (x1[i], y1[i] ) 对于所有 i.

  • segments() 现在用于定义构成误差条的三个部分中的每一个。首先是垂直部分,水平坐标与误差条相同,因此可以使用 bp。垂直坐标是根据柱的高度 (lizards.matrix) 和误差的大小 (lizards.error) 计算得出的。

  • 误差条的两条水平线的制作方法类似。在这里,您还需要定义线条的宽度,这是根据相邻条之间的距离计算得出的。条形的水平坐标存储在 bp 中,因此条形之间的距离(或等效地,条形的宽度)可以根据两个相邻坐标之间的差值计算得出:bp[2,1]-bp[1,1]。 (bp 是一个矩阵,[i, j] 获取第 i 行第 j 列的矩阵元素。)

编辑: 正如 rawr 指出的那样,使用 arrows() 的一次调用而不是 segments() 的三次调用可以获得类似的结果:

arrows(bp, lizards.matrix - lizards.error, bp, lizards.matrix + lizards.error,
       code = 3, angle = 90, length = 0.15)

  • 垂直线覆盖的范围与 segments() 完全相同。
  • code = 3 告诉函数在直线的两端绘制箭头。
  • angle 是箭头的轴与形成箭头的线之间的角度。 90 度角引出一条水平线。

这个解决方案显然更简单,因为它将三个函数调用替换为一个。我看到的唯一缺点是错误栏的宽度(length 参数)以英寸为单位给出,因此当绘制绘图的大小时更改时它可能会更改。在segments()的情况下,误差条的宽度以水平坐标给出。