JBJ
JBJ

Reputation: 886

Plot relative frequencies with dodged bar plots in ggplot2

can anybody answer how I can plot a bar-plot which maps a numeric x-variable to its relative frequency grouped by a factor in ggplot2? The important thing is: The relative frequencies should be calculated as groupwise frequencies within x-values belonging to one factor. Now they are calculated as x-values relative to the total number of x-values.

To illustrates it, an example:

library(ggplot2)

data <-data.frame(x=runif(100,0:1), f=sample(1:3,100,replace=TRUE))
data$f <-factor(data$f)
p <-ggplot(data, aes(x, colour=f, fill=f, group=f)) +
    xlim(0,1) +
    scale_y_continuous('Relative Frequency', formatter='percent') # or labels=percent

Let us plot them facetted. Then the y-axis shows that the proportions are calculated groupwise. I used this code:

p + stat_bin(aes(y=..count../sum(..count..)), position='dodge', binwidth=0.2) +         facet_grid(~f)

Let us plot them dodged. The y-axis shows that the proportions refer to the whole dataset. Here, I used the following code:

p + stat_bin(aes(y=..count../sum(..count..)), position='dodge', binwidth=0.2)

I aim at creating a plot like the second with the frequencies of the variable per group on the y-axis.

Thank a lot for your help in advance! Jana

Upvotes: 2

Views: 5944

Answers (1)

Elodie
Elodie

Reputation: 37

I was facing the same problem as yours and I figured it out: instead of plotting ..count../sum(..count..), plot ..density..*your_binwidth Indeed, the ..density.. variable created by the bin stat is basically equal to proportion/bindwidth. Your code becomes:

p + stat_bin(aes(y=..density..*0.2), position='dodge', binwidth=0.2)

Upvotes: 2

Related Questions