Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create proportion bar plot with dodge position

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")
like image 356
chintan s Avatar asked Jan 29 '26 10:01

chintan s


1 Answers

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") 

enter image description here

like image 197
pogibas Avatar answered Jan 30 '26 23:01

pogibas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!