faceted bar chart in r

# Step 1: Install and load required packages
install.packages("ggplot2")
library(ggplot2)

# Step 2: Create a sample dataset
data <- data.frame(
  Category = rep(c("A", "B", "C"), each = 4),
  Subcategory = rep(c("X", "Y"), each = 2, times = 2),
  Value = c(3, 5, 2, 8, 6, 7, 1, 4, 9, 2, 5, 7)
)

# Step 3: Create the faceted bar chart using ggplot2
plot <- ggplot(data, aes(x = Subcategory, y = Value, fill = Subcategory)) +
  geom_bar(stat = "identity", position = "dodge") +
  facet_wrap(~ Category, scales = "free_y", ncol = 1) +
  labs(title = "Faceted Bar Chart",
       x = "Subcategory",
       y = "Value") +
  theme_minimal()

# Step 4: Display the plot
print(plot)