knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE)
# upload packages
library(tidyverse)
library(janitor)
library(here)
library(readxl)
library(gghighlight)
library(lubridate)
library(plotly)
library(patchwork)
# Read in data
sierra_amphibians <- read_excel("sierra_amphibians.xlsx")

Part 1: graph of total mountain yellow-legged frog (Rana muscosa) count each year across all water bodies, by life stage excluding the ‘EggMass’ level.

# filter for only RAMU adults, subadults, and tadpoles & extract the year 
ramu <- sierra_amphibians %>% 
  filter(amphibian_species == "RAMU") %>% 
  filter(amphibian_life_stage %in% c("SubAdult", "Adult", "Tadpole")) %>% 
  mutate(record_year = year(survey_date))

# get RAMU counts by life stage and year
ramu_totals <- ramu %>% 
  select(amphibian_species, amphibian_life_stage, record_year, amphibian_number) %>% 
  group_by(record_year, amphibian_life_stage) %>% 
  tally(amphibian_number)

# Make a graph
ramu_graph <- ggplot(data = ramu_totals, aes(x = record_year, y = n)) +
  geom_point(aes(color = amphibian_life_stage)) +
  geom_line(aes(color = amphibian_life_stage)) +
  theme_minimal() +
  labs(x = "Year",
       y = "Total Count") +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(plot.subtitle = element_text(hjust = 0.5)) + 
  scale_color_discrete(name = "Life Stage")
  
ramu_graph

Part 2: A column graph containing total counts (over all years of the study) of combined adult and subadult endangered mountain yellow-legged frogs (Rana muscosa) observed in the 5 lakes with the greatest total observed counts.

# Combine adult and subadult RAMU counts
ramu_adult_subadult <- ramu %>% 
  filter(amphibian_life_stage %in% c("Adult", "SubAdult"))

# Total counts of RAMU adults/subadults by lake_id
ramu_lake_totals <- ramu_adult_subadult %>% 
 select(lake_id, amphibian_number) %>% 
  group_by(lake_id) %>% 
  tally(amphibian_number)

# Sort in descending order to find top 5 lakes
lake_sorted <- ramu_lake_totals[order(-ramu_lake_totals$n),]

# create a data frame with only the top 5 lakes listed
top_5_lakes <- lake_sorted %>% 
  head(5) 

# rename the lake ids
top_5_lakes <- top_5_lakes %>% 
  mutate(lake_name = "lake") %>% 
  relocate(lake_name) %>% 
  unite("id_new", lake_name:lake_id, sep ="_") %>% 
  rename(total_count = n)

# Make a graph
top_5_graph <- ggplot(data = top_5_lakes, aes(x = fct_reorder(id_new, id_new), y=total_count)) +
  geom_col(color = "lightblue", fill = "lightblue") +
  theme_minimal() +
  labs(x = "Lake ID",
       y = "Total Count") +
  theme(plot.title = element_text(hjust = 0.5))

top_5_graph

Part 3: Combine your two graphs into a single compound figure using the {patchwork} package.

# Use 'patchwork' to combine the above graphs 
comb_graphs <- ramu_graph / top_5_graph
comb_graphs + plot_annotation(tag_levels = 'A',
                              title = 'Rana mucosa Counts by Year/Life Stage and Lake Sites',
                              theme = theme(plot.title = element_text(hjust = 0.5)))
**Figure 1.** (A) Examines total counts of R. mucosa by year in only Adult, SubAdult, and Tadpole life stages. (B) Examines the lakes with the highest counts of Adult and SubAdult counts of R. mucosa. Note that lake names are withdrawn for the species protection.

Figure 1. (A) Examines total counts of R. mucosa by year in only Adult, SubAdult, and Tadpole life stages. (B) Examines the lakes with the highest counts of Adult and SubAdult counts of R. mucosa. Note that lake names are withdrawn for the species protection.