I'm trying to plot a stacked bar chart showing the relative percentages of each group within a column.
Here's an illustration of my problem, using the default mpg data set:
mpg %>%
ggplot(aes(x=manufacturer, group=class)) +
geom_bar(aes(fill=class), stat="count") +
geom_text(aes(label=scales::percent(..prop..)),
stat="count",
position=position_stack(vjust=0.5))
My problem is that this output shows the percentage of each class against the grand total, not the relative percentage within each manufacturer.
For example, I want the first column (audi) to show 83.3% (15/18) for brown (compact) and 16.6% (3/18) for green (midsize).
I found a similar question here: How to draw stacked bars in ggplot2 that show percentages based on group?
But I wanted to know if there's an easier way to do this within ggplot2, especially since my actual dataset uses a bunch of dplyr pipes to massage the data before ultimately piping it into ggplot2.