Reputation: 461
I want to put confidence interval error bars for ggplot.
I have a dataset and I am plotting it with ggplot as:
df <- data.frame(
Sample=c("Sample1", "Sample2", "Sample3", "Sample4", "Sample5"),
Weight=c(10.5, NA, 4.9, 7.8, 6.9))
p <- ggplot(data=df, aes(x=Sample, y=Weight)) +
geom_bar(stat="identity", fill="black") +
scale_y_continuous(expand = c(0,0), limits = c(0, 8)) +
theme_classic() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)
p
I am new to adding error bars. I looked at some options using geom_bar but I could not make it work.
I will appreciate any help to put confidence interval error bars in the barplot. Thank you!
Upvotes: 5
Views: 9863
Reputation: 497
It is possible to generate error bars automatically with a tool called superb
(summary plot with error bars).
First, lets have more than one point per sample using random data
df2 <- data.frame(
Sample = rep(c("Sample1", "Sample2", "Sample3", "Sample4", "Sample5"),5),
Weight = rep(c(10.5, NA, 4.9, 7.8, 6.9),5)+rnorm(25)
)
Then load the library and ask for the plot (default will be to show 95% confidence intervals):
library(superb)
superb( Weight ~ Sample, df2)
You can ask for standard error (SE) rather than the default confidence intervals (CI) and add any additional formatting options, for example:
p <- superb( Weight ~ Sample, df2,
errorbar = "SE" # or CI for confidence intervals
) +
scale_y_continuous(expand = c(0,0), limits = c(0, 12)) +
theme_classic() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
p
Note that I am the creator of superb
.
Upvotes: 0
Reputation: 1378
Add a layer of error bars with geom_errorbar
df <- data.frame(
Sample=c("Sample1", "Sample2", "Sample3", "Sample4", "Sample5"),
Average.Weight=c(10.5, NA, 4.9, 7.8, 6.9),
# arbitrarily make up some Standard Errors for each mean:
SE = c(1, NA, .3, .25, .2)) # JUST MAKING THINGS UP HERE
Now you have a data frame with a column of Average Weight
and the SE
for each sample in your study. Use ggplot
to plot:
ggplot(data = na.omit(df)) + #don't bother plotting the NA
geom_bar(stat = "identity", aes(x = Sample,y = Average.Weight)) +
geom_errorbar(
aes(x=Sample,
ymin = Average.Weight - 1.96*SE,
ymax = Average.Weight + 1.96*SE),
color = "red"
)
Upvotes: 7