【问题标题】:Adding bidirectional error bars to points on scatter plot in ggplot在ggplot中的散点图上添加双向误差线
【发布时间】:2014-05-06 14:13:59
【问题描述】:

我正在尝试将 x 和 y 轴误差线添加到散点图中的每个单独的点。 每个点代表男性和女性 (n=33) 的健康标准平均值。

我找到了 geom_errorbar 和 geom_errorbarh 函数和这个例子

ggplot2 : Adding two errorbars to each point in scatterplot

但是我的问题是我想从我的数据集中的另一列中为每个点(我已经计算过)指定标准误差,如下所示

 line     MaleBL1   FemaleBL1  BL1MaleSE BL1FemaleSE
     3  0.05343516  0.05615977 0.28666600   0.3142001
     4 -0.53321642 -0.27279609 0.23929438   0.1350793
     5 -0.25853484 -0.08283566 0.25904025   0.2984323
     6 -1.11250479  0.03299387 0.23553281   0.2786233
     7 -0.14784506  0.28781883 0.27872358   0.2657080
    10  0.38168220  0.89476555 0.25620796   0.3108585
    11  0.24466921  0.14419021 0.27386482   0.3322349
    12 -0.06119015  1.42294820 0.32903199   0.3632367
    14  0.38957538  1.66850680 0.30362671   0.4437925
    15  0.05784842 -0.12453429 0.32319116   0.3372879
    18  0.71964923 -0.28669563 0.16336556   0.1911489
    23  0.03191843  0.13955703 0.34522310   0.1872229
    28 -0.04598340 -0.35156017 0.27001451   0.1822967

'line' 是人口(每个 n = 10 个人),其中每个值来自我的 x,y 变量是 'MaleBL1' 和 'FemaleBL1' 以及每个人口的标准误差男性和女性分别为'BL1MaleSE'和'BL1FemaleSE'

到目前为止,我的代码明智

p<-ggplot(BL1ggplot, aes(x=MaleBL1, y=FemaleBL1)) +
geom_point(shape=1) +    
geom_smooth(method=lm)+ # add regression line
xmin<-(MaleBL1-BL1MaleSE)
xmax<-(MaleBL1+BL1MaleSE)
ymin<-(FemaleBL1-BL1FemaleSE)
ymax<-(FemaleBL1+BL1FemaleSE)    
geom_errorbarh(aes(xmin=xmin,xmax=xmax))+
geom_errorbar(aes(ymin=ymin,ymax=ymax))

我认为最后两行指定误差线的限制是错误的。我只是不知道如何告诉 R 从 BL1MaleSEBL1FemaleSE 列中获取每个点的 SE 值

非常感谢任何提示

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    你真的应该学习一些教程。你还没有理解 ggplot2 的语法。

    BL1ggplot <- read.table(text=" line     MaleBL1   FemaleBL1  BL1MaleSE BL1FemaleSE
         3  0.05343516  0.05615977 0.28666600   0.3142001
         4 -0.53321642 -0.27279609 0.23929438   0.1350793
         5 -0.25853484 -0.08283566 0.25904025   0.2984323
         6 -1.11250479  0.03299387 0.23553281   0.2786233
         7 -0.14784506  0.28781883 0.27872358   0.2657080
        10  0.38168220  0.89476555 0.25620796   0.3108585
        11  0.24466921  0.14419021 0.27386482   0.3322349
        12 -0.06119015  1.42294820 0.32903199   0.3632367
        14  0.38957538  1.66850680 0.30362671   0.4437925
        15  0.05784842 -0.12453429 0.32319116   0.3372879
        18  0.71964923 -0.28669563 0.16336556   0.1911489
        23  0.03191843  0.13955703 0.34522310   0.1872229
        28 -0.04598340 -0.35156017 0.27001451   0.1822967", header=TRUE)
    
    library(ggplot2)
    p<-ggplot(BL1ggplot, aes(x=MaleBL1, y=FemaleBL1)) +
      geom_point(shape=1) +    
      geom_smooth(method=lm)+ 
      geom_errorbarh(aes(xmin=MaleBL1-BL1MaleSE,
                       xmax=MaleBL1+BL1MaleSE),
                     height=0.2)+
      geom_errorbar(aes(ymin=FemaleBL1-BL1FemaleSE,
                        ymax=FemaleBL1+BL1FemaleSE),
                    width=0.2)
    
    print(p)
    

    顺便说一句,查看误差条,您可能应该使用戴明回归或总最小二乘法而不是 OLS 回归。

    【讨论】:

    • 非常感谢您的快速回复。我目前正在尝试掌握 ggplot 但我被要求匆忙完成此操作并且无处可去。有没有办法将误差线的末端添加到图中。我将研究使用其他回归类型。干杯
    猜你喜欢
    • 2021-08-06
    • 1970-01-01
    • 2021-10-07
    • 2016-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-13
    相关资源
    最近更新 更多