How do I add group means and SD error bars to raw points in ggplot2?

How do I show each group mean plus or minus one sample SD?

In ggplot2, calculate each group mean and sample SD, then map mean - SD and mean + SD to ymin and ymax in geom_errorbar().

On this page

Published and tested 19 November 2026.

Keep the observations in their own point layer. Use a separate summary table for the mean markers and error bars, with inherit.aes = FALSE on both summary layers. Each summary row produces one mean marker and one error bar. The error-bar reference describes how ymin and ymax specify interval endpoints.

What does the added summary look like?

I read the dots first. Each group has five observations, so I can follow them back to the mean marker before reading the bar as one sample SD on either side.

Before: five synthetic observations in each of groups A and B.
Open full-size image
Before: ten synthetic observations, with response values in arbitrary units. The dots use horizontal jitter only. This article uses simulated example data.
After: the same observations with orange mean diamonds and black bars showing plus or minus one sample SD.
Open full-size image
After: diamonds mark the group means; bars span mean ± 1 sample SD. All ten observation heights and their seeded horizontal positions remain unchanged. Scroll within the figure on a narrow screen.

What numbers should this example produce?

A contains 8, 9, 10, 11, 12; B contains 12, 13, 14, 15, 16. Each group has five observations. The means are 10 and 14. Each sum of squared deviations from its group mean is 10, so each sample SD is sqrt(10 / (5 - 1)), approximately 1.581139. The intervals are approximately 8.418861 to 11.581139 for A and 12.418861 to 15.581139 for B. R's sd documentation specifies the n − 1 denominator.

How do I preserve the raw values and match the groups?

Use the same factor levels in both tables. Map the summary group to x for both the mean and the error bar. The example sets position_jitter(height = 0, seed = 11) for the raw points, preserving their y values and repeating the same horizontal arrangement. The jitter reference documents these controls. Error bars and mean markers remain at the category centres.

These bars describe sample spread using the sample SD. State that definition in the caption; use a suitable separate analysis to test differences between means. The script assumes finite numeric observations and at least two observations per group; review missing values and grouping before applying it to other data.

How do I reproduce the files?

With ggplot2 installed, run Rscript --vanilla example.R in an empty output folder. Tested with R 4.5.3 and ggplot2 4.0.4. The PNG files are 1200 × 900 pixels at approximately 300 PPI; the PDF page is 101.6 × 76.2 mm. These are example dimensions. Check the fonts in the final PDF against the submission requirements.

These are the code, data and result files used in this article. Choose a file below to run the example or compare your results.

  • example.RComplete example code to run on your computer.
  • synthetic.csvThe input data used in the example; open it as a table.
  • summary.csvValues calculated by the example, saved as a table.
  • before.pngThe figure before the change, for comparison.
  • after.pngThe figure after applying the settings in this article.
  • after.pdfThe result in PDF format, for viewing and page layout.

For regression confidence bands, see the separate geom_smooth answer. For sizing, use the pixel calculator.

# Original synthetic values, code and figures: CC0-1.0.
library(ggplot2)
d <- data.frame(group = factor(rep(c("A", "B"), each = 5), levels = c("A", "B")),
                value = c(8, 9, 10, 11, 12, 12, 13, 14, 15, 16))
parts <- split(d$value, d$group)
summary <- data.frame(group = factor(names(parts), levels = levels(d$group)),
                      n = vapply(parts, length, integer(1)),
                      mean = vapply(parts, mean, numeric(1)),
                      sd = vapply(parts, sd, numeric(1)))
summary$lower <- summary$mean - summary$sd
summary$upper <- summary$mean + summary$sd
# Independent hand-check: both groups have sum of squared deviations 10.
stopifnot(all(summary$n == 5), identical(unname(summary$mean), c(10, 14)),
          max(abs(summary$sd - sqrt(10/4))) < 1e-12,
          all(vapply(parts, function(x) sum((x - mean(x))^2), numeric(1)) == 10))
before <- ggplot(d, aes(group, value)) +
  geom_point(position = position_jitter(width = .06, height = 0, seed = 11),
              size = 2, colour = "#17665c") +
  scale_x_discrete(labels = c(A = "A (n = 5)", B = "B (n = 5)")) +
  scale_y_continuous(limits = c(7, 17), breaks = seq(8, 16, 2)) +
  labs(x = "Group", y = "Response (arbitrary units)",
       caption = "Synthetic data; small dots are observations") +
  theme_classic(base_size = 10) +
  theme(plot.caption = element_text(size = 7), plot.margin = margin(7, 8, 7, 7))
after <- before +
  geom_errorbar(data = summary, aes(x = group, ymin = lower, ymax = upper),
                  inherit.aes = FALSE, width = .16, linewidth = .6) +
  geom_point(data = summary, aes(x = group, y = mean), inherit.aes = FALSE,
               shape = 23, size = 3.5, fill = "#D55E00", colour = "black") +
  labs(caption = "Synthetic data; diamonds: means; error bars: +/-1 sample SD")
b <- ggplot_build(before)
a <- ggplot_build(after)
stopifnot(nrow(a$data[[1]]) == 10L,
          identical(a$data[[1]][c("x", "y")], b$data[[1]][c("x", "y")]),
          all(a$data[[1]]$y == d$value),
          max(abs(a$data[[2]]$ymin - summary$lower)) < 1e-12,
          max(abs(a$data[[2]]$ymax - summary$upper)) < 1e-12,
          all(as.numeric(a$data[[2]]$x) == as.numeric(a$data[[3]]$x)))
ggsave("before.png", before, width = 1200, height = 900, units = "px", dpi = 300, bg = "white")
ggsave("after.png", after, width = 1200, height = 900, units = "px", dpi = 300, bg = "white")
ggsave("after.pdf", after, width = 101.6, height = 76.2, units = "mm", bg = "white")
write.csv(d, "synthetic.csv", row.names = FALSE)
write.csv(summary, "summary.csv", row.names = FALSE)
capture.output(sessionInfo(), file = "session-info.txt")
print(summary)
cat("PASS: 10 observations; n=5 in each group; means10/14; sampleSD=sqrt(2.5); y values and seeded x jitter unchanged; interval endpoints and group centres match.\n")

What are the sources and reuse terms?

A Reddit question dated 3 October 2026 prompted this example. The example code and figures were created for this tutorial.

Reddit · 2026-10-03

The original example script, synthetic observations, summary table and PNG/PDF figures are CC0-1.0, including commercial reuse. R retains GPL-2 or GPL-3 and ggplot2 MIT; install the required packages separately to run the code.

CC0-1.0 · Credits