[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 to
compute 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 dataset
data(mtcars)
# Compute the mean and standard deviation of mpg for each group
mtcars_summary <- mtcars %>%
  group_by(cyl, gear) %>%
  summarise(mean_mpg = mean(mpg), sd_mpg = sd(mpg)) %>%
  ungroup()
# replace the NA values in sd_mpg with 1
mtcars_summary$sd_mpg[is.na(mtcars_summary$sd_mpg)] <- 1
# convert group variables into factors
mtcars_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 plot
plt <- 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 top
plt

Well, it is working, but the problem is that the error bars and points are all
aligned at the same position of x-axis. This is not what we want. We want the
subgroups 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_dodge
plt <- 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 top
plt

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 an
excellent 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

Long-Term Implications and Future Developments

The blog post by Zhenguo Zhang provides a well-detailed guide on how to create a plot chart using the ggplot2 package in R with overlaid points and error bars. This skill is increasingly essential in the data analysis field, especially as organizations delve more into data-driven decision making. As a developer or data analyst, mastering the use of ggplot2 for data visualization not only increases efficiency but also the clarity of your data reports.

Possibility of Increased use of ggplot2

With the continual growth of data analysis in almost all sectors, we can expect that more persons will rely on ggplot2 for their data visualization needs. Its ability to create complex and detailed plots with simple code lines makes it a powerful tool for data analysis.

The Need for Improved Visualization Tools

The use of overlaid points and error bars as shown by Zhenguo Zhang is an essential technique in data visualization. However, there is a need to simplify this process and make it more user-friendly for people without programming skills. We can then expect future developments to focus on improving user experience by introducing new functions or tools that make data visualization easier.

Actionable Advice

For individuals dealing with R and data visualization, here are some tips:

  • Enhance Your R skills: Increasing your knowledge on R and its associated data visualization packages, particularly ggplot2, will prove invaluable in professional data analysis.
  • Constant learning: ggplot2 is constantly being updated with new features and functionalities. Therefore, continuously updating your knowledge and skills on the package will keep you ready and equipped to handle any changes that may arise.
  • Engage the R community: Participating in R-bloggers and other similar communities can provide you with a platform to not only share but also learn from others.
  • Explore other visualization tools: While ggplot2 is quite powerful, other packages may be better suited for specific kind of data visualizations. Be open to learning and using other visualization tools.

Remember: The key in today’s data analysis field does not lie in simply analyzing and reporting data, but presenting it in a way that is easy to understand.

Read the original article