Solved – Calculating standard error and attaching an error bar on ggplot2 bar chart

barplotggplot2r

Given a minimal dataset where am looking for the occurrence of a certain motif within a dataset of 500 observations. with_motif represents obervations with the specified motif and without_motif are observations without the motif.

with_motif <- 100
without_motif <- 400
dt <- data.frame(with_motif,without_motif)

The following code will plot a bar-chart using ggplot2 library,

bar_plot <- ggplot(melt(dt),aes(variable,value)) + geom_bar() + scale_x_discrete(name="with or without") + theme_bw() + opts( panel.grid.major = theme_blank(),title = "", plot.title=theme_text(size=14))

bar_plot

I would like to compute a standard error at 95% CI and attach a barchart to the plot. ggplot offers geom_errorbar() but I would be glad to know different ways for deriving the standard errors(deviation) so as to calculate the errorbar limits(CI).

Best Answer

Here's an example from the ggplot2 homepage: https://ggplot2.tidyverse.org/reference/geom_errorbarh.html as others have mentioned in the comments, you have to calculate SE on your own and append this information to the data.frame

df <- data.frame( 
  trt = factor(c(1, 1, 2, 2)), 
  resp = c(1, 5, 3, 4), 
  group = factor(c(1, 2, 1, 2)), 
  se = c(0.1, 0.3, 0.3, 0.2) 
 ) 
df2 <- df[c(1,3),]
p <- ggplot(df, aes(fill=group, y=resp, x=trt)) 
p + geom_bar(position="dodge", stat="identity") 
dodge <- position_dodge(width=0.9) 
p + geom_bar(position=dodge, stat="identity") + geom_errorbar(aes(ymax = resp + se, ymin=resp - se), position=dodge, width=0.25)  

As a pointer, SE @ 95% CI usually looks something like this:

df$se <- 1.96*(sd(your_data, na.rm=T)/sqrt(your_n))

Your upper and lower CI bounds will just be df$se +/- the response (as shown in the aes() for geom_errorbar(), above)

Related Question