1
votes

I am trying to add x and y axis error bars to each individual point in a scatter plot. Each point represents a standardized mean value for fitness for males and females (n=33).

I have found the geom_errorbar and geom_errorbarh functions and this example

ggplot2 : Adding two errorbars to each point in scatterplot

However my issue is that I want to specify the standard error for each point (which I have already calculated) from another column in my dataset which looks like this below

 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' is the population (n=10 individuals in each) from where each value comes from my x,y variables are 'MaleBL1' & 'FemaleBL1' and the standard error for each populations for males and females respectively 'BL1MaleSE' & 'BL1FemaleSE'

So far code wise I have

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))

I think the last two lines are wrong with specifying the limits of the error bars. I just don't know how to tell R where to take the SE values for each point from the columns BL1MaleSE and BL1FemaleSE

Any tips greatly appreciated

1

1 Answers

5
votes

You really should study some tutorials. You haven't understood ggplot2 syntax.

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)

enter image description here

Btw., looking at the errorbars you should probably use Deming regression or Total Least Squares instead of OLS regression.