Often you may want to plot the mean and standard deviation by group in ggplot2.
Fortunately this is easy to do using the geom_point() and geom_errorbar() functions in ggplot2.
The following example shows how to use these functions to create the following plot that shows the mean and standard deviation of points scored by various basketball teams:
Example: Plot Mean and Standard Deviation in ggplot2
Suppose we have the following data frame in R that contains information on the number of points scored by basketball players on three different teams:
#create data frame df <- data.frame(team=rep(c('A', 'B', 'C'), each=6), points=c(8, 10, 12, 12, 14, 15, 10, 11, 12, 18, 22, 24, 3, 5, 5, 6, 7, 9)) #view head of data frame head(df) team points 1 A 8 2 A 10 3 A 12 4 A 12 5 A 14 6 A 15
We can use functions from the dplyr package to quickly calculate the mean and standard deviation of points scored by players on each team:
library(dplyr) #calculate mean and sd of points by team df_mean_std <- df %>% group_by(team) %>% summarise_at(vars(points), list(mean=mean, sd=sd)) %>% as.data.frame() #view results df_mean_std team mean sd 1 A 11.833333 2.562551 2 B 16.166667 6.013873 3 C 5.833333 2.041241
Lastly, we can use the following functions from ggplot2 to visualize the mean and standard deviation of points scored by players on each team:
library(ggplot2) #plot mean and standard deviation of points by team ggplot(df_mean_std , aes(x=team, y=mean)) + geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), width=.3) + geom_point(size=2)
The resulting plot shows the mean and standard deviation of points scored by players on each team.
The circles represent the mean values and the length of the bars above and below each circle represent the standard deviation.
Note: The width argument in the geom_errorbar() function specifies how wide the error bars should be. Feel free to modify this value to adjust the width of the error bars in the plot.
The following tutorials explain how to perform other common tasks in ggplot2: