ggplot replace count with percentage in geom_bar

If your goal is visualization in minimal code, use position = "fill" as an argument in geom_bar().

If you want within group percentages, @Jaap’s dplyr answer answer is the way to go.

Here is a reproducible example using the above dataset to copy/paste:

library(tidyverse)

d <- data_frame(groupchange = c(4,4,4,4,5,5,5,4,2,5,5,5,5,4,5,1,4,1,5,4),
                Symscore3 = c(1,2,1,2,0,0,0,0,2,0,0,1,0,1,1,0,0,1,1,0))

ggplot(d, aes(x = factor(groupchange), fill = factor(Symscore3))) +
  geom_bar(position="fill")

enter image description here

Leave a Comment