Mapping the paths of Southern Resident killer whale encounters highlights their concentrated movements within key areas of the Salish Sea.
#TidyTuesday
Maps
{ggmap}
Author
Aditya Dahiya
Published
October 16, 2024
This week’s Tidy Tuesday dataset comes from the Center for Whale Research (CWR), which monitors Southern Resident killer whales in the Salish Sea, part of the Pacific Northwest. The dataset, scraped by Jadey Ryan and documented here, contains information on encounters from 2017 to 2024. Each encounter involves photographing and identifying individual whales. The data can be accessed via the {orcas} R package and includes variables like encounter duration, location, and pod. While the dataset is mostly tidy, some inconsistencies such as missing values and negative durations remain. |Source|Data
Loading required libraries, data import & creating custom functions.
Code
# Data Import and Wrangling Toolslibrary(tidyverse) # All things tidylibrary(janitor) # Cleaning names etc.library(here) # Root Directory Managementlibrary(dataverse) # Getting data from Harvard Dataverse# Final plot toolslibrary(scales) # Nice Scales for ggplot2library(fontawesome) # Icons display in ggplot2library(ggtext) # Markdown text support for ggplot2library(showtext) # Display fonts in ggplot2library(colorspace) # Lighten and Darken colourslibrary(patchwork) # Combining plotslibrary(ggmap) # To get background map tiles# Option 1: Loading data directly from GitHuborcas <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2024/2024-10-15/orcas.csv')
Visualization Parameters
Code
# Font for titlesfont_add_google("Lato",family ="title_font") # Font for the captionfont_add_google("Saira Extra Condensed",family ="caption_font") # Font for plot textfont_add_google("Oswald",family ="body_font") showtext_auto()# Background Colourbg_col <-"white"# Colour for the texttext_col <- colorspace::darken("#3d545e", 0.2) # Colour for highlighted texttext_hil <- colorspace::darken("#3d545e", 0.2) # Define Base Text Sizebts <-90# Caption stuff for the plotsysfonts::font_add(family ="Font Awesome 6 Brands",regular = here::here("docs", "Font Awesome 6 Brands-Regular-400.otf"))github <-""github_username <-"aditya-dahiya"xtwitter <-""xtwitter_username <-"@adityadahiyaias"social_caption_1 <- glue::glue("<span style='font-family:\"Font Awesome 6 Brands\";'>{github};</span> <span style='color: {text_hil}'>{github_username} </span>")social_caption_2 <- glue::glue("<span style='font-family:\"Font Awesome 6 Brands\";'>{xtwitter};</span> <span style='color: {text_hil}'>{xtwitter_username}</span>")plot_title <-"Tracing Encounters: Orcas in the Salish Sea"plot_caption <-paste0("**Data:** Center for Whale Research (CWR)", " | **Code:** ", social_caption_1, " | **Graphics:** ", social_caption_2 )subtitle_text <-"Arrows trace the journey of each encounter with a Southern Resident killer whale, offering a visual exploration of where these whales are most frequently observed. The map shows a dense cluster of sightings, underscoring the importance of specific areas in the Salish Sea."plot_subtitle <-str_wrap(subtitle_text, width =95)plot_subtitle |>str_view()rm(github, github_username, xtwitter, xtwitter_username, social_caption_1, social_caption_2)
Exploratory Data Analysis and Wrangling
Code
# summarytools::dfSummary(orcas) |> # summarytools::view()# Manually drop the erratic ids by looking at the mapid_to_filter <-c(7, 221, 167, 499, 152, 8, 21, 157, 56, 471, 193, 419, 562)df <- orcas |># Only keep relevant variablesselect( duration, begin_latitude, begin_longitude, end_latitude, end_longitude, location ) |>mutate(# remove parenthesis content from durationduration =str_remove(duration, "\\s*\\(.*\\)"),# remove the "s" for secondsduration =str_extract(duration, "-?\\d+"),# convert the duration into numberduration =as.numeric(duration) ) |># remove aberrant durationsfilter(duration >=0) |>filter(!( (begin_latitude == end_latitude) & (begin_longitude == end_longitude) ) ) |>mutate(id =row_number() ) |>filter(!(id %in% id_to_filter))# Data needed to make a Static Graphic with ggmaplibrary(ggmap)# Register your Stadia Maps keystadia_maps_api_keyregister_stadiamaps(stadia_maps_api_key, write =FALSE)background_tiles_bbox <-c(left =min(c(df$begin_longitude, df$end_longitude), na.rm = T) -0.2,right =max(c(df$begin_longitude, df$end_longitude), na.rm = T) +0.2,top =max(c(df$begin_latitude, df$end_latitude), na.rm = T),bottom =min(c(df$begin_latitude, df$end_latitude), na.rm = T) -0.1)background_tiles_bbox <-c(left =min(c(df$begin_longitude, df$end_longitude), na.rm = T) -0.2,right =-122.2,top =max(c(df$begin_latitude, df$end_latitude), na.rm = T),bottom =min(c(df$begin_latitude, df$end_latitude), na.rm = T) -0.1)stamen_tiles_lowres <- ggmap::get_stadiamap( background_tiles_bbox,zoom =9,maptype ="stamen_terrain_background")# stamen_tiles_10 <- ggmap::get_stadiamap(# background_tiles_bbox,# zoom = 11,# maptype = "stamen_terrain_background"# )object.size(stamen_tiles_lowres) |>print(units ="Mb")
# Saving a thumbnaillibrary(magick)# Saving a thumbnail for the webpageimage_read(here::here("data_vizs", "tidy_orcas_encounters.png")) |>image_resize(geometry ="400") |>image_write( here::here("data_vizs", "thumbnails", "tidy_orcas_encounters.png" ) )
Session Info
Code
# Data Import and Wrangling Toolslibrary(tidyverse) # All things tidylibrary(here) # Root Directory Management# Final plot toolslibrary(scales) # Nice Scales for ggplot2library(fontawesome) # Icons display in ggplot2library(ggtext) # Markdown text support for ggplot2library(showtext) # Display fonts in ggplot2library(colorspace) # Lighten and Darken colourslibrary(patchwork) # Combining plotslibrary(ggmap) # To get background map tilessessioninfo::session_info()$packages |>as_tibble() |>select(package, version = loadedversion, date, source) |>arrange(package) |> janitor::clean_names(case ="title" ) |> gt::gt() |> gt::opt_interactive(use_search =TRUE ) |> gtExtras::gt_theme_espn()