Skip to content

Commit

Permalink
Move species table code to new script #121
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoliver committed Dec 5, 2024
1 parent e828aa2 commit d3ab466
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
2 changes: 1 addition & 1 deletion output/manuscript/papilio-table.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"species","n_filtered","n_hosts","n_hosts_suff","ew"
"Species","# Records","# Host species","# Host SDMs","Location"
"P. appalachiensis",131,1,1,"East"
"P. brevicauda",196,9,8,"East"
"P. canadensis",7200,20,19,"East"
Expand Down
58 changes: 58 additions & 0 deletions src/manuscript/tables-1-species.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Create table with species information for manuscript
# Erin Zylstra & Jeff Oliver
# [email protected] & [email protected]
# 2024-12-05

library(dplyr)
require(stringr)

# Load insect-host information and gbif data summaries
ih <- read.csv("data/insect-host.csv")
gbif <- read.csv("data/gbif-pa-summary.csv")

# File with east/west designation information
ew <- read.csv(file = "data/insect-eastwest.csv")

# Base of output filenames
output_basename <- "output/manuscript/"

# Get number of filtered records for each swallowtail spp
insects <- gbif %>%
filter(str_detect(species, "Papilio"))

# Retain only those that have a presence/absence CSV file
insects <- insects %>%
filter(pa_csv == "yes") %>%
select(species, n_filtered)

# Get number of host plant species (total or with 40+ filtered records)
ih2 <- ih %>%
select(insect, host_accepted) %>%
left_join(select(gbif, species, pa_csv),
join_by("host_accepted" == "species")) %>%
group_by(insect) %>%
summarize(n_hosts = length(host_accepted),
n_hosts_suff = sum(pa_csv == "yes")) %>%
data.frame()

# Add host species counts to insects data
insects <- left_join(insects, ih2, join_by("species" == "insect"))

# Add in east/west indicator & shorten names
insects <- insects %>%
left_join(ew, by = c("species" = "insect")) %>%
mutate(species = str_replace(species, "Papilio", "P."))

# Update column names more appropriate for manuscript
insects <- insects %>%
rename(Species = species,
`# Records` = n_filtered,
`# Host species` = n_hosts,
`# Host SDMs` = n_hosts_suff,
Location = ew)

# Write to file
papilio_table <- paste0(output_basename, "papilio-table.csv")
write.csv(x = insects,
file = papilio_table,
row.names = FALSE)

0 comments on commit d3ab466

Please sign in to comment.