Skip to content

Instantly share code, notes, and snippets.

@PietrH
Created October 16, 2025 13:04
Show Gist options
  • Save PietrH/0f544d4242667383160b98772dafe47b to your computer and use it in GitHub Desktop.
Save PietrH/0f544d4242667383160b98772dafe47b to your computer and use it in GitHub Desktop.
Occurrences of Passerines in South America, 3 most common genera
# Passerines in South America
# Data read from GBIF occurrence download (SQL QUERY)
pas_sa <-
rgbif::occ_download_get("0028483-251009101135966") |>
rgbif::occ_download_import() |>
dplyr::filter(!is.na(month))
# Identify the 3 most common genera -------------------------------------
common_genera <- pas_sa |>
dplyr::group_by(genuskey) |>
dplyr::summarise(n = sum(`COUNT(*)`)) |>
dplyr::slice_max(order_by = n, n = 3) |>
dplyr::mutate(genus = purrr::map_chr(
genuskey,
\(genuskey) {
rgbif::name_usage(genuskey)$data$canonicalName
},
.progress = TRUE
)) |>
dplyr::select(-n)
# Add labels to data
pas_common <-
pas_sa |>
dplyr::mutate(
month_label =
purrr::map_chr(month, ~ as.character(lubridate::month(.x, label = TRUE, locale = "en_US.UTF-8")))
) |>
dplyr::right_join(common_genera, by = dplyr::join_by("genuskey"))
# labels for species
species_labels <- pas_common |>
dplyr::distinct(specieskey) |>
dplyr::mutate(species = purrr::map_chr(
specieskey,
\(specieskey) {
rgbif::name_usage(specieskey)$data$canonicalName
},
.progress = TRUE
))
pas_common <- pas_common |>
dplyr::left_join(species_labels, by = dplyr::join_by("specieskey"))
# Plot the 3 most common genera -------------------------------------------
library(ggplot2)
ggplot(pas_common) +
aes(x = month_label, y = `COUNT(*)`, fill = species, group = species) +
geom_col() +
scale_fill_hue(direction = 1) +
theme_minimal() +
facet_wrap(vars(genus)) +
labs(
title = "Monthly Observations of Common Passerine Species in South America",
subtitle = "3 most common genera",
x = "Month",
y = "Number of Observations",
fill = "Species"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment