R stacked percentage bar plot with percentage of binary factor and labels (with ggplot)

This is a way to generate the plot:

ggplot(bb[bb$FIX == 1, ],aes(x = factor(QUANT), fill = factor(IMG), 
                             y = (..count..)/sum(..count..))) +
 geom_bar() +
 stat_bin(geom = "text",
          aes(label = paste(round((..count..)/sum(..count..)*100), "%")),
          vjust = 5) +
 scale_y_continuous(labels = percent)

Change the value of the vjust parameter to adjust the vertical position of the labels.

enter image description here

Leave a Comment