添加错误条以显示R中的图上的标准偏差

时间:2022-11-26 14:58:00

For each X-value I calculated the average Y-value and the standard deviation (sd) of each Y-value

对于每个x值,我计算了每个y值的平均y值和标准差(sd)

x  = 1:5
y  = c(1.1, 1.5, 2.9, 3.8, 5.2)
sd = c(0.1, 0.3, 0.2, 0.2, 0.4)

plot (x, y)

How can I use the standard deviation to add error bars to each datapoint of my plot?

如何使用标准差向图中的每个数据点添加错误条?

5 个解决方案

#1


18  

A Problem with csgillespie solution appears, when You have an logarithmic X axis. The you will have a different length of the small bars on the right an the left side (the epsilon follows the x-values).

当你有一个对数X轴时,csgillespie解决方案会出现问题。你会有不同长度的小条在右边,左边(和x值的后面)

You should better use the errbar function from the Hmisc package:

您应该更好地使用来自Hmisc包的errbar函数:

d = data.frame(
  x  = c(1:5)
  , y  = c(1.1, 1.5, 2.9, 3.8, 5.2)
  , sd = c(0.2, 0.3, 0.2, 0.0, 0.4)
)

##install.packages("Hmisc", dependencies=T)
library("Hmisc")

# add error bars (without adjusting yrange)
plot(d$x, d$y, type="n")
with (
  data = d
  , expr = errbar(x, y, y+sd, y-sd, add=T, pch=1, cap=.1)
)

# new plot (adjusts Yrange automatically)
with (
  data = d
  , expr = errbar(x, y, y+sd, y-sd, add=F, pch=1, cap=.015, log="x")
)

#2


24  

A solution with ggplot2 :

ggplot2解:

qplot(x,y)+geom_errorbar(aes(x=x, ymin=y-sd, ymax=y+sd), width=0.25)

添加错误条以显示R中的图上的标准偏差

#3


20  

You can use segments to add the bars in base graphics. Here epsilon controls the line across the top and bottom of the line.

您可以使用段在基本图形中添加条。在这里,控制了线的顶部和底部。

plot (x, y, ylim=c(0, 6))
epsilon = 0.02
for(i in 1:5) {
    up = y[i] + sd[i]
    low = y[i] - sd[i]
    segments(x[i],low , x[i], up)
    segments(x[i]-epsilon, up , x[i]+epsilon, up)
    segments(x[i]-epsilon, low , x[i]+epsilon, low)
}

As @thelatemail points out, I should really have used vectorised function calls:

正如@thelatemail所指出的,我真的应该使用矢量化函数调用:

segments(x, y-sd,x, y+sd)
epsilon = 0.02
segments(x-epsilon,y-sd,x+epsilon,y-sd)
segments(x-epsilon,y+sd,x+epsilon,y+sd)

添加错误条以显示R中的图上的标准偏差

#4


19  

In addition to @csgillespie's answer, segments is also vectorised to help with this sort of thing:

除了@csgillespie的答案外,片段也被矢量化来帮助处理这类事情:

plot (x, y, ylim=c(0,6))
segments(x,y-sd,x,y+sd)
epsilon <- 0.02
segments(x-epsilon,y-sd,x+epsilon,y-sd)
segments(x-epsilon,y+sd,x+epsilon,y+sd)

添加错误条以显示R中的图上的标准偏差

#5


19  

You can use arrows:

您可以使用箭头:

arrows(x,y-sd,x,y+sd, code=3, length=0.02, angle = 90)

#1


18  

A Problem with csgillespie solution appears, when You have an logarithmic X axis. The you will have a different length of the small bars on the right an the left side (the epsilon follows the x-values).

当你有一个对数X轴时,csgillespie解决方案会出现问题。你会有不同长度的小条在右边,左边(和x值的后面)

You should better use the errbar function from the Hmisc package:

您应该更好地使用来自Hmisc包的errbar函数:

d = data.frame(
  x  = c(1:5)
  , y  = c(1.1, 1.5, 2.9, 3.8, 5.2)
  , sd = c(0.2, 0.3, 0.2, 0.0, 0.4)
)

##install.packages("Hmisc", dependencies=T)
library("Hmisc")

# add error bars (without adjusting yrange)
plot(d$x, d$y, type="n")
with (
  data = d
  , expr = errbar(x, y, y+sd, y-sd, add=T, pch=1, cap=.1)
)

# new plot (adjusts Yrange automatically)
with (
  data = d
  , expr = errbar(x, y, y+sd, y-sd, add=F, pch=1, cap=.015, log="x")
)

#2


24  

A solution with ggplot2 :

ggplot2解:

qplot(x,y)+geom_errorbar(aes(x=x, ymin=y-sd, ymax=y+sd), width=0.25)

添加错误条以显示R中的图上的标准偏差

#3


20  

You can use segments to add the bars in base graphics. Here epsilon controls the line across the top and bottom of the line.

您可以使用段在基本图形中添加条。在这里,控制了线的顶部和底部。

plot (x, y, ylim=c(0, 6))
epsilon = 0.02
for(i in 1:5) {
    up = y[i] + sd[i]
    low = y[i] - sd[i]
    segments(x[i],low , x[i], up)
    segments(x[i]-epsilon, up , x[i]+epsilon, up)
    segments(x[i]-epsilon, low , x[i]+epsilon, low)
}

As @thelatemail points out, I should really have used vectorised function calls:

正如@thelatemail所指出的,我真的应该使用矢量化函数调用:

segments(x, y-sd,x, y+sd)
epsilon = 0.02
segments(x-epsilon,y-sd,x+epsilon,y-sd)
segments(x-epsilon,y+sd,x+epsilon,y+sd)

添加错误条以显示R中的图上的标准偏差

#4


19  

In addition to @csgillespie's answer, segments is also vectorised to help with this sort of thing:

除了@csgillespie的答案外,片段也被矢量化来帮助处理这类事情:

plot (x, y, ylim=c(0,6))
segments(x,y-sd,x,y+sd)
epsilon <- 0.02
segments(x-epsilon,y-sd,x+epsilon,y-sd)
segments(x-epsilon,y+sd,x+epsilon,y+sd)

添加错误条以显示R中的图上的标准偏差

#5


19  

You can use arrows:

您可以使用箭头:

arrows(x,y-sd,x,y+sd, code=3, length=0.02, angle = 90)