15.1 C
London
Friday, July 5, 2024
HomeTidyverse in Rggplot2 in RHow to Create Side-by-Side Boxplots in R (With Examples)

How to Create Side-by-Side Boxplots in R (With Examples)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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 ggplot2 using the following data frame:

#create data frame
df 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') 

Side-by-side boxplots in base R

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') 

Additional Resources

How to Create a Strip Chart in R
How to Plot Multiple Lines in R
How to Create a Population Pyramid in R

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories