添加误差线以在R中的图上显示标准偏差 [英] Add error bars to show standard deviation on a plot in R
本文介绍了添加误差线以在R中的图上显示标准偏差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
对于每个X
值,我计算了平均Y
值和每个Y值的标准偏差(sd
)
For each X
-value I calculated the average Y
-value and the standard deviation (sd
) of each Y-value
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?
推荐答案
当您具有对数X轴时,出现csgillespie解决方案问题.您将在左侧的右侧和右侧有不同长度的小条(epsilon遵循x值).
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).
您最好使用Hmisc
软件包中的errbar
函数:
You should better use the errbar
function from the Hmisc
package:
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")
)
这篇关于添加误差线以在R中的图上显示标准偏差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文