I am trying to create the following plot but with proportion on the y axis.
library(ggplot2)
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge")
but when I add y=..prop..
, it doesn't group it by clarity
. I have tried the following:
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, y = ..prop.., fill = clarity), position = "dodge")
To calculate proportion (or frequency) you can use ..count..
(proportion is specific count
divided by all count
's):
library(ggplot2)
ggplot(diamonds, aes(cut, (..count..) / sum(..count..), fill = clarity)) +
geom_bar(position = "dodge")