4.5 C
London
Thursday, December 19, 2024
HomeStatistics TutorialRHow to Create a Grouped Barplot in R (With Examples)

How to Create a Grouped Barplot 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...

A grouped barplot is a type of chart that displays quantities for different variables, grouped by another variable.

This tutorial explains how to create grouped barplots in R using the data visualization library ggplot2.

Grouped Barplot in ggplot2

Suppose we have the following data frame that displays the average points scored per game for nine basketball players:

#create data frame
df rep(c('A', 'B', 'C'), each=3),
                 position=rep(c('Guard', 'Forward', 'Center'), times=3),
                 points=c(14, 8, 8, 16, 3, 7, 17, 22, 26))

#view data frame
df

  team position points
1    A    Guard     14
2    A  Forward      8
3    A   Center      8
4    B    Guard     16
5    B  Forward      3
6    B   Center      7
7    C    Guard     17
8    C  Forward     22
9    C   Center     26

We can use the following code to create a grouped barplot that displays the points scored by each player, grouped by team and position:

library(ggplot2)

ggplot(df, aes(fill=position, y=points, x=team)) + 
  geom_bar(position='dodge', stat='identity')

Grouped barplot in R

Customizing a Grouped Barplot

We can also customize the title, axes labels, theme, and colors of the grouped barplot to make it look however we’d like:

library(ggplot2)

ggplot(df, aes(fill=position, y=points, x=team)) + 
  geom_bar(position='dodge', stat='identity') +
  theme_minimal() + 
  labs(x='Team', y='Points', title='Avg. Points Scored by Position & Team') +
  theme(plot.title = element_text(hjust=0.5, size=20, face='bold')) +
  scale_fill_manual('Position', values=c('coral2', 'steelblue', 'pink'))

Grouped barplot in R with ggplot2

We can customize the appearance even more by using one of the themes in the ggthemes library. For example, we could use the Wall Street Journal Theme from this library:

install.packages('ggthemes')

library(ggplot2)
library(ggthemes)

ggplot(df, aes(fill=position, y=points, x=team)) + 
  geom_bar(position='dodge', stat='identity') +
  theme_wsj()

Grouped barplot in R with ggthemes

Refer to our Complete Guide to the Best ggplot2 Themes for even more themes.

Additional Resources

How to Create a Stacked Barplot in R
How to Create a Grouped Boxplot in R Using ggplot2
How to Create Side-by-Side Plots in ggplot2

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