Skip to content
R for the Rest of Us Logo

R in 3 Months (Spring 2026)

Week 4 Coworking Session (Spring 2026)

During this coworking session, you will, in pairs, give feedback, comment and debug the code below. Feel free to take some time before the session to test the code and jot down some initial thoughts on the script.

# Load packages ----------------------------------------------------------

library(tidyverse)
library(tidytuesdayR)
library(gridpattern)

# Load data --------------------------------------------------------------

data_raw <- tidytuesdayR::tt_load("2026-03-31")$ocean_temperature

# Data wrangling ---------------------------------------------------------

# Make sensor depth at low tide a factor for better plotting
data_clean <- data_raw |>
  mutate(
    sensor_depth_at_low_tide_m = as.factor(sensor_depth_at_low_tide_m),
    n_obs = as.integer(n_obs)
  )

# Calculate mean temperature by day

data_daily <- data_clean |>
  group_by(date, sensor_depth_at_low_tide_m) |>
  summarise(mean_temperature = mean(mean_temperature_degree_c, na.rm = TRUE))

# Calculate mean temperature per sensor depth at low tide

data_clean |>
  summarise(mean_temperature = mean(mean_temperature_degree_c, na.rm = TRUE)) |>
  arrange(sensor_depth_at_low_tide_m)

data_monthly_plot <- data_daily |>
  group_by(
    month = as.factor(lubridate::month(date, label = TRUE, abbr = TRUE)),
    sensor_depth_at_low_tide_m
  ) |>
  summarise(mean_temperature = mean(mean_temperature, na.rm = TRUE)) |>
  mutate(
    line_color = colorRampPalette(c("#ffffff", "#30a2ed"))(n_distinct(
      sensor_depth_at_low_tide_m
    ))
  ) |>
  ungroup()

# Plotting ----------------------------------------------------------------

ggplot(
  data_monthly_plot,
  aes(x = sensor_depth_at_low_tide_m, y = mean_temperature)
) +
  geom_boxplot(
    data = data_monthly_plot,
    aes(group = sensor_depth_at_low_tide_m),
    fill = "#30a2ed",
    color = "#61b8f3"
  ) +
  geom_label(
    data = data_clean |>
      group_by(sensor_depth_at_low_tide_m) |>
      summarise(n_obs = sum(n_obs)),
    aes(y = 200, label = paste0("n = ", n_obs)),
    position = position_nudge(y = 0.5),
    size = 30,
    color = "#ffffff",
    fill = "#61b8f3",
    fontface = "italic"
  ) +
  labs(
    title = "Mean Ocean Temperature by Sensor Depth at Low Tide",
    x = "Mean Temperature (°C)",
    y = "Sensor Depth at Low Tide (m)"
  ) +
  theme_minimal()

Have any questions? Put them below and we will help you out!

You need to be signed-in to comment on this post. Login.

Course Content

144 Lessons