ggbrick is now on CRAN

[This article was first published on R Archives - Dan Oehm | Gradient Descending, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

If you’re looking for something a little different, ggbrick creates a ‘waffle’ style chart with the aesthetic of a brick wall. The usage is similar to geom_col where you supply counts as the height of the bar and a fill for a stacked bar. Each whole brick represents 1 unit. Two half bricks equal one whole brick.

It has been available on Git for a while, but recently I’ve made some changes and it now has CRAN’s tick of approval.

Installation

install.packages("ggbrick")

Geoms

There are two main geoms included:

  1. geom_brick(): To make the brick wall-style waffle chart.
  2. geom_waffle(): To make a regular-style waffle chart.

geom_brick()

Use geom_brick() the same way you would use geom_col().

library(dplyr)
library(ggplot2)
library(ggbrick)

# basic usage
mpg |>
  count(class, drv) |>
  ggplot() +
  geom_brick(aes(class, n, fill = drv)) +
  coord_brick()

coord_brick() is included to maintain the aspect ratio of the bricks. It is similar to coord_fixed(), in fact, it is just a wrapper for coord_fixed() with a parameterised aspect ratio based on the number of bricks. The default number of bricks is 4. To change the width of the line outlining the brick use the linewidth parameter as normal.

To change specify the bricks_per_layer parameter in the geom and coord functions.

mpg |>
  count(class, drv) |>
  ggplot() +
  geom_brick(aes(class, n, fill = drv), bricks_per_layer = 6) +
  coord_brick(6)

You can change the width of the columns similar to geom_col() to add more space between the bars. To maintain the aspect ratio you also need to set the width in coord_brick().

mpg |>
  count(class, drv) |>
  ggplot() +
  geom_brick(aes(class, n, fill = drv), width = 0.5) +
  coord_brick(width = 0.5)

To get more space between each brick use the gap parameter.

mpg |>
  count(class, drv) |>
  ggplot() +
  geom_brick(aes(class, n, fill = drv), gap = 0.04) +
  coord_brick()

For no gap set gap = 0 or use the shorthand geom_brick0().

mpg |>
  count(class, drv) |>
  ggplot() +
  geom_brick0(aes(class, n, fill = drv)) +
  coord_brick()

For fun, I’ve included a parameter to randomise the fill of the bricks or add a small amount of variation at the join between two groups. The proportions are maintained and designed to just give a different visual.

mpg |>
  count(class, drv) |>
  ggplot() +
  geom_brick(aes(class, n, fill = drv), type = "soft_random") +
  coord_brick()
mpg |>
  count(class, drv) |>
  ggplot() +
  geom_brick(aes(class, n, fill = drv), type = "random") +
  coord_brick()

geom_waffle()

geom_waffle() has the same functionality as geom_brick() but the bricks are square giving a standard waffle chart. I added this so you can make a normal waffle chart in the same way you would use geom_col(). It requires coord_waffle(). To maintain the aspect ratio.

mpg |>
  count(class, drv) |>
  ggplot() +
  geom_waffle(aes(class, n, fill = drv)) +
  coord_waffle()
mpg |>
  count(class, drv) |>
  ggplot() +
  geom_waffle0(aes(class, n, fill = drv), bricks_per_layer = 6) +
  coord_waffle(6)

You may want to flip the coords when using geom_waffle(). To do so you’ll need to use coord_flip() and theme(aspect.ratio = <number>). I haven’t made a coord_waffle_flip(), yet!

mpg |>
  count(class, drv) |>
  ggplot() +
  geom_waffle0(aes(class, n, fill = drv)) +
  coord_flip() +
  theme(aspect.ratio = 1.8)

Use {ggfx}

I think geom_brick() pairs with with_shadow() and with_inner_blur() pretty well!

library(ggbrick)
library(ggfx)

font_add_google("Karla", "karla")
showtext_auto()
ft <- "karla"
txt <- "grey10"  
bg <- "white"

survivoR::challenge_results |> 
  filter(
    version == "US",
    outcome_type == "Individual",
    result == "Won"
    ) |> 
   left_join(
      survivoR::castaway_details |>
        select(castaway_id, gender, bipoc),
      by = "castaway_id"
   ) |> 
  left_join(
    survivoR::challenge_description |> 
      mutate(type = ifelse(race, "Race", "Endurance")) |> 
      select(version_season, challenge_id, type),
    by = c("version_season", "challenge_id")
  ) |> 
  count(type, gender) |> 
  drop_na() |> 
  ggplot() +
  with_shadow(
    with_inner_glow(
      geom_brick(aes(type, n, fill = gender), linewidth = 0.1, bricks_per_layer = 6)
    ),
    x_offset = 4,
    y_offset = 4
  ) +
  coord_brick(6) +
  scale_fill_manual(values = blue_pink[c(5, 1, 4)]) +
  labs(
    title = toupper("Survivor Challenges"),
    subtitle = "Approximately a third of races and half of endurance challenges\nare won by women.",
    fill = "Gender",
    caption = "Individual challenges only. The different proportions of men and women at merge hasn't been taken into consideration."
  ) +
  theme_void() +
  theme(
    text = element_text(family = ft, colour = txt, lineheight = 0.3, size = 32),
    plot.background = element_rect(fill = bg, colour = bg),
    plot.title = element_markdown(size = 128, colour = txt, hjust = 0.5, margin = margin(b = 10)),
    plot.subtitle = element_text(hjust = 0.5, size = 48, margin = margin(b = 30)),
    plot.caption = element_text(size = 24, hjust = 0, margin = margin(t = 20)),
    axis.text = element_text(vjust = 0),
    axis.title.y = element_blank(),
    plot.margin = margin(t = 30, b = 10, l = 30, r = 30),
    legend.position = "top"
  )

The post ggbrick is now on CRAN appeared first on Dan Oehm | Gradient Descending.

To leave a comment for the author, please follow the link and comment on their blog: R Archives - Dan Oehm | Gradient Descending.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)