[R] How to create errorbars with overlaid points using ggplot

R-bloggers 2025-04-26

[This article was first published on R on Zhenguo Zhang's Blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Zhenguo Zhang’s Blog /2025/04/26/r-how-to-create-an-error-barplot-with-overlaid-points-using-ggplot/ –
library(ggplot2)library(dplyr)

Sometimes you may want to create a plot with the following features:

  • a point to indicate the mean of a group
  • error bars to indicate the standard deviation of the group
  • and each group may have subgroups, which are represented by different colors.

In this post, I will show you how to create such a plot using the ggplot2 package in R.

We will use the builtin mtcars dataset as an example. And we need tocompute the following variables for later use:

  • The mean mpg for each group of cyl (number of cylinders) and gear`` (number of gears), herecylis the main group andgear` is the subgroup.
# Load the mtcars datasetdata(mtcars)# Compute the mean and standard deviation of mpg for each groupmtcars_summary <- mtcars %>%  group_by(cyl, gear) %>%  summarise(mean_mpg = mean(mpg), sd_mpg = sd(mpg)) %>%  ungroup()# replace the NA values in sd_mpg with 1mtcars_summary$sd_mpg[is.na(mtcars_summary$sd_mpg)] <- 1# convert group variables into factorsmtcars_summary$cyl <- factor(mtcars_summary$cyl)mtcars_summary$gear <- factor(mtcars_summary$gear)

Create the plot - first try

Now we can create the plot using ggplot2. We will use the geom_point() function to create the points, and the geom_errorbar() function to create the error bars. We will also use the aes() function to specify the aesthetics of the plot.

# Create the plotplt <- ggplot(mtcars_summary, aes(x = cyl, y = mean_mpg, color = gear)) +  geom_point(size = 3) + # add points  geom_errorbar(aes(ymin = mean_mpg - sd_mpg, ymax = mean_mpg + sd_mpg), width = 0.2) + # add error bars  labs(x = "Number of Cylinders", y = "Mean MPG", color = "Number of Gears") + # add labels  theme_minimal() + # use a minimal theme  theme(legend.position = "top") # move the legend to the topplt

Well, it is working, but the problem is that the error bars and points are allaligned at the same position of x-axis. This is not what we want. We want thesubgroups to be separated by a small distance.

Create the plot - second try

To separate the subgroups, we can use the position_dodge() function. This function will move the points and error bars to the left and right, so that they are not overlapping.

pd <- position_dodge(width = 0.5)# Create the plot with position_dodgeplt <- ggplot(mtcars_summary, aes(x = cyl, y = mean_mpg, color = gear)) +  geom_point(size = 3, position = pd) + # add points with position_dodge  geom_errorbar(aes(ymin = mean_mpg - sd_mpg, ymax = mean_mpg + sd_mpg), width = 0.2, position = pd) + # add error bars with position_dodge  labs(x = "Number of Cylinders", y = "Mean MPG", color = "Number of Gears") + # add labels  theme_minimal() + # use a minimal theme  theme(legend.position = "top") # move the legend to the topplt

Cool. Isn’t it?

The only difference is that we added the position = pd argument to the geom_point() and geom_errorbar() functions. This tells ggplot2 to use the position_dodge() function to separate the subgroups.

Conclusion

In this post, we learned how to create a plot with error bars and overlaid points using the ggplot2 package in R. We also learned how to separate the subgroups using the position_dodge() function.

If you want to learn more about the function position_dodge(), you can check anexcellent post here.

Happy programming! 😃

- /2025/04/26/r-how-to-create-an-error-barplot-with-overlaid-points-using-ggplot/ -
To leave a comment for the author, please follow the link and comment on their blog: R on Zhenguo Zhang's Blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Continue reading: [R] How to create errorbars with overlaid points using ggplot