ggplot geom_bar:aes(组= 1)的含义 [英] ggplot geom_bar: meaning of aes(group = 1)
问题描述
我正在r4ds.had.co.nz的3.7节中学习 geom_bar .我运行这样的代码:
I am learning geom_bar on section 3.7 of r4ds.had.co.nz. I run a code like this:
library(ggplot2)
ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, y = ..prop.., group = 1))
然后我有这个情节:
Then I have this plot:
要点是,如果我排除组= 1"部分:
The point is, if I exclude the "group = 1" part:
library(ggplot2)
ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, y = ..prop..))
情节将是错误的,
但是,如果我将group = 1替换为group = 2或group ="x",则该图看起来仍然正确.所以我不太了解group = 1的含义以及如何使用它.
But if I replace group = 1 by group = 2 or group = "x", the plot still looks correct. So I don't quite understand the meaning of group = 1 here and how to use it.
推荐答案
group="whatever"
是一个虚拟"分组,用于覆盖默认行为,默认行为是(此处)按cut
分组,通常是按x变量. geom_bar
的默认值是按x变量分组,以便分别计算x变量每个级别中的行数.例如,在这里,默认设置是geom_bar
返回cut
等于"Fair","Good"等的行数.
group="whatever"
is a "dummy" grouping to override the default behavior, which (here) is to group by cut
and in general is to group by the x variable. The default for geom_bar
is to group by the x variable in order to separately count the number of rows in each level of the x variable. For example, here, the default would be for geom_bar
to return the number of rows with cut
equal to "Fair", "Good", etc.
但是,如果我们需要比例,那么我们需要一起考虑cut
的所有级别.在第二个图中,首先将数据按cut
分组,因此分别单独考虑cut
的每个级别.公平中公平的比例为100%,商品中良好等的比例也是100%.group=1
(或group="x"
等)可防止这种情况,因此每个级别的切割比例将相对于所有削减水平.
However, if we want proportions, then we need to consider all levels of cut
together. In the second plot, the data are first grouped by cut
, so each level of cut
is considered separately. The proportion of Fair in Fair is 100%, as is the proportion of Good in Good, etc. group=1
(or group="x"
, etc.) prevents this, so that the proportions of each level of cut will be relative to all levels of cut.
这篇关于ggplot geom_bar:aes(组= 1)的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!