Edit: This question is not a duplicate of How to draw stacked bars in ggplot2 that show percentages based on group? . The data there is not purely factors and includes a "y =" in
ggplot(df, aes(x = factor(year), y = amount, fill = type))
As far as I know, "y =" is not relevant to my example.
That said, my goal is to display the percentages for the factor used in fill within ggplot. This is a bit different than other questions because I don't want to display the percentage for the x-variable—I want the percentages to be based on the fill variable, which will be a factor. It's probably easiest to illustrate with mtcars.
df<-data(mtcars)
ggplot(mtcars, aes(x = factor(mtcars$vs),fill=factor(mtcars$am))) +
geom_bar(aes(y=(..count..)/sum(..count..))) +
scale_y_continuous(labels=percent)
This gives us:
What I want, however, is