Table of Contents
The process of plotting the mean using the geom_bar() function in ggplot2 involves creating a bar graph that visualizes the average value of a numerical variable. This can be done by first specifying the appropriate data and aesthetics, and then using the stat_summary() function to calculate the mean for each category or group. The resulting bar graph can then be customized and annotated to effectively communicate the central tendency of the data. Overall, using geom_bar() in ggplot2 offers an intuitive and efficient way to display the mean in a visually appealing manner.
Plot Mean with geom_bar() in ggplot2
You can use the following basic syntax to plot the mean values by group using the geom_bar() function in ggplot2:
library(ggplot2) ggplot(df, aes(group_var, values_var)) + geom_bar(position='dodge', stat='summary', fun='mean')
The following example shows how to use this syntax in practice.
Note: The fun argument in geom_bar() tells ggplot2 which descriptive statistic to display using bars. You could also pass a different descriptive statistic such as ‘median’ to this argument to instead plot the median value by group.
Example: Plot Mean Values with geom_bar() in ggplot2
Suppose we have the following data frame that contains information on the number of points scored by basketball players on various teams:
#create data frame df <- data.frame(team=rep(c('A', 'B', 'C'), each=4), points=c(3, 5, 5, 6, 5, 7, 7, 8, 9, 9, 9, 8)) #view data frame df team points 1 A 3 2 A 5 3 A 5 4 A 6 5 B 5 6 B 7 7 B 7 8 B 8 9 C 9 10 C 9 11 C 9 12 C 8
We can use the following syntax to create a bar chart in which each bar represents the mean value for points, grouped by team:
library(ggplot2) #create bar plot to visualize mean points value by team ggplot(df, aes(team, points)) + geom_bar(position='dodge', stat='summary', fun='mean')

The height of each bar represents the mean value of points for each team.
To display the actual mean value of points for each team, we can use the summarise() function from the dplyr package:
library(dplyr) #calcualte mean value of points, grouped by team df %>% group_by(team) %>% summarise(mean_pts = mean(points, na.rm=TRUE)) # A tibble: 3 x 2 team mean_pts 1 A 4.75 2 B 6.75 3 C 8.75
From the output we can see the mean value of points for each team:
- Team A: 4.75
- Team B: 6.75
- Team C: 8.75
These values match the height of the bars shown in the bar plot above.
Cite this article
stats writer (2024). How can I plot the mean with geom_bar() in ggplot2?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-plot-the-mean-with-geom_bar-in-ggplot2/
stats writer. "How can I plot the mean with geom_bar() in ggplot2?." PSYCHOLOGICAL SCALES, 25 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-plot-the-mean-with-geom_bar-in-ggplot2/.
stats writer. "How can I plot the mean with geom_bar() in ggplot2?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-plot-the-mean-with-geom_bar-in-ggplot2/.
stats writer (2024) 'How can I plot the mean with geom_bar() in ggplot2?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-plot-the-mean-with-geom_bar-in-ggplot2/.
[1] stats writer, "How can I plot the mean with geom_bar() in ggplot2?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I plot the mean with geom_bar() in ggplot2?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
