Tasks 1 & 2: Interactive plots

Exercise 11 — PMAP 8551/4551, Fall 2025

Author

Andrew Heiss

Published

November 3, 2025

Task 1: Session check-in

TipResponses

You’ll put your check-in here in your copy of this .qmd file.

Task 2: Interactive plot

The code below loads and cleans the data. You don’t need to modify anything in this chunk of code—you only need to run it.

library(tidyverse)
library(plotly)

# Technically this isn't necessary; you can run `penguins` and start using it 
# right away since it's a core dataset. This code just puts the dataset in the 
# environment panel so it's easier to see what's in there.
penguins <- datasets::penguins

# 11 rows are missing data on penguin sex, so we drop them
penguins <- penguins |> 
  drop_na(sex)

Recreate plot

Use ggplot() and ggplotly() to recreate this interactive plot:

Some hints!

  • Don’t try to make this interactive right away! Just like in the example, first make a regular static version before feeding it into ggplotly().

  • The points are colored with three of R’s built-in colors: darkorange, purple, and cyan4. Use scale_color_manual() to change the default colors to these (using something like values = c("darkorange", "purple", "cyan4")).

  • The points are a little bigger than the default size. Use size = BLAH in geom_point() to adjust them (but make sure you don’t use that inside aes()!).

  • Notice that the points use different shapes for each species as well as color!

  • The plot uses theme_light().

  • The interactive tooltip should be structured like this:

    Species (sex on island)
    Bill depth: X mm
    Bill length: X mm

    You’ll need to make a new column in the penguins dataset named label (or whatever you want to call it) that builds that label using that template. The example will be useful.

    In the example, I use paste0() to build the label text, like this:

    penguins |> 
      mutate(fancy_label = paste0(species, " (", sex, " on ", island, " Island)")) |> 
      select(fancy_label) |> head(2)
                              fancy_label
    1   Adelie (male on Torgersen Island)
    2 Adelie (female on Torgersen Island)

    …and that works, but it’s super annoying to switch between variable names and text and to keep track of where spaces need to be in the text.

    To make life easier, try out the {glue} package, which is installed as part of the tidyverse, but not loaded with library(tidyverse). {glue} lets you substitute variable values directly in text without needing to separate everything with commas. Anything inside curly braces {} will get replaced with the value in the data:

    library(glue)
    
    penguins |> 
      mutate(fancy_label = glue("{species} ({sex} on {island} Island)")) |> 
      select(fancy_label) |> head(2)
                              fancy_label
    1   Adelie (male on Torgersen Island)
    2 Adelie (female on Torgersen Island)