# Loading packages -------------------------------------------------------------
pacman::p_load(
tidyverse, # Easily Install and Load the 'Tidyverse'
ggtext, # Improved Text Rendering Support for 'ggplot2'
showtext, # Using Fonts More Easily in R Graphs
ggeasy, # Makes theming plots easier
glue, # Interpreted String Literals
ggfx # Pixel Filters for "ggplot2" and "grid"
)
# Visualization Parameters -----------------------------------------------------
# Plot aesthetics
title_col <- "gray20"
subtitle_col <- "gray20"
caption_col <- "gray30"
text_col <- "gray20"
# Icons
tt <- str_glue("#TidyTuesday: { 2024 } Week { 41 } • Source: National Park Species<br>")
li <- str_glue("<span style='font-family:fa6-brands'></span>")
gh <- str_glue("<span style='font-family:fa6-brands'></span>")
# Text
title_text <- str_glue("Abundance of birds per national park")
caption_text <- str_glue("{tt} {li} c-monaghan • {gh} c-monaghan • #rstats #ggplot2")
# Fonts
font_add("fa6-brands", here::here("fonts/6.4.2/Font Awesome 6 Brands-Regular-400.otf"))
font_add_google("Oswald", regular.wt = 400, family = "title")
font_add_google("Noto Sans", regular.wt = 400, family = "caption")
font_add_google("Merriweather Sans", regular.wt = 400, family = "text")
showtext_auto(enable = TRUE)
# Theme
theme_set(theme_minimal(base_size = 14))
theme_update(
plot.title.position = "plot",
plot.caption.position = "plot",
panel.background = element_rect(fill = "white", color = "white"),
panel.grid = element_blank(),
panel.grid.major.x = element_blank(),
axis.text.x = element_text(size = 12, family = "text"),
axis.text.y = element_blank(),
legend.position = "bottom"
)
# Variables --------------------------------------------------------------------
# Paths
path <- "posts/2024/"
folder <- "2024-10-07-TT-W41/"
# Reading in data --------------------------------------------------------------
data <- tidytuesdayR::tt_load(2024, week = 41)
species <- data$most_visited_nps_species_data
This document analyzes a dataset of species from the 15 most visited National Parks in the USA provided by #TidyTuesday. The main focus of this analysis will be on birds.
Setting up
Species data
The dataset includes various information on animal and plant species from 15 of the most visited parks in the USA. In particular, we are interested in the abundance of birds in each park.
Calculating abundance
bird_abundance <- species %>%
select(ParkName, CategoryName) %>%
group_by(ParkName, CategoryName) %>%
summarise(Abundance = n()) %>%
rename(Park_name = ParkName, Species = CategoryName) %>%
mutate(Park_name = as.factor(Park_name)) %>%
filter(Species == "Bird")
bird_abundance %>%
head()
# A tibble: 6 × 3
# Groups: Park_name [6]
Park_name Species Abundance
<fct> <chr> <int>
1 Acadia National Park Bird 364
2 Bryce Canyon National Park Bird 218
3 Cuyahoga Valley National Park Bird 246
4 Glacier National Park Bird 277
5 Grand Canyon National Park Bird 456
6 Grand Teton National Park Bird 266
Visualising bird abundance
bird_abundance_plot <- bird_abundance %>%
ggplot() +
# Add horizontal reference lines at intervals of 125
geom_hline(
data = data.frame(y = c(0:4) * 125),
aes(yintercept = y),
color = "lightgrey") +
# Create a bar plot in polar coordinates
geom_col(
aes(x = reorder(str_wrap(Park_name, 16), Abundance),
y = Abundance,
fill = Abundance),
position = "dodge2", show.legend = TRUE, alpha = 0.9) +
# Use polar coordinates
coord_polar() +
# Set y-axis limits, expand to control padding, and custom breaks for reference lines
scale_y_continuous(
limits = c(-200, 500),
expand = c(0, 0),
breaks = c(0, 100, 200, 300, 400)
) +
# Apply a color gradient for the bars based on abundance levels
scale_fill_gradientn(
"Abundance", # Title
colours = c( "#e9b91c","#db9a17","#ce7b12","#be471b", "#ae1324") # Custom colours
) +
# Customize the legend
guides(
fill = guide_colorsteps(
barwidth = 15,
barheight = 1,
title.position = "top",
title.hjust = .5
)) +
# Add titles and labels
labs(
title = title_text,
caption = caption_text,
x = NULL,
y = NULL) +
# Apply theme customization
theme(
# Title
plot.title = element_text(
size = rel(5),
family = "title",
face = "bold",
colour = title_col,
lineheight = 1.1,
hjust = 0.5,
margin = margin(t = 5, b = 5)),
# Caption
plot.caption = element_markdown(
size = rel(1.25),
family = "caption",
colour = caption_col,
lineheight = 1.1,
hjust = 0.5,
margin = margin(t = 5, b = 5)),
legend.title = element_text(size = 20),
legend.text = element_text(size = 15)
)
bird_abundance_plot
Saving
# Saving plot
ggsave(
filename = here::here(path, folder, "tt_2024_w41.png"),
plot = bird_abundance_plot,
width = 9,
height = 6,
units = "in",
dpi = 320
)
# Thumbnail
magick::image_read(here::here(path, folder, "tt_2024_w41.png")) |>
magick::image_resize(geometry = "800") |>
magick::image_write(here::here(path, "thumbnails/tt_2024_w41_thumb.png"))
Appendix
Expand for Session Info
─ Session info ───────────────────────────────────────────────────────────────
setting value
version R version 4.4.1 (2024-06-14 ucrt)
os Windows 11 x64 (build 22631)
system x86_64, mingw32
ui RTerm
language (EN)
collate English_Ireland.utf8
ctype English_Ireland.utf8
tz Europe/Dublin
date 2024-10-09
pandoc 3.2 @ C:/Coding/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)
quarto 1.5.57
─ Packages ───────────────────────────────────────────────────────────────────
package * version date (UTC) lib source
dplyr * 1.1.4 2023-11-17 [1] CRAN (R 4.4.1)
forcats * 1.0.0 2023-01-29 [1] CRAN (R 4.4.1)
ggeasy * 0.1.4 2023-03-12 [1] CRAN (R 4.4.1)
ggfx * 1.0.1 2022-08-22 [1] CRAN (R 4.4.1)
ggplot2 * 3.5.1 2024-04-23 [1] CRAN (R 4.4.1)
ggtext * 0.1.2 2022-09-16 [1] CRAN (R 4.4.1)
glue * 1.7.0 2024-01-09 [1] CRAN (R 4.4.1)
lubridate * 1.9.3 2023-09-27 [1] CRAN (R 4.4.1)
purrr * 1.0.2 2023-08-10 [1] CRAN (R 4.4.1)
readr * 2.1.5 2024-01-10 [1] CRAN (R 4.4.1)
sessioninfo * 1.2.2 2021-12-06 [1] CRAN (R 4.4.1)
showtext * 0.9-7 2024-03-02 [1] CRAN (R 4.4.1)
showtextdb * 3.0 2020-06-04 [1] CRAN (R 4.4.1)
stringr * 1.5.1 2023-11-14 [1] CRAN (R 4.4.1)
sysfonts * 0.8.9 2024-03-02 [1] CRAN (R 4.4.1)
tibble * 3.2.1 2023-03-20 [1] CRAN (R 4.4.1)
tidyr * 1.3.1 2024-01-24 [1] CRAN (R 4.4.1)
tidyverse * 2.0.0 2023-02-22 [1] CRAN (R 4.4.1)
[1] C:/Coding/R-4.4.1/library
──────────────────────────────────────────────────────────────────────────────
Citation
BibTeX citation:
@online{monaghan2024,
author = {Monaghan, Cormac},
title = {Bird {Abundance}},
date = {2024-10-08},
url = {https://c-monaghan.github.io/posts/2024/2024-10-07-TT-W41/},
langid = {en}
}
For attribution, please cite this work as:
Monaghan, Cormac. 2024. “Bird Abundance .” October 8, 2024.
https://c-monaghan.github.io/posts/2024/2024-10-07-TT-W41/.