r 散点图,两个方向都有误差条 [英] r scatter plot with error bars in both directions

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

问题描述

如何创建一个在两个方向上都带有误差线的散点图?通常误差棒在垂直方向(即 y 值的不确定性).但是我的数据在 x 值上也有不确定性

How can I create a scatter plot with error bars in two directions? Usually the error bars are in the vertical direction (i.e. the uncertainty in the y value). However my data has uncertainty in the x value as well

X      ErrX   Y     ErrY
1.0    0.1    3.0   0.2
1.5    0.3    4.2   0.1
etc

推荐答案

使用 ggplot2,这很容易.您可以完全控制误差条的所有四个边"的长度.使用 geom_errorbar() 设置 y 误差,使用 geom_errobarh()(h 表示水平)设置 x 误差.

Using ggplot2, this is easy. You have complete control over the length of all four "sides" of the errorbars. With geom_errorbar() you set the y-errors, and geom_errobarh() (the h is for horizontal) you set the x-errors.

#toy data
df <- data.frame(X = rnorm(4), errX = rnorm(4)*0.1, Y = rnorm(4), errY = rnorm(4)*0.2)

#load ggplot2
require(ggplot2)

#make graph
ggplot(data = df, aes(x = X, y = Y)) + geom_point() + #main graph
    geom_errorbar(aes(ymin = Y-errY, ymax = Y+errY)) + 
    geom_errorbarh(aes(xmin = X-errX, xmax = X+errX))

通过在 geom_errorbar() 中设置参数,您可以单独控制每个条的颜色、线宽等.有关详细信息,请参阅帮助和 Google.例如,您可以控制大写"的宽度或使用 width 参数完全消除它们.将上图与此图进行比较,以获取删除它们的示例:

You have separate control for the color of each bar, the linewidth, etc by setting parameters inside geom_errorbar(). See the help and Google for details. For example, you can control the width of the "caps" or eliminate them entirely with the width parameter. Compare the graph above to this one for an example of removing them:

ggplot(data = df, aes(x = X, y = Y)) + geom_point() + 
        geom_errorbar(aes(ymin = Y-errY, ymax = Y+errY), width = 0) + 
        geom_errorbarh(aes(xmin = X-errX, xmax = X+errX), height = 0)

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

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