I'm wondering how the proportion on the y-axis computed with the following code:
library(ggplot2)
ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, y = stat(prop), group = color))
Here is the output plot.
I know there's a post about the meaning of aes(group = 1)
here. But that doesn't address my issue.
Thank you.
We can see more clearly what is going on if we draw black lines around each bar:
library(ggplot2)
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, y = stat(prop), group = color),
color = "black")
We can see that these bars are stacked. Clearly each group does not sum up to one, so the proportions are not the proportions of each cut that are made up of different colors; rather, they are the proportion of each color belonging to a particular cut. It's easier to see this if we use position_dodge
and fill according to color
:
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, y = stat(prop), fill = color, group = color),
color = "black", position = "dodge")