I have the dummy dataset df
:
df <- data.frame(
Product = c(755, 728, 417, 355, 913, 634, 385, 208, 204, 696, 968, 816, 279, 869, 823, 646, 986, 674, 806, 335, 731, 734, 107, 105, 512, 859, 159, 113, 353, 105, 205, 919, 243, 717, 838, 408, 423, 357, 408, 464, 724, 643, 943, 648, 623, 451, 449, 135, 842, 711),
Category = c("Bread", "Cheese", "Bread", "Cheese", "Bread", "Cheese", "Bread", "Bread", "Bread", "Cheese", "Bread", "Bread", "Cheese", "Bread", "Cheese", "Bread", "Bread", "Bread", "Cheese", "Cheese", "Cheese", "Bread", "Bread", "Cheese", "Bread", "Cheese", "Bread", "Bread", "Bread", "Cheese", "Cheese", "Bread", "Bread", "Cheese", "Cheese", "Bread", "Bread", "Bread", "Bread", "Cheese", "Cheese", "Cheese", "Cheese", "Cheese", "Cheese", "Cheese", "Cheese", "Bread", "Bread", "Cheese"),
Sub_Category = c("White", "Camembert", "White", "Camembert", "White", "Brie", "Brown", "Wholemeal", "White", "Brie", "Wholemeal", "Wholemeal", "Gouda", "Wholemeal", "Camembert", "Brown", "White", "Wholemeal", "Gouda", "Gouda", "Brie", "Wholemeal", "Wholemeal", "Camembert", "White", "Brie", "Wholemeal", "Wholemeal", "Brown", "Brie", "Brie", "White", "Wholemeal", "Camembert", "Gouda", "White", "Wholemeal", "Brown", "White", "Camembert", "Brie", "Brie", "Camembert", "Brie", "Camembert", "Gouda", "Camembert", "Wholemeal", "White", "Camembert"),
Fibre = c(TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, TRUE, TRUE)
)
With this data, I want to create a grouped bar chart that plots the proportions of products containing fibre by sub-category and grouped by category.
I can create a simple plot that shows the proportions of products containing fibre by category:
proportions <- df %>%
group_by(Category) %>%
summarise(Proportion = mean(Fibre == TRUE) * 100)
ggplot(proportions, aes(x = Proportion, y = reorder(Category, Proportion), fill = Category)) +
geom_bar(stat = "identity", fill = "blue")
But what I would really like to show is a plot with six bars representing the proportions of products containing fibre for each of the sub-categories (not the main categories) and grouped together (by colour/legend) by the main category.
How can I create a grouped bar chart showing this in R?