Clustered Bar Plot with Multiple Groups
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), Group3 = c (5, 9, 6, 11) ) # Create a clustered bar plot with multiple groups 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" , width = 0.7) + geom_bar ( aes (y = Group3, fill = "Group 3" ), stat = "identity" , position = "dodge" , width = 0.5) + labs (title = "Clustered Bar Plot with Multiple Groups" , y = "Value" ) + scale_fill_manual (values = c ( "Group 1" = "blue" , "Group 2" = "green" , "Group 3" = "red" )) + theme_minimal () |
Output:
In this example, we create a clustered bar plot with three groups (Group 1, Group 2, and Group 3) for each category. The bars for each group are clustered together using position = “dodge”, and we adjust the width of the bars for better visibility. Different fill colors are used to distinguish the groups.
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