I got some floating stacked bars!

data-visualisation
ggplot2
r-how-to
Or did I…
Author
Affiliation

Building Stories with Data

Published

July 15, 2022

This morning’s fun #rstats discovery, while exploring colorblindr::scale_fill_OkabeIto(): I got some floating stacked bars!

Or did I…? Palmer Penguins to the rescue!🐧🐧🐧

Stacked bar plot of the Adelie palmer penguins where the bar fill is the variable "sex", where two of the bars appear to be floating at different levels above the X axis. The clue is in the legend, where NA doesn't have a colour.

Wait a minute…

Same bar plot as in previous image (Adelie penguins), with the NA and corresponding lack of colour is circled in the legend.

palmerpenguins::penguins %>%
  filter(species == "Adelie") %>%
  ggplot() +
  geom_bar(aes(x = island, fill = sex)) +
  labs(x = "", y = "", title = "Adelie", fill = "Sex") +
  theme_minimal() +
  colorblindr::scale_fill_OkabeIto()

The solution? Specify the NA colour using something like this…

colorblindr::scale_fill_OkabeIto(na.value = "grey50"). Or if you prefer, “pink”!

Adelie penguins bar stacked bar plot, where the NA values are represented in pink.

palmerpenguins::penguins %>%
  filter(species == "Adelie") %>%
  ggplot() +
  geom_bar(aes(x = island, fill = sex)) +
  labs(x = "", y = "", title = "Adelie", fill = "Sex") +
  theme_minimal() +
  colorblindr::scale_fill_OkabeIto(na.value = "pink")

Reuse

Citation

For attribution, please cite this work as:
Thompson, Cara. 2022. “I Got Some Floating Stacked Bars!” July 15, 2022. https://www.cararthompson.com/posts/2022-07-15-this-mornings-fun-rstats-discovery/.