ggplot2 geom_errorbar() in R: Add Error Bars to Plots
The geom_errorbar() function in ggplot2 draws vertical error bars from ymin to ymax at each x position. It is the standard tool for showing uncertainty on bar charts, point plots, and line plots.
ggplot(df, aes(x, y, ymin = lo, ymax = hi)) + geom_errorbar() geom_errorbar(width = 0.2) # narrower bars ggplot(df, aes(x, y)) + geom_col() + geom_errorbar(aes(ymin = y - sd, ymax = y + sd)) geom_pointrange(aes(ymin, ymax)) # alternative: point + range geom_ribbon(aes(ymin, ymax)) # for continuous bands
Need explanation? Read on for examples and pitfalls.
What geom_errorbar() does in one sentence
geom_errorbar() draws vertical bars from ymin to ymax at each x, with horizontal whiskers at the top and bottom. Used for confidence intervals, standard errors, or any uncertainty interval per point.
Syntax
geom_errorbar(mapping = NULL, data = NULL, width = 0.5, ...). Requires aes(x, ymin, ymax).
width = 0.2 for cleaner look. For continuous x, set width to a small value like 0.1.Five common patterns
1. On bar chart
2. On point plot
3. Stat_summary auto-error
4. Color and dodge
5. Confidence intervals
geom_errorbar requires ymin and ymax aesthetics. Default width is too wide for most plots; set 0.2 or smaller for cleaner appearance.geom_errorbar() vs geom_pointrange() vs geom_ribbon()
| Function | Visual | Best for |
|---|---|---|
geom_errorbar() |
Bar with whiskers | Discrete x with error |
geom_pointrange() |
Point + range bar | Cleaner combination |
geom_ribbon() |
Continuous band | Time-series uncertainty |
geom_linerange() |
Line only (no point) | Sparse error display |
A practical workflow
Use stat_summary for "compute mean and error from raw data" in one step.
Raw points + mean + standard error in one plot.
Common pitfalls
Pitfall 1: forgetting width. Default 0.5 looks ugly on most plots. Set width = 0.2.
Pitfall 2: wrong error type. SD is per-observation spread. SE is mean's uncertainty. CI is interval. Pick what your data shows.
geom_errorbar is for VERTICAL bars only; use geom_errorbarh() for HORIZONTAL. They have different aesthetics (xmin/xmax for h).Try it yourself
Try it: Make a bar chart of mean mpg per cyl with error bars showing +/- standard error. Save to ex_plot.
Click to reveal solution
Explanation: Compute mean and SE per cyl; plot as bar with error bars.
Related ggplot2 functions
After mastering geom_errorbar, look at:
geom_pointrange(): point + rangegeom_linerange(): line onlygeom_ribbon(): continuous bandsgeom_errorbarh(): horizontal barsstat_summary(): compute error from raw datamean_sdl(),mean_se(),mean_cl_normal(): stat helpers
FAQ
What does geom_errorbar do in ggplot2?
geom_errorbar() draws vertical bars from ymin to ymax at each x position with horizontal whiskers at the ends.
How do I make error bars narrower?
Set width: geom_errorbar(width = 0.2). Default is 0.5 (often too wide).
What is the difference between geom_errorbar and geom_pointrange?
errorbar has horizontal whiskers; pointrange combines a central point with a range bar (no whiskers).
How do I make horizontal error bars?
Use geom_errorbarh() with xmin and xmax. Or swap the aesthetic mapping: aes(y, x, ...).
Should I show SD, SE, or CI as error bars?
SD shows individual variation. SE shows mean uncertainty. CI is interval estimate. Pick by what you want the reader to interpret.