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:
- 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