问题
I know in this command line the height of the bars represents the count of each group in this variable "color":
ggplot(diamonds, aes(color, fill = cut)) +
geom_bar()
But I really wanna know what about this command line:
ggplot(diamonds, aes(color, fill = cut)) +
geom_bar(alpha=0.5, position = "identity")
I know the former is defaulted as position "stack" and I also know the meaning of position "identity". But I really can't figure out what the height of the bars in the later one represents?
Thanks many in advance!
回答1:
I think the best way to understand it is to imagine using position='dodge'
(which places multiple bars for different cuts, separated by color) and instead layering all the cut bars on top of each other.
ggplot(diamonds, aes(color, fill = cut)) +
geom_bar(alpha=0.5, position = "dodge")
ggplot(diamonds, aes(color, fill = cut)) +
geom_bar(alpha=0.5, position = "identity")
(Note, the colors get distorted because the 'Fair' cut is in front.)
回答2:
When you use position=stack, for each x position counts per group in the fill are.stacked on top.of each other..with position=identity on the other hand for each x position if there are multiple groups in the fill varaibles they also start at y=0 and are essentially overlaid.
来源:https://stackoverflow.com/questions/60843077/what-does-the-height-of-bars-in-the-two-bar-chats-with-different-position-repr