[英]Default spacing of grouped boxplots in ggplot2: how to derive correct position_dodge width to line up geoms?
this may be a dupe but I have not found the exact solution I need here.这可能是一个骗局,但我还没有在这里找到我需要的确切解决方案。 I need the answer more for pedagogical purposes: I have made a plot that looks the way I want, but I wanted to explain to ggplot beginners exactly why it works.
出于教学目的,我需要更多的答案:我制作了一个看起来像我想要的方式的 plot,但我想向 ggplot 初学者解释它的工作原理。 The question is, why does a
position_dodge(width = 0.75)
argument make the points from stat_summary
line up with the grouped boxplot?问题是,为什么
position_dodge(width = 0.75)
参数使stat_summary
中的点与分组箱线图对齐? I found this number by trial and error but I cannot find the default spacing value that causes the 0.75 width to be "correct."我通过反复试验找到了这个数字,但我找不到导致 0.75 宽度“正确”的默认间距值。 Where is this value found?
这个值在哪里找到?
set.seed(1)
g1mean <- rep(1:4, times=10)
g2mean <- rep(1:4, each=10)
y <- rnorm(n = length(g1mean), mean = g1mean+g2mean, sd = 2)
dat <- data.frame(g1=factor(g1mean), g2=factor(g2mean), y=y)
library(ggplot2)
ggplot(dat, aes(x=g1, fill=g2, y=y)) +
geom_boxplot() +
stat_summary(fun = mean, geom = 'point', color = 'blue', position = position_dodge(width = 0.75))
This looks fine but how can I programmatically determine the optimal width for position_dodge
to make the geoms line up?这看起来不错,但是我如何以编程方式确定
position_dodge
的最佳宽度以使几何图形对齐?
First of all, it actually looks like your points are not quite lined up with the center of each box.... width=
should be just about 0.84 to make it perfect.首先,实际上看起来您的点与每个框的中心并没有完全对齐......
width=
应该只是大约 0.84 以使其完美。
But that's not really the answer to your question.但这并不是您问题的真正答案。 The answer to your question is to realize that there is, in fact, a
position=position_dodge()
applied to the geom_boxplot
call as well.您的问题的答案是要意识到实际上,
position=position_dodge()
也应用于geom_boxplot
调用。 ggplot2
tries to be intelligent, and when you supply a fill=
aesthetic to use, ggplot2
realizes that means you want to use dodging for the boxplot geom. ggplot2
试图变得智能,当你提供一个fill=
美学来使用时, ggplot2
意识到这意味着你想对箱线图几何图形使用闪避。 Do not expect this behavior for all geoms by default, but that's the case for boxplots.默认情况下,不要期望所有几何图形都有这种行为,但箱线图就是这种情况。
The real answer here is that in order to make your points line up between the two, you should supply the same value for position=
to both.这里真正的答案是,为了使您的点在两者之间对齐,您应该为两者提供相同的
position=
值。 You can even specify this outside the ggplot
call:您甚至可以在
ggplot
调用之外指定它:
pos <- position_dodge(width=0.9)
ggplot(dat, aes(x=g1, fill=g2, y=y)) +
geom_boxplot(position=pos) +
stat_summary(fun = mean, geom = 'point', color = 'blue', position = pos)
So... why is the default dodge width somewhere around 0.85 or 0.84?那么...为什么默认的闪避宽度在 0.85 或 0.84 左右? Beats me.
打败我。 Gotta start somewhere?
一定要从某个地方开始吗? It's more important to know how to control it.
更重要的是知道如何控制它。 You will want better control especially if you start to define the width of your boxplots with
width=
.您将需要更好的控制,特别是如果您开始使用
width=
定义箱线图的宽度。 dodge width = geom width will give you dodging so that the boxes exactly touch each other. dodge width = geom width 会给你闪避,使盒子完全相互接触。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.