Grouped Stacked Bar Plot

R




# Load the ggplot2 package
library(ggplot2)
 
# Sample data
sales_data <- data.frame(
  Category = c("Electronics", "Clothing", "Books", "Furniture"),
  North = c(250, 180, 90, 450),
  South = c(220, 150, 140, 310),
  West = c(280, 100, 100, 380)
)
 
# Create a visually appealing grouped stacked bar plot
ggplot(sales_data, aes(x = Category)) +
  geom_bar(aes(y = North, fill = "North"), stat = "identity") +
  geom_bar(aes(y = South, fill = "South"), stat = "identity") +
  geom_bar(aes(y = West, fill = "West"), stat = "identity") +
  labs(
    title = "Sales by Product Category and Region",
    y = "Total Sales",
    fill = "Region"
  ) +
  scale_fill_manual(
    values = c("North" = "#1f77b4", "South" = "#ff7f0e", "West" = "#2ca02c"),
    labels = c("North Region", "South Region", "West Region")
  ) +
  theme_minimal() +
  theme(legend.position = "top")


Output:

Sales by Product Category and Region

  • In this example, we use sales data for four product categories (Electronics, Clothing, Books, Furniture) across three regions (North, South, West). We create a grouped stacked bar plot to visualize how sales are divided within each category and region.
  • Different fill colors (blue, orange, green) are used to represent the three regions (North, South, West).
  • The bars are stacked for each product category, showing the total sales for each category.
  • The legend is positioned at the top for better readability.
  • Custom fill colors and labels are specified using scale_fill_manual for improved aesthetics.

Clustered Bar Plot in R

One of the most popular packages for data visualisation is ggplot2, which can be used to create a clustered bar plot in R.

Table of Content

  • Clustered Bar Plot
  • Simple Clustered Bar Plot
  • Grouped Stacked Bar Plot
  • Clustered Bar Plot with Multiple Groups
  • Clustered Bar Plot in R using Plotly
  • Conclusion

Similar Reads

Clustered Bar Plot

...

Simple Clustered Bar Plot

A clustered bar chart is a horizontal chart, which could present multiple bars in the form of a cluster. The horizontal bars basically group together, as they are under the same, y values. We have various options to format clustered bar charts, we can change the value of the x-axis, y-axis, its title, etc. In this article, we will learn how to format a clustered bar chart in Power BI and explore its various options....

Grouped Stacked Bar Plot

R # Load the ggplot2 package library(ggplot2)   # Sample data data <- data.frame(   Category = c("A", "B", "C", "D"),   Group1 = c(10, 15, 7, 12),   Group2 = c(8, 11, 9, 14) )   # Create a simple clustered bar plot ggplot(data, aes(x = Category)) +   geom_bar(aes(y = Group1, fill = "Group 1"), stat = "identity", position = "dodge") +   geom_bar(aes(y = Group2, fill = "Group 2"), stat = "identity", position = "dodge") +   labs(title = "Simple Clustered Bar Plot", y = "Value") +   scale_fill_manual(values = c("Group 1" = "blue", "Group 2" = "red")) +   theme_minimal()...

Clustered Bar Plot with Multiple Groups

...

Clustered Bar Plot in R using Plotly

R # Load the ggplot2 package library(ggplot2)   # Sample data sales_data <- data.frame(   Category = c("Electronics", "Clothing", "Books", "Furniture"),   North = c(250, 180, 90, 450),   South = c(220, 150, 140, 310),   West = c(280, 100, 100, 380) )   # Create a visually appealing grouped stacked bar plot ggplot(sales_data, aes(x = Category)) +   geom_bar(aes(y = North, fill = "North"), stat = "identity") +   geom_bar(aes(y = South, fill = "South"), stat = "identity") +   geom_bar(aes(y = West, fill = "West"), stat = "identity") +   labs(     title = "Sales by Product Category and Region",     y = "Total Sales",     fill = "Region"   ) +   scale_fill_manual(     values = c("North" = "#1f77b4", "South" = "#ff7f0e", "West" = "#2ca02c"),     labels = c("North Region", "South Region", "West Region")   ) +   theme_minimal() +   theme(legend.position = "top")...

Conclusion

...