I have a data like this:
df <- data.frame(
groups = c(rep("A", 5),
rep("B", 3),
rep("C", 2),
rep(c("D","E","F","G","H"), 1)),
subgroups = paste0("Subgroup", 1:15),
length = c(103,112,141,152,50,
77,82,88,
59,86,
3,17,1,5,24))
I want to plot a stacked bar graph for lengths of the groups. For each group, the height of the bar should show the percentage of that group among all groups calculated using lengths; and the bar should be divided showing the percentage of the subgroups in that group. But the thing is, in the original data the lengths are in millions, so i want to combine some groups to create an "Other" group, and their bar should be sectioned to show the percentage of the classes in that group. so I will have 5 columns for A,B,C,D,Other: The bars of first 4 should reflect the percentage of lengths of subgroups and the "Other" bar should reflect the percentage of lengths of groups that were combined.
I did a bit of data wrangling using dplyr to make new columns showing the length percentages, so in a new column I have the group_length_percent for each group and subgroup_length_percent. However I still could not figure out how to plot because when I try to plot using ggplot, it plots the sum of the percentages so the bars are more than 100% or the bar for the Other group is divided equally to the number of groups combined, does not reflect the lengths of the classes. I feel confused and not sure how to proceed.
Thank you for your responses.