# Calculating standard error and attaching an error bar on ggplot2 bar chart

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

• +1, but kindly avoid "plot" as an object name! Aug 11, 2011 at 10:36
• yep! edited and nice advice! Aug 11, 2011 at 10:38
• @Biorelated There's a very good online help for ggplot2, e.g. had.co.nz/ggplot2/geom_errorbar.html. I also provided some example of use of geom_errorbar() in this response. Is this what you're after?
– chl
Aug 11, 2011 at 10:50
• @Biorelated As can be seen in my response, you'll need to compute SD or SE or 95% CI yourself, and add them to your data frame (or use another one), then call geom_errorbar(). If your question is about how to compute those estimates, then you may consider updating your question and provide more information on your dataset.
– chl
Aug 11, 2011 at 11:21
• @Biorelated I don't quite follow your update. To compute any measure of precision of a given estimate, we need to know what distribution it follows, right? So, what do with and without stand for in your code?
– chl
Aug 11, 2011 at 11:54

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)

• When you forget how to do something, google it, and find your own answer 7 years later. May 22, 2018 at 17:01