Open In App

Stacked Bar Chart in R ggplot2

Last Updated : 30 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A stacked bar plot displays data using rectangular bars grouped by categories. Each group represents a category, and inside each group, different subcategories are stacked on top of each other. It also shows relationships between groups and subcategories, making them useful for various data analysis tasks.

For example, imagine an ice cream shop., and the owner wants to see which ice cream flavours sold better in July and August:

  • In July, Vanilla sold 200 cones, and Chocolate sold 180 cones.
  • In August, Vanilla sold 220 cones, and Chocolate sold 190 cones.

A stacked bar chart will be useful to compare the sales across months and see that Vanilla was more popular both times.

Creating a Stacked Bar Plot in R

We will now see how to create a stacked bar graph in R using ggplot2 package.

1. Install and Load Required Library

We are going to install and load the ggplot2 library.

R
install.packages("ggplot2")
library(ggplot2)

2. Create Sample Data

We are going to set a random seed so that the randomly generated data stays the same every time you run the code. Then we randomly assign one of the four age groups ("Child", "Adult", "Working", "Retired") to 50 individuals and a random number of hours (between 1 and 4) to each individual. We will do the same for the city , assigning one of four ("Noida", "Delhi", "Jaipur", "Udaipur") .

R
set.seed(50)

age <- factor(sample(c("Child", "Adult", "Working", "Retired"),
                     size = 50, replace = TRUE),
              levels = c("Child", "Adult", "Working", "Retired"))
              
hours <- sample(1:4, size = 50, replace = TRUE)

city <- sample(c("Noida", "Delhi", "Jaipur", "Udaipur"),
               size = 50, replace = TRUE)

3. Combine the Data into a Data Frame

We are going to combine age, hours, and city into one single data frame for easier plotting.

R
df <- data.frame(x = age, y = hours, group = city)

4. Create the Stacked Bar Plot

We are going to use ggplot2 to create a stacked bar plot, where bars represent age groups and are filled according to city.

R
library(ggplot2)
ggplot(df, aes(x = x, fill = group)) + 
  geom_bar()


Output:

stacked-bar-chart
Stacked Bar Chart

5. Changing Axis Labels

We can customize the x and y axis labels to make them more descriptive using labs().

R
ggplot(df, aes(x = x, fill = group)) + 
  geom_bar() + 
  labs(x = "Age Groups", y = "Number of People", title = "Age Group Distribution Across Cities")

Output:

labeled
Labled the axes

6. Using Different Colour Palette

The scale_fill_brewer() function is used to apply a color scheme we like to the bars. The type = "seq" is used to choose a sequential color style, where colors gradually move from light to dark. The palette = "Greens" is used to pick the green color palette from the available options.

R
ggplot(df, aes(x = x, fill = group)) + 
  geom_bar() +
  labs(x = "Age Groups", y = "Number of People", title = "Age Group Distribution Across Cities") +
  scale_fill_brewer(type = "seq", palette = "Greens") 

Output:

greens
Using Different Colours

7. Creating a Horizontal Stacked Bar Plot

We are creating a horizontal stacked bar plot by using coord_flip(), which swaps the x and y axes. This rotates the plot so that the categories appear on the y-axis, and the bars extend horizontally.

R
ggplot(df, aes(x = x, fill = group)) + 
  geom_bar() + 
  scale_fill_brewer(type = "seq", palette = 5) + 
  coord_flip()

Output:

transpose
Horizontal Stacked Bar Chart

Advantages of Stacked Bar Charts

  1. Easy Comparison: Compare different items within groups quickly.
  2. Clear Visualization: Presents data in a simpler way.
  3. Spotting Trends: Helps identify patterns and trends.
  4. Flexible Data Use: Works well with many types of data.
  5. Customizable: Can be styled for better clarity or appearance.
  6. Good for Beginners: Useful for explaining data to those less familiar with it.

Disadvantages of Stacked Bar Charts

  1. Can Get Messy: Too many groups or items can make it confusing.
  2. Space Limitations: Too many categories can shrink the bars, making them hard to read.
  3. Category-Only Use: Works only with categorical data, not continuous scales.
  4. Label Overlap: Long or numerous labels might overlap and reduce readability.
  5. Comparison Between Groups Is Harder: Comparing values across groups can be tricky.
  6. Risk of Misinterpretation: Poor labeling or scaling can confuse viewers.

In this article, we will learned how to create a clustered bar plot in R Programming Language and saw its advantages and disadvantages as well.


Next Article

Similar Reads