Hello đź‘‹

đź‘© Cara Thompson

👩‍💻 Love for patterns in music & language, and a fascination with the human brain %>%

       Psychology PhD %>%

       Analysis of postgraduate medical examinations %>%

       Data Visualisation Consultant


đź’™ Helping others maximise the impact of their expertise

The main aim for today

  • Build a (gg)plot

The main aim for today

  • Build a (gg)plot
  • Give it a clear visual identity

The main aim for today

  • Build a (gg)plot
  • Give it a clear visual identity
  • Parameterise it

Let’s go!

library(tidyverse)

palmerpenguins::penguins |>
  ggplot() +
  geom_point(aes(x = bill_length_mm,
                 y = flipper_length_mm,
                 colour = species),
             size = 5, 
             alpha = 0.9) +
  labs(title = "Flipper lengths are proportional to bill lengths within each species",
       x = "Bill length (mm)",
       y = "Flipper length (mm)")

Let’s go!

Change the theme

palmerpenguins::penguins |>
  ggplot() +
  geom_point(aes(x = bill_length_mm,
                 y = flipper_length_mm,
                 colour = species),
             size = 5, 
             alpha = 0.9) +
  labs(title = "Flipper lengths are proportional to bill lengths within each species",
       x = "Bill length (mm)",
       y = "Flipper length (mm)") +
  theme_minimal()

Let’s add some style!

Change the colours

palmerpenguins::penguins |>
  ggplot() +
  geom_point(aes(x = bill_length_mm,
                 y = flipper_length_mm,
                 colour = species),
             size = 5, 
             alpha = 0.9) +
  labs(title = "Flipper lengths are proportional to bill lengths within each species",
       x = "Bill length (mm)",
       y = "Flipper length (mm)") +
  theme_minimal() +
  scale_colour_manual(values = c("pink", 
                                 "orange", 
                                 "darkgreen"))

Let’s add some style!

Change the colours - wait a minute…

head(palmerpenguins::penguins, 200) |>
  ggplot() +
  geom_point(aes(x = bill_length_mm,
                 y = flipper_length_mm,
                 colour = species),
             size = 5, 
             alpha = 0.9) +
  labs(title = "Flipper lengths are proportional to bill lengths within each species",
       x = "Bill length (mm)",
       y = "Flipper length (mm)") +
  theme_minimal() +
  scale_colour_manual(values = c("pink", 
                                 "orange", 
                                 "darkgreen"))

Let’s add some style!

Mini tip #1: Named vectors for colours!

penguin_colours <- c(Adelie = "pink", 
                     Chinstrap = "orange",
                     Gentoo = "darkgreen")

palmerpenguins::penguins |>
  ggplot() +
  geom_point(aes(x = bill_length_mm,
                 y = flipper_length_mm,
                 colour = species),
             size = 5, 
             alpha = 0.9) +
  labs(title = "Flipper lengths are proportional to bill lengths within each species",
       x = "Bill length (mm)",
       y = "Flipper length (mm)") +
  theme_minimal() +
  scale_colour_manual(values = penguin_colours)

Let’s add some style!

Mini tip #1: Named vectors for colours!

penguin_colours <- c(Adelie = "pink", 
                     Chinstrap = "orange",
                     Gentoo = "darkgreen")

head(palmerpenguins::penguins, 200) |>
  ggplot() +
  geom_point(aes(x = bill_length_mm,
                 y = flipper_length_mm,
                 colour = species),
             size = 5, 
             alpha = 0.9) +
  labs(title = "Flipper lengths are proportional to bill lengths within each species",
       x = "Bill length (mm)",
       y = "Flipper length (mm)") +
  theme_minimal() +
  scale_colour_manual(values = penguin_colours)

Let’s add some style!

Mini tip #2: Invest in your own custom theme

penguin_colours <- c(Adelie = "pink", 
                     Chinstrap = "orange",
                     Gentoo = "darkgreen")

palmerpenguins::penguins |>
  ggplot() +
  geom_point(aes(x = bill_length_mm,
                 y = flipper_length_mm,
                 colour = species),
             size = 5, 
             alpha = 0.9) +
  labs(title = "Flipper lengths are proportional to bill lengths within each species",
       x = "Bill length (mm)",
       y = "Flipper length (mm)",
       colour = "") +
  theme_dt_demo() +
  scale_colour_manual(values = penguin_colours)

Let’s add some style!

Mini tip #2: Invest in your own custom theme with relative text sizes

penguin_colours <- c(Adelie = "pink", 
                     Chinstrap = "orange",
                     Gentoo = "darkgreen")

palmerpenguins::penguins |>
  ggplot() +
  geom_point(aes(x = bill_length_mm,
                 y = flipper_length_mm,
                 colour = species),
             size = 5, 
             alpha = 0.9) +
  labs(title = "Flipper lengths are proportional to bill lengths within each species",
       x = "Bill length (mm)",
       y = "Flipper length (mm)",
       colour = "") +
  theme_dt_demo(base_size = 16) +
  scale_colour_manual(values = penguin_colours)

Let’s add some annotations

I ❤️ {ggtext}

mean_x_y <- palmerpenguins::penguins |>
  group_by(species) |>
  summarise(mean_x = mean(bill_length_mm, na.rm = TRUE),
            mean_y = mean(flipper_length_mm, na.rm = TRUE))

palmerpenguins::penguins |>
  ggplot() +
  geom_point(aes(x = bill_length_mm,
                 y = flipper_length_mm,
                 colour = species),
             size = 5, 
             alpha = 0.9) +
  ggtext::geom_textbox(data = mean_x_y,
                       aes(x = mean_x,
                           y = mean_y,
                           label = species),
                       size = 7) +
  labs(title = "Flipper lengths are proportional to bill lengths within each species",
       x = "Bill length (mm)",
       y = "Flipper length (mm)",
       colour = "") +
  theme_dt_demo(16) +
  scale_colour_manual(values = penguin_colours)

Let’s add some annotations

I ❤️ {ggtext}

mean_x_y <- palmerpenguins::penguins |>
  group_by(species) |>
  summarise(mean_x = mean(bill_length_mm, na.rm = TRUE),
            mean_y = mean(flipper_length_mm, na.rm = TRUE),
            count = length(species))

palmerpenguins::penguins |>
  ggplot() +
  geom_point(aes(x = bill_length_mm,
                 y = flipper_length_mm,
                 colour = species),
             size = 5, 
             alpha = 0.9,
             show.legend = FALSE) +
  ggtext::geom_textbox(data = mean_x_y,
                       aes(x = mean_x,
                           y = mean_y,
                           label = paste0("**", species, "**",
                                          "<br>N = ", count)),
                       size = 7) +
  labs(title = "Flipper lengths are proportional to bill lengths within each species",
       x = "Bill length (mm)",
       y = "Flipper length (mm)",
       colour = "") +
  theme_dt_demo(16) +
  scale_colour_manual(values = penguin_colours)

Let’s add some annotations

I ❤️ {ggtext}

mean_x_y <- palmerpenguins::penguins |>
  group_by(species) |>
  summarise(mean_x = mean(bill_length_mm, na.rm = TRUE),
            mean_y = mean(flipper_length_mm, na.rm = TRUE),
            mean_weight = mean(body_mass_g, na.rm = TRUE),
            count = length(species))

palmerpenguins::penguins |>
  ggplot() +
  geom_point(aes(x = bill_length_mm,
                 y = flipper_length_mm,
                 colour = species),
             size = 5, 
             alpha = 0.9,
             show.legend = FALSE) +
  ggtext::geom_textbox(data = mean_x_y,
                       aes(x = mean_x,
                           y = mean_y,
                           label = paste0("**", species, "**",
                                          " (N = ", count, ")",
                                          "<br>Mean weight: ", janitor::round_half_up(mean_weight/1000, 2), "kg")),
                       size = 7) +
  labs(title = "Flipper lengths are proportional to bill lengths within each species",
       x = "Bill length (mm)",
       y = "Flipper length (mm)",
       colour = "") +
  theme_dt_demo(16) +
  scale_colour_manual(values = penguin_colours)

Let’s add some annotations

I ❤️ {ggtext}

mean_x_y <- palmerpenguins::penguins |>
  group_by(species) |>
  summarise(mean_x = mean(bill_length_mm, na.rm = TRUE),
            mean_y = mean(flipper_length_mm, na.rm = TRUE),
            mean_weight = mean(body_mass_g, na.rm = TRUE),
            count = length(species))

palmerpenguins::penguins |>
  ggplot() +
  geom_point(aes(x = bill_length_mm,
                 y = flipper_length_mm,
                 colour = species,
                 size = body_mass_g),
             size = 5, 
             alpha = 0.9,
             show.legend = FALSE) +
  ggtext::geom_textbox(data = mean_x_y,
                       aes(x = mean_x,
                           y = mean_y,
                           label = paste0("**", species, "**",
                                          " (N = ", count, ")",
                                          "<br>Mean weight: ", janitor::round_half_up(mean_weight/1000, 2), "kg")),
                       size = 7,
                       family = "Work Sans",
                       width = unit(20, "lines"),
                       fill = "#FEFDFA",
                       box.colour = NA,
                       alpha = 0.9,
                       halign = 0.5) +
  labs(title = "Flipper lengths are proportional to bill lengths within each species",
       x = "Bill length (mm)",
       y = "Flipper length (mm)",
       colour = "") +
  theme_dt_demo() +
  scale_colour_manual(values = penguin_colours)

From basic to styled and annotated

From basic to styled and annotated

Let’s
dance!

Parameterisation #1: Different data

Parameterisation #1: Different data

“Oh, I’m sorry, I actually just wanted the first 200 penguins…”

mean_x_y <- head(palmerpenguins::penguins, 200) |>
  group_by(species) |>
  summarise(mean_x = mean(bill_length_mm, na.rm = TRUE),
            mean_y = mean(flipper_length_mm, na.rm = TRUE),
            mean_weight = mean(body_mass_g, na.rm = TRUE),
            count = length(species))

head(palmerpenguins::penguins, 200) |>
  ggplot() +
  geom_point(aes(x = bill_length_mm,
                 y = flipper_length_mm,
                 colour = species,
                 size = body_mass_g),
             size = 5, 
             alpha = 0.9,
             show.legend = FALSE) +
  ggtext::geom_textbox(data = mean_x_y,
                       aes(x = mean_x,
                           y = mean_y,
                           label = paste0("**", species, "**",
                                          " (N = ", count, ")",
                                          "<br>Mean weight: ", janitor::round_half_up(mean_weight/1000, 2), "kg")),
                       size = 7,
                       family = "Work Sans",
                       width = unit(20, "lines"),
                       fill = "#FEFDFA",
                       box.colour = NA,
                       alpha = 0.9,
                       halign = 0.5) +
  labs(title = "Flipper lengths are proportional to bill lengths within each species",
       x = "Bill length (mm)",
       y = "Flipper length (mm)",
       colour = "") +
  theme_dt_demo(16) +
  scale_colour_manual(values = penguin_colours)

Parameterisation #1: Different data

“Hmm, wait, could do both, for comparison?”

Parameterisation #1: Different data

“… and could we check with 300 and 400 also?”

Parameterisation #1: Different data

“I realise the meeting is in 5 minutes, but I really don’t like those fonts and colours… Could you change them?”

Parameterisation #1: Different data

Our starting point

mean_x_y <- palmerpenguins::penguins |>
  group_by(species) |>
  summarise(mean_x = mean(bill_length_mm, na.rm = TRUE),
            mean_y = mean(flipper_length_mm, na.rm = TRUE),
            mean_weight = mean(body_mass_g, na.rm = TRUE),
            count = length(species))

palmerpenguins::penguins |>
  ggplot() +
  geom_point(aes(x = bill_length_mm,
                 y = flipper_length_mm,
                 colour = species,
                 size = body_mass_g),
             size = 5, 
             alpha = 0.9,
             show.legend = FALSE) +
  ggtext::geom_textbox(data = mean_x_y,
                       aes(x = mean_x,
                           y = mean_y,
                           label = paste0("**", species, "**",
                                          " (N = ", count, ")",
                                          "<br>Mean weight: ", janitor::round_half_up(mean_weight/1000, 2), "kg")),
                       size = 7,
                       family = "Work Sans",
                       width = unit(20, "lines"),
                       fill = "#FEFDFA",
                       box.colour = NA,
                       alpha = 0.9,
                       halign = 0.5) +
  labs(title = "Flipper lengths are proportional to bill lengths within each species",
       x = "Bill length (mm)",
       y = "Flipper length (mm)",
       colour = "") +
  theme_dt_demo(16) +
  scale_colour_manual(values = penguin_colours)

Parameterisation #1: Different data

Mini tips #3 and #4: Defaults and environments

make_penguin_plot <- function(df = palmerpenguins::penguins,
                              colours = penguin_colours) {
  
  mean_x_y <- df |>
    group_by(species) |>
    summarise(mean_x = mean(bill_length_mm, na.rm = TRUE),
              mean_y = mean(flipper_length_mm, na.rm = TRUE),
              mean_weight = mean(body_mass_g, na.rm = TRUE),
              count = length(species))
  
  df |>
    ggplot() +
    geom_point(aes(x = bill_length_mm,
                   y = flipper_length_mm,
                   colour = species,
                   size = body_mass_g),
               size = 5, 
               alpha = 0.9,
               show.legend = FALSE) +
    ggtext::geom_textbox(data = mean_x_y,
                         aes(x = mean_x,
                             y = mean_y,
                             label = paste0("**", species, "**",
                                            " (N = ", count, ")",
                                            "<br>Mean weight: ", janitor::round_half_up(mean_weight/1000, 2), "kg")),
                         size = 7,
                         family = "Work Sans",
                         width = unit(20, "lines"),
                         fill = "#FEFDFA",
                         box.colour = NA,
                         alpha = 0.9,
                         halign = 0.5) +
    labs(title = "Flipper lengths are proportional to bill lengths within each species",
         x = "Bill length (mm)",
         y = "Flipper length (mm)",
         colour = "") +
    theme_dt_demo(16) +
    scale_colour_manual(values = colours)
  
}

Parameterisation #1: Different data

Let’s see the original again…

make_penguin_plot() 

Parameterisation #1: Different data

And can we do it with just the first 300 and 400 penguins?

make_penguin_plot(df = head(palmerpenguins::penguins, 
                            300)) 

make_penguin_plot(df = head(palmerpenguins::penguins, 
                            400)) 

Parameterisation #1: Different data

Actually, let’s do it by island…

make_penguin_plot(filter(palmerpenguins::penguins, 
                         island == "Dream")) 

make_penguin_plot(filter(palmerpenguins::penguins, 
                         island == "Biscoe")) 

Parameterisation #2: Different grouping

Parameterisation #2: Different grouping

Rather than grouping by species, can we group by island?

make_penguin_plot <- function(df = palmerpenguins::penguins,
                              colours = penguin_colours) {
  
  mean_x_y <- df |>
    group_by(species) |>
    summarise(mean_x = mean(bill_length_mm, na.rm = TRUE),
              mean_y = mean(flipper_length_mm, na.rm = TRUE),
              mean_weight = mean(body_mass_g, na.rm = TRUE),
              count = length(species))
  
  df |>
    ggplot() +
    geom_point(aes(x = bill_length_mm,
                   y = flipper_length_mm,
                   colour = species,
                   size = body_mass_g),
               size = 5, 
               alpha = 0.9,
               show.legend = FALSE) +
    ggtext::geom_textbox(data = mean_x_y,
                         aes(x = mean_x,
                             y = mean_y,
                             label = paste0("**", species, "**",
                                            " (N = ", count, ")",
                                            "<br>Mean weight: ", janitor::round_half_up(mean_weight/1000, 2), "kg")),
                         size = 7,
                         family = "Work Sans",
                         width = unit(20, "lines"),
                         fill = "#FEFDFA",
                         box.colour = NA,
                         alpha = 0.9,
                         halign = 0.5) +
    labs(title = "Flipper lengths are proportional to bill lengths within each species",
         x = "Bill length (mm)",
         y = "Flipper length (mm)",
         colour = "") +
    theme_dt_demo(16) +
    scale_colour_manual(values = colours)
  
}

Parameterisation #2: Different grouping

This doesn’t work…

make_penguin_plot <- function(df = palmerpenguins::penguins,
                              colours = penguin_colours,
                              grouping_variable = species) {
  
  mean_x_y <- df |>
    group_by(grouping_variable) |>
    summarise(mean_x = mean(bill_length_mm, na.rm = TRUE),
              mean_y = mean(flipper_length_mm, na.rm = TRUE),
              mean_weight = mean(body_mass_g, na.rm = TRUE),
              count = length(grouping_variable))
  
  df |>
    ggplot() +
    geom_point(aes(x = bill_length_mm,
                   y = flipper_length_mm,
                   colour = grouping_variable),
               size = 5, 
               alpha = 0.9,
               show.legend = FALSE) +
    ggtext::geom_textbox(data = mean_x_y,
                         aes(x = mean_x,
                             y = mean_y,
                             label = paste0("**", grouping_variable, "**",
                                            " (N = ", count, ")",
                                            "<br>Mean weight: ", janitor::round_half_up(mean_weight/1000, 2), "kg")),
                         size = 7,
                         family = "Work Sans",
                         width = unit(20, "lines"),
                         fill = "#FEFDFA",
                         box.colour = NA,
                         alpha = 0.9,
                         halign = 0.5) +
    labs(title = paste0("Flipper lengths are proportional to bill lengths within each ",
                        grouping_variable),
         x = "Bill length (mm)",
         y = "Flipper length (mm)",
         colour = "") +
    theme_dt_demo(16) +
    scale_colour_manual(values = colours)
  
}

Parameterisation #2: Different grouping

We need get()!

make_penguin_plot <- function(df = palmerpenguins::penguins,
                              colours = penguin_colours,
                              grouping_variable = "species") {
  
  mean_x_y <- df |>
    group_by(get(grouping_variable)) |>
    summarise(mean_x = mean(bill_length_mm, na.rm = TRUE),
              mean_y = mean(flipper_length_mm, na.rm = TRUE),
              mean_weight = mean(body_mass_g, na.rm = TRUE),
              count = length(get(grouping_variable))) 
  
  df |>
    ggplot() +
    geom_point(aes(x = bill_length_mm,
                   y = flipper_length_mm,
                   colour = get(grouping_variable),
                   size = body_mass_g),
               size = 5, 
               alpha = 0.9,
               show.legend = FALSE) +
    ggtext::geom_textbox(data = mean_x_y,
                         aes(x = mean_x,
                             y = mean_y,
                             label = paste0("**", get(grouping_variable), "**",
                                            " (N = ", count, ")",
                                            "<br>Mean weight: ", janitor::round_half_up(mean_weight/1000, 2), "kg")),
                         size = 7,
                         family = "Work Sans",
                         width = unit(20, "lines"),
                         fill = "#FEFDFA",
                         box.colour = NA,
                         alpha = 0.9,
                         halign = 0.5) +
    labs(title = paste0("Flipper lengths are proportional to bill lengths within each ",
                        grouping_variable),
         x = "Bill length (mm)",
         y = "Flipper length (mm)",
         colour = "") +
    theme_dt_demo(16) +
    scale_colour_manual(values = colours)
  
}

Parameterisation #2: Different grouping

get()? Interpret string into object name, return its value

penguin <- c("Chinstrap", "Gentoo", "Adelie")

get("penguin")
[1] "Chinstrap" "Gentoo"    "Adelie"   

Parameterisation #2: Different grouping

get()? Interpret string as object name, return its value

penguin <- c("Chinstrap", "Gentoo", "Adelie")
favourite_animal <- "penguin"

get(favourite_animal)
[1] "Chinstrap" "Gentoo"    "Adelie"   

Parameterisation #2: Different grouping

Still one thing to fix…

make_penguin_plot()
Error in `ggtext::geom_textbox()`:
  ! Problem while computing aesthetics.
i Error occurred in the 2nd layer.
Caused by error in `get()`:
  ! object 'species' not found

debug(make_penguin_plot)

👀 the first column name…

Browse[2]> mean_x_y
# A tibble: 3 x 5
`get(grouping_variable)` mean_x mean_y mean_weight count
<fct>                     <dbl>  <dbl>       <dbl> <int>
  1 Adelie                     38.8   190.       3701.   152
2 Chinstrap                  48.8   196.       3733.    68
3 Gentoo                     47.5   217.       5076.   124

Parameterisation #2: Different grouping

👀 the first column name…

make_penguin_plot <- function(df = palmerpenguins::penguins,
                              colours = penguin_colours,
                              grouping_variable = "species") {
  
  mean_x_y <- df |>
    group_by(get(grouping_variable)) |>
    summarise(mean_x = mean(bill_length_mm, na.rm = TRUE),
              mean_y = mean(flipper_length_mm, na.rm = TRUE),
              mean_weight = mean(body_mass_g, na.rm = TRUE),
              count = length(get(grouping_variable)))
  
  df |>
    ggplot() +
    geom_point(aes(x = bill_length_mm,
                   y = flipper_length_mm,
                   colour = get(grouping_variable),
                   size = body_mass_g),
               size = 5, 
               alpha = 0.9,
               show.legend = FALSE) +
    ggtext::geom_textbox(data = mean_x_y,
                         aes(x = mean_x,
                             y = mean_y,
                             label = paste0("**", `get(grouping_variable)`, "**",
                                            " (N = ", count, ")",
                                            "<br>Mean weight: ", janitor::round_half_up(mean_weight/1000, 2), "kg")),
                         size = 7,
                         family = "Work Sans",
                         width = unit(20, "lines"),
                         fill = "#FEFDFA",
                         box.colour = NA,
                         alpha = 0.9,
                         halign = 0.5) +
    labs(title = paste0("Flipper lengths are proportional to bill lengths within each ",
                        grouping_variable),
         x = "Bill length (mm)",
         y = "Flipper length (mm)",
         colour = "") +
    theme_dt_demo(16) +
    scale_colour_manual(values = colours)
  
}

Parameterisation #2: Different grouping

Rather than grouping by species, can we group by island?

make_penguin_plot(grouping_variable = "species")

make_penguin_plot(grouping_variable = "island")

Parameterisation #2: Different grouping

We need more colours!

penguin_colours <- c(Adelie = "pink",
                     Chinstrap = "orange",
                     Gentoo = "darkgreen",
                     Biscoe = "steelblue",
                     Torgersen = "turquoise",
                     Dream = "skyblue",
                     male = "#2C3D4F",
                     female = "#28A569")

Parameterisation #2: Different grouping

Actually, can we just look at a few different grouping options?

make_penguin_plot(grouping_variable = "island")

make_penguin_plot(grouping_variable = "sex")

Parameterisation #2: Different grouping

Let’s make the labels optional!

make_penguin_plot <- function(df = palmerpenguins::penguins,
                              colours = penguin_colours,
                              grouping_variable = "species",
                              add_labels = TRUE) {
  
  mean_x_y <- df |>
    group_by(get(grouping_variable)) |>
    summarise(mean_x = mean(bill_length_mm, na.rm = TRUE),
              mean_y = mean(flipper_length_mm, na.rm = TRUE),
              mean_weight = mean(body_mass_g, na.rm = TRUE),
              count = length(get(grouping_variable)))
  
  unlabelled_plot <- df |>
    ggplot() +
    geom_point(aes(x = bill_length_mm,
                   y = flipper_length_mm,
                   colour = get(grouping_variable),
                   size = body_mass_g),
               size = 5, 
               alpha = 0.9) +
    # Reinstated the legend
    labs(title = paste0("Flipper lengths are proportional to bill lengths within each ",
                        grouping_variable),
         x = "Bill length (mm)",
         y = "Flipper length (mm)",
         colour = "") +
    theme_dt_demo(16) +
    scale_colour_manual(values = colours)
  
  if(add_labels == TRUE) {
    unlabelled_plot + 
      ggtext::geom_textbox(data = mean_x_y,
                           aes(x = mean_x,
                               y = mean_y,
                               label = paste0("**", `get(grouping_variable)`, "**",
                                              " (N = ", count, ")",
                                              "<br>Mean weight: ", janitor::round_half_up(mean_weight/1000, 2), "kg")),
                           size = 7,
                           family = "Work Sans",
                           width = unit(20, "lines"),
                           fill = "#FEFDFA",
                           box.colour = NA,
                           alpha = 0.9,
                           halign = 0.5) +
      theme(legend.position = "none")
  } else {
    unlabelled_plot
  }
  
}

Parameterisation #2: Different grouping

Actually, can we just look at a few different grouping options?

make_penguin_plot(grouping_variable = "island", 
                  add_labels = FALSE)

make_penguin_plot()

Parameterisation #2: Different grouping

Ok, now split by island within each sex.

make_penguin_plot(df = filter(palmerpenguins::penguins,
                              sex == "female"),
                  grouping_variable = "island", 
                  add_labels = FALSE)

make_penguin_plot(df = filter(palmerpenguins::penguins,
                              sex == "male"),
                  grouping_variable = "island", 
                  add_labels = FALSE)

Parameterisation #3: Different styling

Parameterisation #3: Different styling

Switching off the labels

make_penguin_plot()

make_penguin_plot(add_labels = FALSE)

Parameterisation #3: Different styling

Add an optional subtitle

make_penguin_plot <- function(df = palmerpenguins::penguins,
                              colours = penguin_colours,
                              grouping_variable = "species",
                              subtitle = NA,
                              add_labels = TRUE) {
  
  mean_x_y <- df |>
    group_by(get(grouping_variable)) |>
    summarise(mean_x = mean(bill_length_mm, na.rm = TRUE),
              mean_y = mean(flipper_length_mm, na.rm = TRUE),
              mean_weight = mean(body_mass_g, na.rm = TRUE),
              count = length(get(grouping_variable)))
  
  unlabelled_plot <- df |>
    ggplot() +
    geom_point(aes(x = bill_length_mm,
                   y = flipper_length_mm,
                   colour = get(grouping_variable),
                   size = body_mass_g),
               size = 5, 
               alpha = 0.9) +
    # Reinstated the legend
    labs(title = paste0("Flipper lengths are proportional to bill lengths within each ",
                        grouping_variable),
         x = "Bill length (mm)",
         y = "Flipper length (mm)",
         colour = "") +
    theme_dt_demo(16) +
    scale_colour_manual(values = colours)
  
  if(add_labels == TRUE) {
    plot_to_export <- unlabelled_plot + 
      ggtext::geom_textbox(data = mean_x_y,
                           aes(x = mean_x,
                               y = mean_y,
                               label = paste0("**", `get(grouping_variable)`, "**",
                                              " (N = ", count, ")",
                                              "<br>Mean weight: ", janitor::round_half_up(mean_weight/1000, 2), "kg")),
                           size = 7,
                           family = "Work Sans",
                           width = unit(20, "lines"),
                           fill = "#FEFDFA",
                           box.colour = NA,
                           alpha = 0.9,
                           halign = 0.5) +
      theme(legend.position = "none")
  } else {
    plot_to_export <- unlabelled_plot
  }
  
  if(!is.na(subtitle)) {
    plot_to_export +
      labs(subtitle = subtitle)
  } else {
    plot_to_export 
  }
  
}

Parameterisation #3: Different styling

Add optional subtitle

make_penguin_plot()

make_penguin_plot(subtitle = "There are always some exceptions!")

Parameterisation #3: Different styling

The magic of ...

make_penguin_plot <- function(df = palmerpenguins::penguins,
                              colours = penguin_colours,
                              grouping_variable = "species",
                              subtitle = NA,
                              add_labels = TRUE,
                              ...) {
  
  mean_x_y <- df |>
    group_by(get(grouping_variable)) |>
    summarise(mean_x = mean(bill_length_mm, na.rm = TRUE),
              mean_y = mean(flipper_length_mm, na.rm = TRUE),
              mean_weight = mean(body_mass_g, na.rm = TRUE),
              count = length(get(grouping_variable)))
  
  unlabelled_plot <- df |>
    ggplot() +
    geom_point(aes(x = bill_length_mm,
                   y = flipper_length_mm,
                   colour = get(grouping_variable),
                   size = body_mass_g),
               size = 5, 
               alpha = 0.9) +
    # Reinstated the legend
    labs(title = paste0("Flipper lengths are proportional to bill lengths within each ",
                        grouping_variable),
         x = "Bill length (mm)",
         y = "Flipper length (mm)",
         colour = "") +
    theme_dt_demo(16) +
    scale_colour_manual(values = colours) +
    theme(...)
  
  if(add_labels == TRUE) {
    
    plot_to_export <- unlabelled_plot + 
      ggtext::geom_textbox(data = mean_x_y,
                           aes(x = mean_x,
                               y = mean_y,
                               label = paste0("**", `get(grouping_variable)`, "**",
                                              " (N = ", count, ")",
                                              "<br>Mean weight: ", janitor::round_half_up(mean_weight/1000, 2), "kg")),
                           size = 7,
                           family = "Work Sans",
                           width = unit(20, "lines"),
                           fill = "#FEFDFA",
                           box.colour = NA,
                           alpha = 0.9,
                           halign = 0.5) +
      theme(legend.position = "none")
    
  } else {
    
    plot_to_export <- unlabelled_plot
    
  }
  
  if(!is.na(subtitle)) {
    
    plot_to_export +
      labs(subtitle = subtitle)
    
  } else {
    
    plot_to_export 
    
  }
  
}

Parameterisation #3: Different styling

The magic of ...

make_penguin_plot(axis.title.x = element_text(size = 60, 
                                              colour = "purple"))

make_penguin_plot(plot.background = element_rect(colour = "red", 
                                                 size = 5))

Parameterisation #3: Different styling

A more sensible use case…

make_penguin_plot(df = filter(palmerpenguins::penguins, sex == "female"),
                  grouping_variable = "island",
                  subtitle = "Among female penguins...",
                  add_labels = FALSE,
                  plot.background = element_rect(colour = penguin_colours[["female"]], size = 5))

make_penguin_plot(df = filter(palmerpenguins::penguins, sex == "male"),
                  grouping_variable = "island",
                  add_labels = FALSE,
                  subtitle = "...and among male penguins",
                  plot.background = element_rect(colour = penguin_colours[["male"]], size = 5))

Give it a go!

cararthompson.com/talks/parameterise