Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201919

stat_count function in ggplot2

$
0
0

For the simplest case

library(ggplot2)
ggplot(data = diamonds) + 
 geom_bar(aes(x = cut, y = ..prop..))

it finds the frequency for each level of cut. Then the proportion is calculated with respect to that level so giving ..prop.. as 1 for each level.

If we specify group = 1, then it calculates the proportions based on the grand total.

If I argue in the same way, then

ggplot(data = diamonds) + 
 geom_bar(aes(x = cut, y = ..prop.., fill = color))

should produce frequencies similar to a 2-way contingency table. So it seems like ..prop.. is calculated for each cell in the contingency table with respect to that particular cell. The result is what I expected.

But if I specify group = 1, why doesn't it produce the same output as

ggplot(data = diamonds) + 
 geom_bar(aes(x = cut, y = ..count.. / sum(..count..), fill = color))

Because I thought setting group = 1 would change the default grouping criteria. Is there a code that we can write using group?


Viewing all articles
Browse latest Browse all 201919

Trending Articles