【问题标题】:geom_col with facet_grid with margins=TRUE not stacking up?geom_col with facet_grid with margins=TRUE 不堆叠?
【发布时间】:2021-01-26 06:13:03
【问题描述】:

边距中的条形似乎选择了最大方面,而不是像我预期的那样将它们相加。

  • 它们不应该堆积在边缘吗?
  • 这是一个错误吗?
  • 对我如何更改 geom_col / geom_bar / position 设置或 stat_function 有什么建议吗?解决这个问题?
library(palmerpenguins); library(tidyverse); library(janitor)

penguins_raw %>%
  clean_names() %>%
  ggplot() +
  geom_col(aes(x = clutch_completion, y = body_mass_g, fill = sex), position = "dodge") +
  facet_grid(rows = vars(species), cols = vars(island), margins = TRUE)
#> Warning: Removed 8 rows containing missing values (geom_col).

reprex package (v0.3.0) 于 2020 年 10 月 11 日创建

编辑 1:这里有一些进一步的调查:如果我只使用 geom_col position = dodge 没有刻面,我会得到以下结果。 dodge2 向我们展示了它是在彼此之上绘制而不是将它们堆叠起来,所以这就是为什么它看起来像最大值?看起来堆叠的较大闪避条似乎也没有拾取 dodge2 中显示的所有数据,但这可能是因为数据是如何分层的?

所以我的问题变成了:如何同时堆叠和躲避 geom_col?

library(palmerpenguins, quietly = TRUE); library(tidyverse, quietly = TRUE); library(janitor, quietly = TRUE)
#> Warning: package 'palmerpenguins' was built under R version 3.6.3
#> Warning: package 'ggplot2' was built under R version 3.6.3
#> Warning: package 'tibble' was built under R version 3.6.3
#> Warning: package 'tidyr' was built under R version 3.6.3
#> Warning: package 'purrr' was built under R version 3.6.3
#> Warning: package 'dplyr' was built under R version 3.6.3
#> Warning: package 'forcats' was built under R version 3.6.3
#> Warning: package 'janitor' was built under R version 3.6.3
#> 
#> Attaching package: 'janitor'
#> The following objects are masked from 'package:stats':
#> 
#>     chisq.test, fisher.test
penguins_raw %>% clean_names() %>%
  ggplot() + 
  geom_col(aes(x = clutch_completion, y = body_mass_g, col = sex), fill = "grey", position = "dodge") +
  geom_col(aes(x = clutch_completion, y = body_mass_g, fill = sex), position = "dodge2")
#> Warning: Removed 2 rows containing missing values (geom_col).
#> Warning: Removed 2 rows containing missing values (geom_col).

reprex package (v0.3.0) 于 2020 年 10 月 11 日创建

【问题讨论】:

  • 我已将其作为潜在问题记录在 github.com/tidyverse/ggplot2/issues/4228
  • 我已经接受了一个合适的解决方法,但我认为 stack/dodge/dodge2 以及不堆叠的构面边距仍然存在问题。

标签: r ggplot2 margins facet-grid geom-col


【解决方案1】:

这个问题可能有一个聪明的解决方案,但也许这可以适应你想做的事情?

options(scipen = 100000)
library(palmerpenguins); library(tidyverse); library(janitor)

penguins_raw %>%
  clean_names() %>%
  mutate(clutch_int = factor(str_replace(interaction(clutch_completion,
                                                     sex),
                                         '\\.', ' / '),
                             ordered=TRUE)) %>% 
  ggplot() +
  geom_col(aes(x = clutch_int,
               y = body_mass_g,
               fill = sex),
           position = "stack") +
  theme(axis.text.x = element_text(angle = 75, hjust = 1)) +
  facet_grid(scales = "free",
             rows = vars(species), drop = FALSE,
             cols = vars(island), margins = TRUE)

【讨论】:

  • 谢谢!根据您的情节,我期望/从 position = "dodge" 得到的东西似乎存在问题,而不仅仅是 facet_grid 边距。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
  • 2016-06-22
  • 1970-01-01
相关资源
最近更新 更多