Revisiting tidyverse

Data visualization challenge

Two weeks ago, we introduced a number of packages within the R “Tidyverse”

  • Many of your semester project analyses seem to make use of these packages

  • Challenge today is to apply these packages to an ecological dataset

  • Goal: Use packages from the tidyverse to create a graph showing the abundance distributions of birds in University Lake near LSU.

  • Note: In addition to the core tidyverse packages, I used the package glue (but it is not necessary)

To acquire the dataset, please use the following command:

ebird_out <- readRDS(url("https://gitlab.com/gklab/teaching/reproducible-research-f25/-/raw/main/11-tidyverse/ebird_dat.rds"))

(I previously acquired this dataset using the rebirdpackage, which interacts with the eBird API – see “databases” lecture):

ebird_out <-
  crossing(year = c(2023,2024), months = 1:12, date = 1:28) |>
  mutate(months = str_pad(months, 2, pad = "0"), date = str_pad(date, 2, pad = "0")) |>
  mutate(date_format = glue("{year}-{months}-{date}")) |>
  mutate(ebird_out = map(date_format, \(x)
                         ebirdhistorical(loc = 'L841875',
                                         date = x)))

Your goal is to create the plot on the following slide.

Important attributes:

  • Each panel is 1 month in 2023
  • Each panel shows the abundance of the 20 most abundant species recorded that month at University Lakes
  • Each panel also shows the total number of species, as part of the title

If you complete this challenge…

Modify this code to also show Shannon diversity for each month in the title.

Recall that Shannon diversity is calculated as:

\[H' = -\sum_{i=1}^{S} p_i*\text{ln}(p_i)\]

Hint: the following function returns the Shannon diversity for a dataframe (assuming the dataframe has a column named avg_obs). You are welcome to use it in your solution.

calculate_shannon <- function(df) {
  obs_vec <- df$avg_obs
  total_obs = sum(obs_vec)
  pi = obs_vec/total_obs
  log_pi = log(pi)
  round(-1*(sum(pi*log_pi)), 2)
}

Exercises

Lots of resources to learn more: