map
fauna
Author

Ana Luisa Bodevan

Published

September 2, 2025

Code
############## TIDYTUESDAY WEEK 35
############## AUSTRALIAN FROGS 

############## 1. SETUP

library(pacman)

pacman :: p_load(tidyverse, dplyr, showtext, ggtext, sf, rnaturalearth,
                 rnaturalearthdata, RColorBrewer, ggshadow, viridis)

frogID_data <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2025/2025-09-02/frogID_data.csv')
frog_names <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2025/2025-09-02/frog_names.csv')

############## 2. DATA ANALYSIS AND TIDYING 

# Get Australia outline 
aus_outline <- ne_countries(country = "australia", returnclass = "sf", scale = "medium")

# Create combined dataset 
frog_combined <- frogID_data %>%
  left_join(frog_names, by = "scientificName") %>%
  filter(!is.na(decimalLatitude) & !is.na(decimalLongitude))

# Convert frog data to spatial points
frog_sf <- frog_combined %>%
  st_as_sf(coords = c("decimalLongitude", "decimalLatitude"), crs = 4326)

frog_with_states <- st_join(frog_sf, aus_outline)

# Extract coordinates back and combine with state info
frog_spatial_df <- frog_with_states %>%
  mutate(
    longitude = st_coordinates(.)[,1],
    latitude = st_coordinates(.)[,2]
  ) %>%
  st_drop_geometry() %>% 
  filter(!is.na(name))

frog_richness <- frog_spatial_df %>%
  group_by(longitude = round(longitude, 1),
           latitude = round(latitude, 1)) %>%
  summarise(species_richness = n_distinct(commonName), .groups = "drop")


############## 3. PLOT 

# Load fonts 
font_add_google("Ubuntu", "ubuntu")
font_add_google("Fredoka One", "fredoka")
showtext_auto()

# Step 1. Give hexagons an ID
hex_grid <- st_make_grid(
  aus_outline,
  cellsize = 1.5,    # adjust for hex size
  square = FALSE
) %>%
  st_intersection(aus_outline) %>%
  st_sf() %>%
  mutate(hex_id = row_number())   # <-- unique ID for each hex

# Step 2. Assign each frog record to a hex
frog_hex <- st_join(frog_sf, hex_grid, join = st_within)

# Step 3. Calculate species richness per hex
hex_richness <- frog_hex %>%
  st_drop_geometry() %>%
  group_by(hex_id) %>%
  summarise(species_richness = n_distinct(commonName)) %>%
  left_join(hex_grid, by = "hex_id") %>%
  st_as_sf()

ggplot() +
  geom_sf(data = aus_outline, fill = "#fef9ef", color = "white") +
  geom_sf(data = hex_richness,
          aes(fill = species_richness), color = "white", size = 0.2) +
  scale_fill_viridis(option = "D", name = "Frog diversity", direction = -1) +
  coord_sf() +
  labs(
    title = "🐸 Frogs of Australia",
    subtitle = "Frog Species Diversity across Australia",
    caption = "Data: TidyTuesday 2025 • Viz: @anabodevan"
  ) +
  theme_void() +
  theme(
    plot.title = element_text(family = "fredoka", size = 26, face = "bold", hjust = 0.5),
    plot.subtitle = element_text(family = "ubuntu", size = 20, hjust = 0.5),
    plot.caption = element_text(family = "ubuntu", size = 14),
    plot.background = element_rect(fill = "white", color = NA), 
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    legend.position = "bottom",
    legend.title = element_text(family = "ubuntu", size = 12, face = "bold"),
    legend.text = element_text(family = "ubuntu", size = 10),
    legend.margin = margin(t = -45, b = 0, unit = "pt"),       
    legend.box.margin = margin(t = -45, unit = "pt")           
  )