简体   繁体   English

是否可以使用单个 `geom_boxplot()` 将 ggplot 分组的部分箱线图分组,而没有分面?

[英]is it possible to ggplot grouped partial boxplots w/o facets w/ a single `geom_boxplot()`?

I needed to add some partial boxplots to the following plot:我需要在下图中添加一些部分箱线图:

library(tidyverse)

foo <- tibble(
  time = 1:100,
  group = sample(c("a", "b"), 100, replace = TRUE) %>% as.factor()
) %>% 
  group_by(group) %>% 
  mutate(value = rnorm(n()) + 10 * as.integer(group)) %>%
  ungroup()

foo %>%
  ggplot(aes(x = time, y = value, color = group)) + 
    geom_point() +
    geom_smooth(se = FALSE)

在此处输入图片说明

I would add a grid of (2 x 4 = 8) boxplots (4 per group) to the plot above.我会在上面的图中添加一个 (2 x 4 = 8) 箱线图(每组 4 个)的网格。 Each boxplot should consider a consecutive selection of 25 (or n) points (in each group).每个箱线图应考虑连续选择 25 个(或 n 个)点(在每组中)。 Ie, the firsts two boxplots represent the points between the 1st and the 25th (one boxplot below for the group a, and one boxplot above for the group b).即,前两个箱线图表示第 1 个和第 25 个之间的点(a 组下方的一个箱线图,b 组上方的一个箱线图)。 Next to them, two other boxplots for the points between the 26th and 50th, etcetera.在它们旁边,还有另外两个箱线图,用于第 26 和第 50 点之间的点,等等。 If they are not in a perfect grid (which I suppose would be both more challenging to obtain and uglier) it would be even better: I prefer if they will "follow" their corresponding smooth line!如果它们不是在一个完美的网格中(我认为这会更难获得并且更丑),那就更好了:我更喜欢它们是否会“遵循”相应的平滑线!

That all without using facets (because I have to insert them in a plot which is already facetted :-))这一切都没有使用刻面(因为我必须将它们插入到已经刻面的图中:-))

I tried to我试过了

bar <- foo %>%
  group_by(group) %>%
  mutate(cut = 12.5 * (time %/% 25)) %>%
  ungroup()

bar %>%
  ggplot(aes(x = time, y = value, color = group)) + 
    geom_point() +
    geom_smooth(se = FALSE) + 
    geom_boxplot(aes(x = cut))

but it doesn't work.但它不起作用。

在此处输入图片说明

I tried to call geom_boxplot() using group instead of x我尝试使用group而不是x调用geom_boxplot()

bar %>%
  ggplot(aes(x = time, y = value, color = group)) + 
    geom_point() +
    geom_smooth(se = FALSE) + 
    geom_boxplot(aes(group = cut))

But it draws the boxplots without considering the groups and loosing even the colors (and add a redundant call including color = group doesn't help)但它在不考虑组的情况下绘制箱线图甚至丢失颜色(并添加包括color = group的冗余调用无济于事)

在此处输入图片说明

Finally, I decided to try it roughly:最后,我决定粗略地尝试一下:

bar %>%   
  ggplot(aes(x = time, y = value, color = group)) + 
  geom_point() +
  geom_smooth(se = FALSE) +
  geom_boxplot(data = filter(bar, group == "a"), aes(group = cut)) +
  geom_boxplot(data = filter(bar, group == "b"), aes(group = cut))

And it works (maintaining even the correct colors from the main aes )!它有效(甚至从主aes保持正确的颜色)! 在此处输入图片说明

Does someone know if it is possible to obtain it using a single call to geom_boxplot() ?有人知道是否可以通过一次调用geom_boxplot()来获得它?

Thanks!谢谢!

This was interesting!这很有趣! I haven't tried to use geom_boxplot with a continuous x before and didn't know how it behaved.我之前没有尝试使用带有连续x geom_boxplot并且不知道它的表现如何。 I think what is happening is that setting group overrides colour in geom_boxplot , so it doesn't respect either the inherited or repeated colour aesthetic.我认为正在发生的事情是设置group覆盖geom_boxplot colour ,因此它不尊重继承或重复的colour美学。 I think this workaround does the trick;我认为这种解决方法可以解决问题; we combine the group and cut variables into group_cut , which takes 8 different values (one for each desired boxplot).我们将groupcut变量组合到group_cut ,它采用 8 个不同的值(每个所需的箱线图一个)。 Now we can map aes(group = group_cut) and get the desired output.现在我们可以映射aes(group = group_cut)并获得所需的输出。 I don't think this is particularly intuitive and it might be worth raising it on the Github, since usually we expect aesthetics to combine nicely (eg combining colour and linetype works fine).我不认为这是特别直观的,它可能值得在 Github 上提出,因为通常我们期望美学能够很好地结合(例如,结合colourlinetype效果很好)。

library(tidyverse)

bar <- tibble(
  time = 1:100,
  group = sample(c("a", "b"), 100, replace = TRUE) %>% as.factor()
) %>% 
  group_by(group) %>% 
  mutate(
    value = rnorm(n()) + 10 * as.integer(group),
    cut = 12.5 * ((time - 1)  %/% 25), # modified this to prevent an extra boxplot
    group_cut = str_c(group, cut)
  ) %>%
  ungroup()

bar %>%
  ggplot(aes(x = time, y  = value, colour = group)) +
  geom_point() +
  geom_smooth(se = FALSE) +
  geom_boxplot(aes(group = group_cut), position = "identity")
#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'

Created on 2019-08-13 by the reprex package (v0.3.0)reprex 包(v0.3.0) 于 2019 年 8 月 13 日创建

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM