簡體   English   中英

在ggplot中的散點圖上向點添加雙向誤差線

[英]Adding bidirectional error bars to points on scatter plot in ggplot

我試圖將x和y軸誤差條添加到散點圖中的每個單獨點。 每個點代表男性和女性健康的標准化平均值(n = 33)。

我找到了geom_errorbar和geom_errorbarh函數以及這個例子

ggplot2:向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值的位置

任何提示非常感謝

你真的應該學習一些教程。 您還沒有理解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回歸。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM