添加误差线以在 R 中的绘图上显示标准偏差 [英] Add error bars to show standard deviation on a plot in R

查看:28
本文介绍了添加误差线以在 R 中的绘图上显示标准偏差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于每个 X-value 我计算了平均 Y-value 和每个 Y-value 的标准偏差 (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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆