"
TODO Description
Author
Affiliation

Building Stories with Data

Published

December 8, 2022

Two #rstats #dataviz aha moments from this morning

Photo of tasty looking waffles

Photo of three cows looking down at the camera with a blue sky in the background

🧇 The order of the fill colour is determined by the order in the dataframe, not by the order of factor levels. To make sure the waffle is in the order you want, rearrange your dataframe first!

palmerpenguins::penguins |> group_by(species) |> count() |> mutate(species = factor(species, levels = c(“Adelie”, “Gentoo”, “Chinstrap”))) |> ggplot() + waffle::geom_waffle(aes(fill = species, values = n), colour = “#FFFFFF”, make_proportional = FALSE, size = 2) + labs(fill = “Species”) + coord_equal() + theme_void() + theme(legend.position = “top”)

:
palmerpenguins::penguins |>
  group_by(species) |>
  count() |>
  mutate(species = factor(species, 
                          levels = c("Adelie", "Gentoo", "Chinstrap"))) |>
  arrange(desc(n)) |>
  ggplot() +
  waffle::geom_waffle(aes(fill = species,
                          values = n),
                      colour = "#FFFFFF",
                      make_proportional = FALSE,
                      size = 2) +
  labs(fill = "Species") +
  coord_equal() +
  theme_void() +
  theme(legend.position = "top")

Waffle plot in which the species are ordered Adelie, Chinstrap, Gentoo in the plot, but Adelie, Gentoo, Chinstrap in the legend

Waffle plot in which the species are ordered the same way in the plot and in the legend

🐮 Integrating a plot into a report styled with a coloured background can be a pain when using a plot which has fixed coords or uses coord_polar() because you end up with white space around it and a lot of trial an error with plot sizes to get rid of it!

{cowplot} to the rescue!

Alt text: NA

Alt text: NA

Alt text: NA

Now to get some lunch - waffles, anyone?

(Code snippet for second tweet: https://gist.github.com/cararthompson/8e9451341cde62c5e89f878c3b8e6edf)

Reuse

Citation

For attribution, please cite this work as:
Thompson, Cara. 2022. “Two Rstats Dataviz Aha Moments from This Morning.” December 8, 2022. https://www.cararthompson.com/posts/2022-12-08-two-rstats-dataviz-aha-moments/.