带有误差线的散点图 [英] Scatter plot with error bars

查看:535
本文介绍了带有误差线的散点图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在R中生成以下图?图中显示的点是平均值,其范围对应于最小值和最大值. 我有两个文件中的数据(下面是一个示例).

How can I generate the following plot in R? Points, shown in the plot are the averages, and their ranges correspond to minimal and maximal values. I have data in two files (below is an example).

x   y
1   0.8773
1   0.8722
1   0.8816
1   0.8834
1   0.8759
1   0.8890
1   0.8727
2   0.9047
2   0.9062
2   0.8998
2   0.9044
2   0.8960
..  ...

推荐答案

首先:非常遗憾和令人惊奇的是 R不能开箱即用"绘制误差线.

First of all: it is very unfortunate and surprising that R cannot draw error bars "out of the box".

这是我最喜欢的解决方法,优点是您不需要任何额外的程序包.诀窍是绘制箭头(!),但要用很少的水平线代替箭头(!!!).这个不太直接的想法来自 R Wiki提示,并且已被复制.这里作为一个可行的例子.

Here is my favourite workaround, the advantage is that you do not need any extra packages. The trick is to draw arrows (!) but with little horizontal bars instead of arrowheads (!!!). This not-so-straightforward idea comes from the R Wiki Tips and is reproduced here as a worked-out example.

让我们假设您有一个平均值" avg的向量和另一个标准偏差" sdev的向量,它们的长度均n.让我们使横坐标只是这些度量"的数量,所以x <- 1:n.使用这些,绘制命令如下:

Let's assume you have a vector of "average values" avg and another vector of "standard deviations" sdev, they are of the same length n. Let's make the abscissa just the number of these "measurements", so x <- 1:n. Using these, here come the plotting commands:

plot(x, avg,
    ylim=range(c(avg-sdev, avg+sdev)),
    pch=19, xlab="Measurements", ylab="Mean +/- SD",
    main="Scatter plot with std.dev error bars"
)
# hack: we draw arrows but with very special "arrowheads"
arrows(x, avg-sdev, x, avg+sdev, length=0.05, angle=90, code=3)

结果如下:

arrows(...)函数中,length=0.05是箭头"的大小(以英寸为单位),angle=90指定箭头"垂直于箭头的轴,并且特别直观的code=3参数指定我们想要在箭头的两端绘制一个箭头.

In the arrows(...) function length=0.05 is the size of the "arrowhead" in inches, angle=90 specifies that the "arrowhead" is perpendicular to the shaft of the arrow, and the particularly intuitive code=3 parameter specifies that we want to draw an arrowhead on both ends of the arrow.

对于水平误差线,需要进行以下更改,假设sdev向量现在包含x值中的误差,并且y值是纵坐标:

For horizontal error bars the following changes are necessary, assuming that the sdev vector now contains the errors in the x values and the y values are the ordinates:

plot(x, y,
    xlim=range(c(x-sdev, x+sdev)),
    pch=19,...)
# horizontal error bars
arrows(x-sdev, y, x+sdev, y, length=0.05, angle=90, code=3)

这篇关于带有误差线的散点图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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