Simple Clustered 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 () |
Output:
In this example, we create a basic clustered bar plot using ggplot2. We have two groups (Group 1 and Group 2) with four categories (A, B, C, and D). The geom_bar function is used to plot bars for each group, and position = “dodge” ensures that bars for the same category are clustered together. We specify colors for the two groups using scale_fill_manual, and add labels and a title to the plot.
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