3

The bars in the margins appear to be picking the maximum facet rather than summing them up as I expected.

  • Shouldn't they be stacking up in the margins?
  • Is this a bug?
  • Any suggestions how I might change geom_col / geom_bar / position settings, or a stat_function? to fix this?
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).

Created on 2020-10-11 by the reprex package (v0.3.0)

Edit 1: Here's some further investigation: if I just use geom_col position = dodge without the facets, I get the below. dodge2 shows us that it's plotting on top of each other rather than stacking them up so that's why it looks like the maximum? the larger dodge bars that look stacked don't seem to pick up all the data shown in dodge2 either, but that could be because of how the data was layered on?

So my question becomes: how do I stack and dodge geom_col at the same time?

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

Created on 2020-10-11 by the reprex package (v0.3.0)

Arthur Yip
  • 5,810
  • 2
  • 31
  • 50
  • I have logged it as a potential issue at https://github.com/tidyverse/ggplot2/issues/4228 – Arthur Yip Oct 11 '20 at 22:29
  • 1
    I've accepted an answer as a suitable work-around, but I believe there remains an issue with stack/dodge/dodge2 as well as the facet margins that don't stack. – Arthur Yip Oct 22 '20 at 20:59

1 Answers1

1

There's probably a clever solution to this issue but maybe this can be adapted for what you want to do?

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)

example_image

jared_mamrot
  • 22,354
  • 4
  • 21
  • 46
  • Thanks! Based on your plot, it looks like there are issues with what I expect/get from position = "dodge" and not just with the facet_grid margins. – Arthur Yip Oct 12 '20 at 18:26