简体   繁体   English

ggplot2 中分组箱线图的默认间距:如何得出正确的 position_dodge 宽度以排列几何图形?

[英]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?这个值在哪里找到?

reprex代表

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))

result结果

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.

相关问题 如何增加具有离散 x 轴的箱线图 (ggplot2) 之间的距离 - position_dodge? - How to increase the distance between boxplots (ggplot2) which have discrete x-axis - position_dodge? ggplot2 position_dodge影响误差线的宽度 - ggplot2 position_dodge affects error bar width 如何使用 ggplot2 中的 position_dodge() 为 geom_col() 中的每个条获得相同的宽度? - How to get the same width for every bar in this geom_col() with position_dodge() in ggplot2? 如何在ggplot2中使用position_dodge和2个级别的分组 - How to use position_dodge with 2 levels of groupings in ggplot2 为什么我得到“position_dodge需要恒定宽度”,即使ggplot2中的宽度是恒定的 - Why do i get “position_dodge requires constant width” even though widths are constant in ggplot2 将position_dodge和position_fill组合在ggplot2中 - Combining position_dodge and position_fill in ggplot2 如何在 ggplot2 中的 position_dodge 中将单条 position 居中 - How to centre single bar position with multiple bars in position_dodge in ggplot2 ggplot2 position_dodge高度不起作用 - ggplot2 position_dodge height does not work 按颜色显示position_dodge,但在ggplot2中不是带有geom_point的pch - position_dodge by colour but not pch with geom_point in ggplot2 ggplot2: geom_bar with group, position_dodge and fill - ggplot2: geom_bar with group, position_dodge and fill
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM