Table of Contents
To create side-by-side boxplots in R, you can use the built-in function “boxplot()” with the argument “formula” specifying the variables you want to compare. This will generate multiple boxplots, one for each variable, organized side-by-side for easy comparison. For example, if you have a dataset containing the heights of males and females, you can use the syntax “boxplot(height ~ gender)” to create side-by-side boxplots showing the distribution of heights for each gender. You can also customize the appearance of the boxplots by specifying different color schemes, adding labels and titles, and adjusting the scale of the axes. Side-by-side boxplots are useful for visually comparing the distribution of multiple variables and identifying any differences or similarities between them.
Create Side-by-Side Boxplots in R (With Examples)
Side-by-side boxplots can be used to quickly visualize the similarities and differences between different distributions.
This tutorial explains how to create side-by-side boxplots in both base R and using the following data frame:
#create data frame df <- data.frame(team=rep(c('A', 'B', 'C'), each=8), points=c(5, 5, 6, 6, 8, 9, 13, 15, 11, 11, 12, 14, 15, 19, 22, 24, 19, 23, 23, 23, 24, 26, 29, 33)) #view first 10 rows head(df, 10) team points 1 A 5 2 A 5 3 A 6 4 A 6 5 A 8 6 A 9 7 A 13 8 A 15 9 B 11 10 B 11
Side-by-Side Boxplots in Base R
The following code shows how to create side-by-side boxplots in base R:
#create vertical side-by-side boxplots boxplot(df$points ~ df$team, col='steelblue', main='Points by Team', xlab='Team', ylab='Points')

We can use the argument horizontal=TRUE to display the boxplots horizontally instead of vertically:
#create horizontal side-by-side boxplots boxplot(df$points ~ df$team, col='steelblue', main='Points by Team', xlab='Points', ylab='Team', horizontal=TRUE)

Side-by-Side Boxplots in ggplot2
The following code shows how to create vertical side-by-side boxplots in ggplot2:
library(ggplot2) #create vertical side-by-side boxplots ggplot(df, aes(x=team, y=points, fill=team)) + geom_boxplot() + ggtitle('Points by Team')

And we can use the coord_flip() argument to display the boxplots horizontally instead of vertically:
library(ggplot2) #create horizontal side-by-side boxplots ggplot(df, aes(x=team, y=points, fill=team)) + geom_boxplot() + coord_flip() + ggtitle('Points by Team')

Cite this article
stats writer (2024). How can I create side-by-side boxplots in R with examples?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-create-side-by-side-boxplots-in-r-with-examples/
stats writer. "How can I create side-by-side boxplots in R with examples?." PSYCHOLOGICAL SCALES, 2 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-create-side-by-side-boxplots-in-r-with-examples/.
stats writer. "How can I create side-by-side boxplots in R with examples?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-create-side-by-side-boxplots-in-r-with-examples/.
stats writer (2024) 'How can I create side-by-side boxplots in R with examples?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-create-side-by-side-boxplots-in-r-with-examples/.
[1] stats writer, "How can I create side-by-side boxplots in R with examples?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.
stats writer. How can I create side-by-side boxplots in R with examples?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
