Arthur Yip
Arthur Yip

Reputation: 6230

geom_col with facet_grid with margins=TRUE not stacking up?

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

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)

Upvotes: 3

Views: 590

Answers (1)

jared_mamrot
jared_mamrot

Reputation: 26650

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

Upvotes: 1

Related Questions