R-ggplot2: Adding both vertical and horizontal error bars in each point of a scatter plot

Sometimes it might be necessary to show both horizontal and vertical error bars/point in a scatterplot if both the dependent (Y) and independent (X) variables are averages of some other values. This is my personal note to keep things organized and ease of future use. I’d be happy if anyone is benefitted.

Suppose, we have collected data of biomass and height of some plants and recorded the species names. Now we want to see how biomass is related to plant’s height across species. To do so lets plot the average biomass in Y-axis and average height in X-axis in a scatter plot and fit a linear regression line across the species. We also want to draw error bars for each mean biomass and mean height pair. Here is how we can do:


# Create the dataset
biomass = c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15)
height = c(0.15,.22,.34,.41,.5,.62,.71,.81,.92,1.01,1.1,1.25,1.32,1.41,1.52)
species = c('a','a','a','a','a','b','b','b','b','b','c','c','c','c','c')

dummyData = data.frame(biomass,height,species)
dummyData

library(Rmisc)

# Calculating SE (standard error) which will be used for error bars
bmassStat = summarySE(dummyData,measurevar=”biomass”,groupvars = “species”)
bmassStat
heightStat = summarySE(data=dummyData,measurevar = “height”,groupvars = “species”)
heightStat

# Combining variables and statistics
bmassHt = data.frame(bmassStat$biomass,bmassStat$se,heightStat$height,heightStat$se,heightStat$species)
names(bmassHt)=c(“avgBiomass”,”biomassSE”,”avgHeight”,”heightSE”,”species”)
bmassHt

library(ggplot2)
ggplot(data=bmassHt,aes(x=avgHeight,y=avgBiomass,color=species))+
geom_smooth(method=’lm’,se=FALSE,color=”black”)+
geom_point()+
geom_errorbar(aes(ymax=avgBiomass+biomassSE,ymin=avgBiomass-biomassSE,width=0))+
geom_errorbarh(aes(xmax=avgHeight+heightSE,xmin=avgHeight-heightSE))

# geom_errobarh creates the horizontal error bar for the variable in the X-axis (in this case avgHeight)

So, this is the final graph:
Horizontal & Vertical Error Bars

Please let me know if you have any questions or suggestions.

Sometimes it might be necessary to show both horizontal and vertical error bars/point in a scatterplot if both the dependent (Y) and independent (X) variables are averages of some other values. This is my personal note to keep things organized and ease of future use. I’d be happy if anyone is benefitted.

Suppose, we have collected data of biomass and height of some plants and recorded the species names. Now we want to see how biomass is related to plant’s height across species. To do so lets plot the average biomass in Y-axis and average height in X-axis in a scatter plot and fit a linear regression line across the species. We also want to draw error bars for each mean biomass and mean height pair. Here is how we can do:


# Create the dataset
biomass = c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15)
height = c(0.15,.22,.34,.41,.5,.62,.71,.81,.92,1.01,1.1,1.25,1.32,1.41,1.52)
species = c('a','a','a','a','a','b','b','b','b','b','c','c','c','c','c')

dummyData = data.frame(biomass,height,species)
dummyData

library(Rmisc)

# Calculating SE (standard error) which will be used for error bars
bmassStat = summarySE(dummyData,measurevar=”biomass”,groupvars = “species”)
bmassStat
heightStat = summarySE(data=dummyData,measurevar = “height”,groupvars = “species”)
heightStat

# Combining variables and statistics
bmassHt = data.frame(bmassStat$biomass,bmassStat$se,heightStat$height,heightStat$se,heightStat$species)
names(bmassHt)=c(“avgBiomass”,”biomassSE”,”avgHeight”,”heightSE”,”species”)
bmassHt

library(ggplot2)
ggplot(data=bmassHt,aes(x=avgHeight,y=avgBiomass,color=species))+
geom_smooth(method=’lm’,se=FALSE,color=”black”)+
geom_point()+
geom_errorbar(aes(ymax=avgBiomass+biomassSE,ymin=avgBiomass-biomassSE,width=0))+
geom_errorbarh(aes(xmax=avgHeight+heightSE,xmin=avgHeight-heightSE))

# geom_errobarh creates the horizontal error bar for the variable in the X-axis (in this case avgHeight)

So, this is the final graph:
Horizontal & Vertical Error Bars

Please let me know if you have any questions or suggestions.

Sometimes it might be necessary to show both horizontal and vertical error bars/point in a scatterplot if both the dependent (Y) and independent (X) variables are averages of some other values. This is my personal note to keep things organized and ease of future use. I’d be happy if anyone is benefitted.

Suppose, we have collected data of biomass and height of some plants and recorded the species names. Now we want to see how biomass is related to plant’s height across species. To do so lets plot the average biomass in Y-axis and average height in X-axis in a scatter plot and fit a linear regression line across the species. We also want to draw error bars for each mean biomass and mean height pair. Here is how we can do:


# Create the dataset
biomass = c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15)
height = c(0.15,.22,.34,.41,.5,.62,.71,.81,.92,1.01,1.1,1.25,1.32,1.41,1.52)
species = c('a','a','a','a','a','b','b','b','b','b','c','c','c','c','c')

dummyData = data.frame(biomass,height,species)
dummyData

library(Rmisc)

# Calculating SE (standard error) which will be used for error bars
bmassStat = summarySE(dummyData,measurevar=”biomass”,groupvars = “species”)
bmassStat
heightStat = summarySE(data=dummyData,measurevar = “height”,groupvars = “species”)
heightStat

# Combining variables and statistics
bmassHt = data.frame(bmassStat$biomass,bmassStat$se,heightStat$height,heightStat$se,heightStat$species)
names(bmassHt)=c(“avgBiomass”,”biomassSE”,”avgHeight”,”heightSE”,”species”)
bmassHt

library(ggplot2)
ggplot(data=bmassHt,aes(x=avgHeight,y=avgBiomass,color=species))+
geom_smooth(method=’lm’,se=FALSE,color=”black”)+
geom_point()+
geom_errorbar(aes(ymax=avgBiomass+biomassSE,ymin=avgBiomass-biomassSE,width=0))+
geom_errorbarh(aes(xmax=avgHeight+heightSE,xmin=avgHeight-heightSE))

# geom_errobarh creates the horizontal error bar for the variable in the X-axis (in this case avgHeight)

So, this is the final graph:
Horizontal & Vertical Error Bars

Please let me know if you have any questions or suggestions.