5

I'm trying to create a bar graph on ggplot that has proportions rather than counts, and I have c+geom_bar(aes(y=(..count..)/sum(..count..)*100)) but I'm not sure what either of the counts refer to. I tried putting in the data but it didn't seem to work. What should I input here?

This is the data I'm using

> describe(topprob1)
topprob1 
      n missing  unique    Info    Mean 
    500       0       9    0.93   3.908 

            1   2 3  4  5   6  7  8 9
Frequency 128 105 9 15 13 172 39 12 7
%          26  21 2  3  3  34  8  2 1
llrs
  • 3,308
  • 35
  • 68
m123
  • 51
  • 1
  • 1
  • 3

2 Answers2

6

You haven't provided a reproducible example, so here's an illustration with the built-in mtcars data frame. Compare the following two plots. The first gives counts. The second gives proportions, which are displayed in this case as percentages. ..count.. is an internal variable that ggplot creates to store the count values.

library(ggplot2)
library(scales)

ggplot(mtcars, aes(am)) +
  geom_bar()

ggplot(mtcars, aes(am)) +
  geom_bar(aes(y=..count../sum(..count..))) +
  scale_y_continuous(labels=percent_format())

enter image description here

Community
  • 1
  • 1
eipi10
  • 91,525
  • 24
  • 209
  • 285
5

You can also use ..prop.. computed variable with group aesthetics:

library(ggplot2)
library(scales)

ggplot(mtcars, aes(am)) +
  geom_bar(aes(y=..prop.., group = 1)) +
  scale_y_continuous(labels=percent_format())

enter image description here

dvillaj
  • 780
  • 1
  • 9
  • 8