3

边距中的条似乎在选择最大方面,而不是像我预期的那样总结它们。

  • 他们不应该堆积在边缘吗?
  • 这是一个错误吗?
  • 有什么建议可以更改 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 包(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 包(v0.3.0)于 2020 年 10 月 11 日创建

4

1 回答 1

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)

示例图像

于 2020-10-11T23:42:34.783 回答