# load packages
library(tidyverse)
library(lubridate)
library(cowplot)
library(leaflet)
library(dplyr)
library(knitr)
library(DT)

#setting working directory
knitr::opts_knit$set(root.dir = '..')
map_target_species <- function(df, color_use) {

  icos <- iconList(
    red = makeIcon(
      iconUrl = paste0("https://storage.googleapis.com/andersenlab.org/img/red.svg"),
      iconWidth = 15, iconHeight = 15,
      popupAnchorX = 0.001, popupAnchorY = -15,
      iconAnchorX = 15/2, iconAnchorY = 15
    ),
    orange = makeIcon(
      iconUrl = paste0("https://storage.googleapis.com/andersenlab.org/img/orange.svg"),
     iconWidth = 15, iconHeight = 15,
      popupAnchorX = 0.001, popupAnchorY = -15,
      iconAnchorX = 15/2, iconAnchorY = 15
  ),
    blue = makeIcon(
      iconUrl = paste0("https://storage.googleapis.com/andersenlab.org/img/blue.svg"),
      iconWidth = 15, iconHeight = 15,
      popupAnchorX = 0.001, popupAnchorY = -15,
      iconAnchorX = 15/2, iconAnchorY = 15
  ),
    black = makeIcon(
      iconUrl = paste0("https://storage.googleapis.com/andersenlab.org/img/black.svg"),
      iconWidth = 15, iconHeight = 15,
      popupAnchorX = 0.001, popupAnchorY = -15,
      iconAnchorX = 15/2, iconAnchorY = 15
  )
)
  df <- dplyr::filter(df, !is.na(df[[color_use]])) %>%
    dplyr::mutate(substrate=ifelse(is.na(substrate), "", substrate)) %>%
    dplyr::arrange(species_id)

  #print(df)
  # "https://stamen-tiles-{s}.a.ssl.fastly.net/terrain/{z}/{x}/{y}.png"
  #"https://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}"
  attach(df)
  leaflet::leaflet(data = df, width = "100%", options = list(zoomControl = T)) %>%
    addProviderTiles("Stamen.Terrain") %>%
    leaflet::addMarkers(~collection_longitude,
                        ~collection_latitude,
                        popup = glue::glue("<h2>{c_label}</h2><hr />
                                             <strong>Collected by:</strong> {collection_by}<br />
                                             <strong>Latitidue, Longitude:</strong> {format(round(collection_latitude, 6), nsmall = 6)}, 
                                             {format(round(collection_longitude, 6), nsmall = 6)}<br />
                                             <strong>Collection location:</strong> {collection_location}<br />
                                             <strong>Collection island:</strong> {collection_island}<br />
                                             <strong>Collection trail:</strong> {collection_trail}<br />
                                             <strong>Substrate:</strong> {substrate}<br />
                                             <strong>C. elegans:</strong> {`C. elegans count`}
                                             <strong>C. briggsae:</strong> {`C. briggsae count`}
                                             <strong>C. tropicalis:</strong> {`C. tropicalis count`}<br />
                                           <img style='width: 150px;' src='{sample_photo1_processed_url}'></a>"),
                        popupOptions(maxWidth = 500),
                        icon = icos[ df[[color_use]] ] )

}

knitr::knit_hooks$set(
  warning = function(x, options) {
     paste('\n\n<div class="alert alert-warning">',
           gsub('##', '\n', gsub('^##\ Warning:', '**Warning**', x)),
           '</div>', sep = '\n')
  },
  message = function(x, options) {
     paste('\n\n<div class="alert alert-info">',
           gsub('##', '\n', x),
           '</div>', sep = '\n')
   }
)

Project overview

sf <- lubridate::stamp("Tuesday, September 30, 1997")

# pull information on project
projects <- pull(df%>%dplyr::distinct(project)%>%dplyr::filter(!is.na(project)))
collectors <- pull(df%>%dplyr::distinct(collection_by)%>%dplyr::filter(!is.na(collection_by)))
isolators <- pull(df%>%dplyr::distinct(isolation_by)%>%dplyr::filter(!is.na(isolation_by)))
collection_dates <- pull(df%>%dplyr::distinct(collection_date_UTC)%>%
                           dplyr::filter(!is.na(collection_date_UTC)) %>%
                           dplyr::arrange(collection_date_UTC)) %>% ymd() %>% sf()
isolation_dates <- pull(df%>%dplyr::distinct(isolation_date_UTC)%>%
                          dplyr::filter(!is.na(isolation_date_UTC)) %>%
                          dplyr::arrange(isolation_date_UTC)) %>% ymd() %>% sf()

# make personnel dataframe
personnel_projects <- data.frame(projects)
personnel_collectors <- data.frame(collectors)
personnel_isolators <- data.frame(isolators)
personnel_collection_dates <- data.frame(collection_dates)
personnel_isolation_dates <- data.frame(isolation_dates)

# print nice tables 
DT::datatable(personnel_projects, colnames = "easyFulcrum project name:", 
              rownames = FALSE, filter = "none", options = list(dom = 't',ordering=F))

DT::datatable(personnel_collectors, colnames = "Participants in collection:", 
              rownames = FALSE, filter = "none", options = list(dom = 't'))

DT::datatable(personnel_isolators, colnames = "Participants in strain isolation:", 
              rownames = FALSE, filter = "none", options = list(dom = 't'))

DT::datatable(personnel_collection_dates, colnames = "Dates of collection:", 
              rownames = FALSE, filter = "none", options = list(dom = 't'))

DT::datatable(personnel_isolation_dates, colnames = "Dates of isolation:", 
              rownames = FALSE, filter = "none", options = list(dom = 't'))

Collections

The first of these tables describes the presence of nematodes on a collection plate, where "yes" signifies that worms are present, "no" signifies that no worms are present, and "tracks" denotes that only tracks are observed, but no worms were found.

# temporary table to collect enumerations on certain quantities of interest
collection_counts_1 <- df %>%
  dplyr::filter(!is.na(c_label)) %>%
  dplyr::mutate(class = case_when(
      !is.na(species_id) & worms_on_sample == "Yes" & species_id == "Caenorhabditis elegans" ~ "C. elegans",
      !is.na(species_id) & worms_on_sample == "Yes" & species_id == "Caenorhabditis tropicalis" ~ "C. tropicalis",
      !is.na(species_id) & worms_on_sample == "Yes" & species_id == "Caenorhabditis briggsae" ~ "C. briggsae",
      !is.na(species_id) & worms_on_sample == "Yes" & grepl("^Caeno", species_id) & !(species_id %in% c("Caenorhabditis elegans","Caenorhabditis briggsae", "Caenorhabditis tropicalis")) ~ "other-Caenorhabditis",
      !is.na(species_id) & worms_on_sample == "Yes" & !(grepl("^Caeno", species_id)) ~ "non-Caenorhabditis",
      is.na(species_id) & worms_on_sample %in% c("Yes", "Tracks") ~ "unknown nematode",
      is.na(species_id) & worms_on_sample == "No" ~ "no nematode",
      TRUE ~ "unknown/NA")) %>%
  dplyr::mutate(class = factor(
    class,
    levels = c(
      "C. elegans",
      "C. tropicalis",
      "C. briggsae",
      "other-Caenorhabditis",
      "non-Caenorhabditis",
      "unknown nematode",
      "no nematode",
      "unknown/NA"
    )
  )) %>% # "unknown nematode", "no nematode"
  dplyr::arrange(class)

collection_distinct <-
  as.numeric(length(unique(collection_counts_1$c_label)))

collection_worms_on_sample <- collection_counts_1 %>%
  dplyr::distinct(c_label, .keep_all = T) %>%
  dplyr::group_by(worms_on_sample) %>%
  dplyr::mutate(n = n()) %>%
  dplyr::distinct(worms_on_sample, n) %>%
  dplyr::mutate(
    worms_on_sample = ifelse(
      worms_on_sample %in% c("Yes", "Tracks", "No"),
      worms_on_sample,
      "unknown/NA"
    ),
    worms_on_sample = factor(worms_on_sample, levels = c("Yes", "Tracks", "No", "unknown/NA"))
  )

collection_species_id <- collection_counts_1 %>%
  dplyr::distinct(c_label, .keep_all = T) %>%
  dplyr::group_by(class) %>%
  dplyr::mutate(n = n()) %>%
  dplyr::distinct(class, n)

message(paste(
  "Total number of distinct collections:",
  collection_distinct,
  sep = " "
))

DT::datatable(
  collection_worms_on_sample,
  colnames = c("worms on sample", "number of collections"),
  rownames = FALSE,
  filter = "none",
  options = list(dom = 't')
)

DT::datatable(
  collection_species_id,
  colnames = c("collection class", "number of collections"),
  rownames = FALSE,
  filter = "none",
  options = list(dom = 't')
) 

All collections table

collections_df <- df %>%
  dplyr::select(c_label,s_label,species_id,proc_ambient_temperature,ambient_humidity,
                proc_substrate_temperature,collection_altitude, substrate,
                landscape, collection_by)

DT::datatable(collections_df, rownames = FALSE, colnames = c("C-label", "S-label",
                                                        "SpeciesID", "AmbientTemp",
                                                        "AmbientHumidity",
                                                        "SubstrateTemp", "Altitude", 
                                                        "Substrate", "Landscape",
                                                        "CollectedBy"),
              filter = "none", options = list(dom = 'ftrp', scrollX = '200px'))

All collections map

target_df <- df %>%
  dplyr::group_by(c_label, species_id) %>%
  dplyr::mutate(species_count = n()) %>%
  dplyr::mutate(`C. elegans count` = ifelse(species_id == "Caenorhabditis elegans" & !is.na(species_id), species_count, 0),
                `C. briggsae count` = ifelse(species_id == "Caenorhabditis briggsae" & !is.na(species_id), species_count, 0),
                `C. tropicalis count` = ifelse(species_id == "Caenorhabditis tropicalis" & !is.na(species_id), species_count, 0)) %>%
  dplyr::ungroup() %>%
  dplyr::distinct(c_label, species_id, .keep_all =T) %>%
  dplyr::arrange(c_label, desc(species_count)) %>%
  dplyr::group_by(c_label) %>%
  dplyr::mutate(`C. elegans count` = sum(`C. elegans count`),
                `C. briggsae count` = sum(`C. briggsae count`),
                `C. tropicalis count` = sum(`C. tropicalis count`)) %>%
  dplyr::ungroup() %>%
  dplyr::distinct(c_label, .keep_all =T) %>%
  dplyr::mutate(color = case_when(worms_on_sample == "Yes" ~ "red",
                                  worms_on_sample == "No" ~ "blue",
                                  worms_on_sample == "Tracks" ~ "orange",
                                  is.na(worms_on_sample) ~ "black"))

target_map <-  map_target_species(target_df,"color")

target_map

red points are collections with worms on sample.
blue points are collections without worms on sample.
orange points are collections with tracks on sample.
black points are collections that were NA.

All collections plots

stat_df <- df %>%
  dplyr::distinct(project, c_label, .keep_all = T) %>%
  dplyr::select(project, c_label, s_label, species_id, proc_ambient_temperature,
                ambient_humidity, proc_substrate_temperature, collection_altitude) %>%
  tidyr::gather(env_par, value, proc_ambient_temperature,ambient_humidity,
                proc_substrate_temperature, collection_altitude)

plot_atemp <- ggplot(data = stat_df %>% dplyr::filter(env_par == "proc_ambient_temperature")) +
  geom_boxplot(aes(x = env_par, y = value), outlier.color = NA) +
  geom_jitter(aes(x = env_par, y = value, shape = project), size = 1.5, width = .25) +
  theme_bw() +
  theme(axis.title = element_text(size = 8, color = "black"),
        axis.text = element_text(size = 8, color = "black")) + 
  labs(x = "", y = "Ambient temperature (°C)") +
  theme(axis.text.x = element_text(size = 9, face = "italic", color = "black"),
        axis.text.y = element_text(size = 9, face = "plain", color = "black"),
        axis.title.x = element_text(size = 9, face = "bold", color = "black"),
        axis.title.y = element_text(size = 9, face = "bold", color = "black"),
        plot.margin = unit(c(0,1,1,1), "lines"),
        legend.position = "none")
plot_stemp <- ggplot(data = stat_df %>% dplyr::filter(env_par == "proc_substrate_temperature")) +
  geom_boxplot(aes(x = env_par,y = value), outlier.color = NA) +
  geom_jitter(aes(x = env_par,y = value, shape = project), size = 1.5, width = .25) +
  theme_bw() +
  theme(axis.title = element_text(size = 8, color = "black"),
        axis.text = element_text(size = 8, color = "black")) + 
  labs(x = "", y = "Substrate temperature (°C)") +
  theme(axis.text.x = element_text(size = 9, face = "italic", color = "black"),
        axis.text.y = element_text(size = 9, face = "plain", color = "black"),
        axis.title.x = element_text(size = 9, face = "bold", color = "black"),
        axis.title.y = element_text(size = 9, face = "bold", color = "black"),
        plot.margin = unit(c(0,1,1,1), "lines"),
        legend.position = "none")
plot_ahum <- ggplot(data = stat_df %>% dplyr::filter(env_par == "ambient_humidity")) +
  geom_boxplot(aes(x = env_par, y = value), outlier.color = NA) +
  geom_jitter(aes(x = env_par, y = value, shape = project), size = 1.5, width = .25) +
  theme_bw() +
  theme(axis.title = element_text(size = 8, color = "black"),
        axis.text = element_text(size = 8, color = "black")) + 
  labs(x = "", y = "Ambient humidity (%)") +
  theme(axis.text.x = element_text(size = 9, face = "italic", color = "black"),
        axis.text.y = element_text(size = 9, face = "plain", color = "black"),
        axis.title.x = element_text(size = 9, face = "bold", color = "black"),
        axis.title.y = element_text(size = 9, face = "bold", color = "black"),
        plot.margin = unit(c(0,1,1,1), "lines"),
        legend.position = "none")
plot_elev <- ggplot(data = stat_df %>% dplyr::filter(env_par == "collection_altitude")) +
  geom_boxplot(aes(x = env_par, y = value), outlier.color = NA) +
  geom_jitter(aes(x = env_par, y = value, shape = project), size = 1.5, width = .25) +
  theme_bw() +
  theme(axis.title = element_text(size = 8, color = "black"),
        axis.text = element_text(size = 8, color = "black")) + 
  labs(x = "", y = "Elevation (m)") +
  theme(axis.text.x = element_text(size = 9, face = "italic", color = "black"),
        axis.text.y = element_text(size = 9, face = "plain", color = "black"),
        axis.title.x = element_text(size = 9, face = "bold", color = "black"),
        axis.title.y = element_text(size = 9, face = "bold", color = "black"),
        plot.margin = unit(c(0,1,1,1), "lines"),
        legend.position = "none")

cowplot::plot_grid(plot_atemp, plot_stemp, plot_ahum, plot_elev,
                   ncol = 2, nrow = 2, align = "hv", axis = "l")

Isolations

# temporary table to collect enumerations on certain quantities of interest
isolation_counts_1 <- df %>%
  dplyr::filter(!is.na(s_label)) %>%
  dplyr::mutate(class = case_when(!is.na(species_id) ~ species_id,
                                  is.na(species_id) & (pcr_product_ssu == 1 & pcr_product_its2 == 0) ~ "non-Caenorhabditis",
                                  is.na(species_id) & (pcr_product_ssu == 0 & pcr_product_its2 == 0) ~ "Unknown nematode",
                                  is.na(species_id) & pcr_product_its2 == 1 ~ "its2 genotype missing",
                                  TRUE ~ "unknown/NA")) %>%
  dplyr::distinct(s_label, .keep_all = TRUE) 

isolation_distinct <- as.numeric(length(unique(isolation_counts_1$s_label)))

isolation_species_id <- isolation_counts_1 %>%
  dplyr::group_by(class) %>%
  dplyr::mutate(n = n()) %>%
  dplyr::distinct(class, n) %>%
  dplyr::arrange(class)

message(paste("Total number of distinct isolations:", 
              isolation_distinct, sep = " "))

DT::datatable(isolation_species_id, colnames = c("isolation class","number of isolations"),
              rownames = FALSE, filter = "none", options = list(dom = 't'))

Selfing Caenorhabditis species

Selfing Caenorhabditis table

selfing_df <- df %>% dplyr::filter(species_id %in% c("Caenorhabditis elegans", 
                                                    "Caenorhabditis tropicalis", 
                                                    "Caenorhabditis briggsae")) %>%
  dplyr::select(c_label,s_label,species_id,proc_ambient_temperature,ambient_humidity,
                proc_substrate_temperature,collection_altitude, substrate,
                landscape, collection_by)

DT::datatable(selfing_df, rownames = FALSE, colnames = c("C-label", "S-label",
                                                        "SpeciesID", "AmbientTemp",
                                                        "AmbientHumidity",
                                                        "SubstrateTemp", "Altitude", 
                                                        "Substrate", "Landscape",
                                                        "CollectedBy"),
              filter = "none", options = list(dom = 'ftrp', scrollX = '200px'))

Selfing Caenorhabditis map

target_df <- df %>%
  dplyr::group_by(c_label, species_id) %>%
  dplyr::mutate(species_count = n()) %>%
  dplyr::mutate(`C. elegans count` = ifelse(species_id == "Caenorhabditis elegans" & !is.na(species_id), species_count, 0),
                `C. briggsae count` = ifelse(species_id == "Caenorhabditis briggsae" & !is.na(species_id), species_count, 0),
                `C. tropicalis count` = ifelse(species_id == "Caenorhabditis tropicalis" & !is.na(species_id), species_count, 0)) %>%
  dplyr::ungroup() %>%
  dplyr::distinct(c_label, species_id, .keep_all =T) %>%
  dplyr::arrange(c_label, desc(species_count)) %>%
  dplyr::group_by(c_label) %>%
  dplyr::mutate(`C. elegans count` = sum(`C. elegans count`),
                `C. briggsae count` = sum(`C. briggsae count`),
                `C. tropicalis count` = sum(`C. tropicalis count`)) %>%
  dplyr::ungroup() %>%
  dplyr::filter(species_id %in% c("Caenorhabditis elegans", "Caenorhabditis briggsae", "Caenorhabditis tropicalis")) %>%
  dplyr::distinct(c_label, .keep_all =T) %>%
  dplyr::mutate(color = "black") 

target_map <-  map_target_species(target_df,"color")

target_map

Selfing Caenorhabditis plots

stat_df <- df%>%
  dplyr::filter(species_id %in% c("Caenorhabditis elegans", "Caenorhabditis tropicalis", 
                                  "Caenorhabditis briggsae")) %>%
  dplyr::distinct(project, c_label, species_id, .keep_all = T) %>%
  dplyr::select(project, c_label, s_label, species_id, proc_ambient_temperature,
                ambient_humidity, proc_substrate_temperature, collection_altitude) %>%
  dplyr::mutate(species_id = forcats::as_factor(species_id),
                species_id = forcats::fct_relevel(species_id,
                                                      "Caenorhabditis elegans",
                                                      "Caenorhabditis tropicalis",
                                                      "Caenorhabditis briggsae")) %>%
  tidyr::gather(env_par, value, proc_ambient_temperature,ambient_humidity,
                proc_substrate_temperature, collection_altitude)

plot_atemp <- ggplot(data = stat_df %>% dplyr::filter(env_par == "proc_ambient_temperature")) +
  geom_boxplot(aes(x = species_id, y = value, fill = species_id), outlier.color = NA) +
  scale_fill_manual(values=c("red","blue","orange")) +
  geom_jitter(aes(x = species_id, y = value, shape = project), size = 1.5, width = .25) +
  theme_bw() +
  theme(axis.title = element_text(size = 8, color = "black"),
        axis.text = element_text(size = 8, color = "black")) + 
  labs(fill = "Species", x = "", y = "Ambient temperature (°C)") +
  theme(axis.text.x = element_text(size = 9, face = "italic", color = "black"),
        axis.text.y = element_text(size = 9, face = "plain", color = "black"),
        axis.title.x = element_text(size = 9, face = "bold", color = "black"),
        axis.title.y = element_text(size = 9, face = "bold", color = "black"),
        plot.margin = unit(c(0,1,1,1), "lines"),
        legend.position = "none")
plot_stemp <- ggplot(data = stat_df %>% dplyr::filter(env_par == "proc_substrate_temperature")) +
  geom_boxplot(aes(x = species_id, y = value, fill = species_id), outlier.color = NA) +
  scale_fill_manual(values=c("red","blue","orange")) +
  geom_jitter(aes(x = species_id, y = value, shape = project), size = 1.5, width = .25) +
  theme_bw() +
  theme(axis.title = element_text(size = 8, color = "black"),
        axis.text = element_text(size = 8, color = "black")) + 
  labs(fill = "Species", x = "", y = "Substrate temperature (°C)") +
  theme(axis.text.x = element_text(size = 9, face = "italic", color = "black"),
        axis.text.y = element_text(size = 9, face = "plain", color = "black"),
        axis.title.x = element_text(size = 9, face = "bold", color = "black"),
        axis.title.y = element_text(size = 9, face = "bold", color = "black"),
        plot.margin = unit(c(0,1,1,1), "lines"),
        legend.position = "none")
plot_ahum <- ggplot(data = stat_df %>% dplyr::filter(env_par == "ambient_humidity")) +
  geom_boxplot(aes(x = species_id, y = value, fill = species_id), outlier.color = NA) +
  scale_fill_manual(values=c("red","blue","orange")) +
  geom_jitter(aes(x = species_id, y = value, shape = project), size = 1.5, width = .25) +
  theme_bw() +
  theme(axis.title = element_text(size = 8, color = "black"),
        axis.text = element_text(size = 8, color = "black")) + 
  labs(fill = "Species", x = "", y = "Ambient humidity (%)") +
  theme(axis.text.x = element_text(size = 9, face = "italic", color = "black"),
        axis.text.y = element_text(size = 9, face = "plain", color = "black"),
        axis.title.x = element_text(size = 9, face = "bold", color = "black"),
        axis.title.y = element_text(size = 9, face = "bold", color = "black"),
        plot.margin = unit(c(0,1,1,1), "lines"),
        legend.position = "none")
plot_elev <- ggplot(data = stat_df %>% dplyr::filter(env_par == "collection_altitude")) +
  geom_boxplot(aes(x = species_id, y = value, fill = species_id), outlier.color = NA) +
  scale_fill_manual(values=c("red","blue","orange")) +
  geom_jitter(aes(x = species_id, y = value, shape = project), size = 1.5, width = .25) +
  theme_bw() +
  theme(axis.title = element_text(size = 8, color = "black"),
        axis.text = element_text(size = 8, color = "black")) + 
  labs(fill = "Species", x = "", y = "Elevation (m)") +
  theme(axis.text.x = element_text(size = 9, face = "italic", color = "black"),
        axis.text.y = element_text(size = 9, face = "plain", color = "black"),
        axis.title.x = element_text(size = 9, face = "bold", color = "black"),
        axis.title.y = element_text(size = 9, face = "bold", color = "black"),
        plot.margin = unit(c(0,1,1,1), "lines"),
        legend.position = "none")

cowplot::plot_grid(plot_atemp, plot_stemp, plot_ahum, plot_elev,
                   ncol = 2, nrow = 2, align = "hv", axis = "l")

Selfing Caenorhabditis species

C. elegans

elegans_eval <- ifelse(df %>% 
                      dplyr::filter(species_id == "Caenorhabditis elegans") %>%
                      dplyr::pull(c_label) %>% length()==0,
                    FALSE,TRUE)

if(elegans_eval == FALSE) {
  warning("No collections in this project isolated C. elegans samples")
}
elegans_df <- df %>% dplyr::filter(species_id == "Caenorhabditis elegans") %>%
  dplyr::select(c_label,s_label,species_id,proc_ambient_temperature,ambient_humidity,
                proc_substrate_temperature,collection_altitude, substrate,
                landscape, collection_by)

DT::datatable(elegans_df, rownames = FALSE, colnames = c("C-label", "S-label",
                                                        "SpeciesID", "AmbientTemp",
                                                        "AmbientHumidity",
                                                        "SubstrateTemp", "Altitude", 
                                                        "Substrate", "Landscape",
                                                        "CollectedBy"),
              filter = "none", options = list(dom = 'ftrp', scrollX = '200px'))
target_df <- df %>%
  dplyr::group_by(c_label, species_id) %>%
  dplyr::mutate(species_count = n()) %>%
  dplyr::mutate(`C. elegans count` = ifelse(species_id == "Caenorhabditis elegans" & !is.na(species_id), species_count, 0),
                `C. briggsae count` = ifelse(species_id == "Caenorhabditis briggsae" & !is.na(species_id), species_count, 0),
                `C. tropicalis count` = ifelse(species_id == "Caenorhabditis tropicalis" & !is.na(species_id), species_count, 0)) %>%
  dplyr::ungroup() %>%
  dplyr::distinct(c_label, species_id, .keep_all =T) %>%
  dplyr::arrange(c_label, desc(species_count)) %>%
  dplyr::group_by(c_label) %>%
  dplyr::mutate(`C. elegans count` = sum(`C. elegans count`),
                `C. briggsae count` = sum(`C. briggsae count`),
                `C. tropicalis count` = sum(`C. tropicalis count`)) %>%
  dplyr::ungroup() %>%
  dplyr::filter(species_id == "Caenorhabditis elegans") %>%
  dplyr::distinct(c_label, .keep_all =T) %>%
  dplyr::mutate(color = case_when(species_id == "Caenorhabditis elegans" ~ "red",
                                  species_id == "Caenorhabditis briggsae" ~ "blue",
                                  species_id == "Caenorhabditis tropicalis" ~ "orange")) 

target_map <-  map_target_species(target_df,"color")

target_map
stat_df <- df%>%
  dplyr::filter(species_id == "Caenorhabditis elegans") %>%
  dplyr::distinct(project, c_label, species_id, .keep_all = T) %>%
  dplyr::select(project, c_label, s_label, species_id, proc_ambient_temperature,
                ambient_humidity, proc_substrate_temperature, collection_altitude) %>%
  dplyr::mutate(species_id = forcats::as_factor(species_id),
                species_id = forcats::fct_relevel(species_id,
                                                      "Caenorhabditis elegans",
                                                      "Caenorhabditis tropicalis",
                                                      "Caenorhabditis briggsae")) %>%
  tidyr::gather(env_par, value, proc_ambient_temperature,ambient_humidity,
                proc_substrate_temperature, collection_altitude)

plot_atemp <- ggplot(data = stat_df %>% dplyr::filter(env_par == "proc_ambient_temperature")) +
  geom_boxplot(aes(x = species_id, y = value, fill = species_id), outlier.color = NA) +
  scale_fill_manual(values=c("red","blue","orange")) +
  geom_jitter(aes(x = species_id, y = value, shape = project), size = 1.5, width = .25) +
  theme_bw() +
  theme(axis.title = element_text(size = 8, color = "black"),
        axis.text = element_text(size = 8, color = "black")) + 
  labs(fill = "Species", x = "", y = "Ambient temperature (°C)") +
  theme(axis.text.x = element_text(size = 9, face = "italic", color = "black"),
        axis.text.y = element_text(size = 9, face = "plain", color = "black"),
        axis.title.x = element_text(size = 9, face = "bold", color = "black"),
        axis.title.y = element_text(size = 9, face = "bold", color = "black"),
        plot.margin = unit(c(0,1,1,1), "lines"),
        legend.position = "none")
plot_stemp <- ggplot(data = stat_df %>% dplyr::filter(env_par == "proc_substrate_temperature")) +
  geom_boxplot(aes(x = species_id, y = value, fill = species_id), outlier.color = NA) +
  scale_fill_manual(values=c("red","blue","orange")) +
  geom_jitter(aes(x = species_id, y = value, shape = project), size = 1.5, width = .25) +
  theme_bw() +
  theme(axis.title = element_text(size = 8, color = "black"),
        axis.text = element_text(size = 8, color = "black")) + 
  labs(fill = "Species", x = "", y = "Substrate temperature (°C)") +
  theme(axis.text.x = element_text(size = 9, face = "italic", color = "black"),
        axis.text.y = element_text(size = 9, face = "plain", color = "black"),
        axis.title.x = element_text(size = 9, face = "bold", color = "black"),
        axis.title.y = element_text(size = 9, face = "bold", color = "black"),
        plot.margin = unit(c(0,1,1,1), "lines"),
        legend.position = "none")
plot_ahum <- ggplot(data = stat_df %>% dplyr::filter(env_par == "ambient_humidity")) +
  geom_boxplot(aes(x = species_id, y = value, fill = species_id), outlier.color = NA) +
  scale_fill_manual(values=c("red","blue","orange")) +
  geom_jitter(aes(x = species_id, y = value, shape = project), size = 1.5, width = .25) +
  theme_bw() +
  theme(axis.title = element_text(size = 8, color = "black"),
        axis.text = element_text(size = 8, color = "black")) + 
  labs(fill = "Species", x = "", y = "Ambient humidity (%)") +
  theme(axis.text.x = element_text(size = 9, face = "italic", color = "black"),
        axis.text.y = element_text(size = 9, face = "plain", color = "black"),
        axis.title.x = element_text(size = 9, face = "bold", color = "black"),
        axis.title.y = element_text(size = 9, face = "bold", color = "black"),
        plot.margin = unit(c(0,1,1,1), "lines"),
        legend.position = "none")
plot_elev <- ggplot(data = stat_df %>% dplyr::filter(env_par == "collection_altitude")) +
  geom_boxplot(aes(x = species_id, y = value, fill = species_id), outlier.color = NA) +
  scale_fill_manual(values=c("red","blue","orange")) +
  geom_jitter(aes(x = species_id, y = value, shape = project), size = 1.5, width = .25) +
  theme_bw() +
  theme(axis.title = element_text(size = 8, color = "black"),
        axis.text = element_text(size = 8, color = "black")) + 
  labs(fill = "Species", x = "", y = "Elevation (m)") +
  theme(axis.text.x = element_text(size = 9, face = "italic", color = "black"),
        axis.text.y = element_text(size = 9, face = "plain", color = "black"),
        axis.title.x = element_text(size = 9, face = "bold", color = "black"),
        axis.title.y = element_text(size = 9, face = "bold", color = "black"),
        plot.margin = unit(c(0,1,1,1), "lines"),
        legend.position = "none")

cowplot::plot_grid(plot_atemp, plot_stemp, plot_ahum, plot_elev,
                   ncol = 2, nrow = 2, align = "hv", axis = "l")

C. briggsae

briggsae_eval <- ifelse(df %>% 
                      dplyr::filter(species_id == "Caenorhabditis briggsae") %>%
                      dplyr::pull(c_label) %>% length()==0,
                    FALSE,TRUE)

if(briggsae_eval == FALSE) {
  warning("No collections in this project isolated C. briggsae samples")
}
briggsae_df <- df %>% dplyr::filter(species_id == "Caenorhabditis briggsae") %>%
  dplyr::select(c_label,s_label,species_id,proc_ambient_temperature,ambient_humidity,
                proc_substrate_temperature,collection_altitude, substrate,
                landscape, collection_by)

DT::datatable(briggsae_df, rownames = FALSE, colnames = c("C-label", "S-label",
                                                        "SpeciesID", "AmbientTemp",
                                                        "AmbientHumidity",
                                                        "SubstrateTemp", "Altitude", 
                                                        "Substrate", "Landscape",
                                                        "CollectedBy"),
              filter = "none", options = list(dom = 'ftrp', scrollX = '200px'))
target_df <- df %>%
  dplyr::group_by(c_label, species_id) %>%
  dplyr::mutate(species_count = n()) %>%
  dplyr::mutate(`C. elegans count` = ifelse(species_id == "Caenorhabditis elegans" & !is.na(species_id), species_count, 0),
                `C. briggsae count` = ifelse(species_id == "Caenorhabditis briggsae" & !is.na(species_id), species_count, 0),
                `C. tropicalis count` = ifelse(species_id == "Caenorhabditis tropicalis" & !is.na(species_id), species_count, 0)) %>%
  dplyr::ungroup() %>%
  dplyr::distinct(c_label, species_id, .keep_all =T) %>%
  dplyr::arrange(c_label, desc(species_count)) %>%
  dplyr::group_by(c_label) %>%
  dplyr::mutate(`C. elegans count` = sum(`C. elegans count`),
                `C. briggsae count` = sum(`C. briggsae count`),
                `C. tropicalis count` = sum(`C. tropicalis count`)) %>%
  dplyr::ungroup() %>%
  dplyr::filter(species_id == "Caenorhabditis briggsae") %>%
  dplyr::distinct(c_label, .keep_all =T) %>%
  dplyr::mutate(color = case_when(species_id == "Caenorhabditis elegans" ~ "red",
                                  species_id == "Caenorhabditis briggsae" ~ "blue",
                                  species_id == "Caenorhabditis tropicalis" ~ "orange")) 

target_map <-  map_target_species(target_df,"color")

target_map
stat_df <- df%>%
  dplyr::filter(species_id == "Caenorhabditis briggsae") %>%
  dplyr::distinct(project, c_label, species_id, .keep_all = T) %>%
  dplyr::select(project, c_label, s_label, species_id, proc_ambient_temperature,
                ambient_humidity, proc_substrate_temperature, collection_altitude) %>%
  dplyr::mutate(species_id = forcats::as_factor(species_id),
                species_id = forcats::fct_relevel(species_id,
                                                      "Caenorhabditis elegans",
                                                      "Caenorhabditis tropicalis",
                                                      "Caenorhabditis briggsae")) %>%
  tidyr::gather(env_par, value, proc_ambient_temperature,ambient_humidity,
                proc_substrate_temperature, collection_altitude)

plot_atemp <- ggplot(data = stat_df %>% dplyr::filter(env_par == "proc_ambient_temperature")) +
  geom_boxplot(aes(x = species_id, y = value, fill = species_id), outlier.color = NA) +
  scale_fill_manual(values=c("red","blue","orange")) +
  geom_jitter(aes(x = species_id, y = value, shape = project), size = 1.5, width = .25) +
  theme_bw() +
  theme(axis.title = element_text(size = 8, color = "black"),
        axis.text = element_text(size = 8, color = "black")) + 
  labs(fill = "Species", x = "", y = "Ambient temperature (°C)") +
  theme(axis.text.x = element_text(size = 9, face = "italic", color = "black"),
        axis.text.y = element_text(size = 9, face = "plain", color = "black"),
        axis.title.x = element_text(size = 9, face = "bold", color = "black"),
        axis.title.y = element_text(size = 9, face = "bold", color = "black"),
        plot.margin = unit(c(0,1,1,1), "lines"),
        legend.position = "none")
plot_stemp <- ggplot(data = stat_df %>% dplyr::filter(env_par == "proc_substrate_temperature")) +
  geom_boxplot(aes(x = species_id, y = value, fill = species_id), outlier.color = NA) +
  scale_fill_manual(values=c("red","blue","orange")) +
  geom_jitter(aes(x = species_id, y = value, shape = project), size = 1.5, width = .25) +
  theme_bw() +
  theme(axis.title = element_text(size = 8, color = "black"),
        axis.text = element_text(size = 8, color = "black")) + 
  labs(fill = "Species", x = "", y = "Substrate temperature (°C)") +
  theme(axis.text.x = element_text(size = 9, face = "italic", color = "black"),
        axis.text.y = element_text(size = 9, face = "plain", color = "black"),
        axis.title.x = element_text(size = 9, face = "bold", color = "black"),
        axis.title.y = element_text(size = 9, face = "bold", color = "black"),
        plot.margin = unit(c(0,1,1,1), "lines"),
        legend.position = "none")
plot_ahum <- ggplot(data = stat_df %>% dplyr::filter(env_par == "ambient_humidity")) +
  geom_boxplot(aes(x = species_id, y = value, fill = species_id), outlier.color = NA) +
  scale_fill_manual(values=c("red","blue","orange")) +
  geom_jitter(aes(x = species_id, y = value, shape = project), size = 1.5, width = .25) +
  theme_bw() +
  theme(axis.title = element_text(size = 8, color = "black"),
        axis.text = element_text(size = 8, color = "black")) + 
  labs(fill = "Species", x = "", y = "Ambient humidity (%)") +
  theme(axis.text.x = element_text(size = 9, face = "italic", color = "black"),
        axis.text.y = element_text(size = 9, face = "plain", color = "black"),
        axis.title.x = element_text(size = 9, face = "bold", color = "black"),
        axis.title.y = element_text(size = 9, face = "bold", color = "black"),
        plot.margin = unit(c(0,1,1,1), "lines"),
        legend.position = "none")
plot_elev <- ggplot(data = stat_df %>% dplyr::filter(env_par == "collection_altitude")) +
  geom_boxplot(aes(x = species_id, y = value, fill = species_id), outlier.color = NA) +
  scale_fill_manual(values=c("red","blue","orange")) +
  geom_jitter(aes(x = species_id, y = value, shape = project), size = 1.5, width = .25) +
  theme_bw() +
  theme(axis.title = element_text(size = 8, color = "black"),
        axis.text = element_text(size = 8, color = "black")) + 
  labs(fill = "Species", x = "", y = "Elevation (m)") +
  theme(axis.text.x = element_text(size = 9, face = "italic", color = "black"),
        axis.text.y = element_text(size = 9, face = "plain", color = "black"),
        axis.title.x = element_text(size = 9, face = "bold", color = "black"),
        axis.title.y = element_text(size = 9, face = "bold", color = "black"),
        plot.margin = unit(c(0,1,1,1), "lines"),
        legend.position = "none")

cowplot::plot_grid(plot_atemp, plot_stemp, plot_ahum, plot_elev,
                   ncol = 2, nrow = 2, align = "hv", axis = "l")

C. tropicalis

tropicalis_eval <- ifelse(df %>% 
                            dplyr::filter(species_id == "Caenorhabditis tropicalis") %>%
                            dplyr::pull(c_label) %>% length()==0,
                          FALSE,TRUE)

if(tropicalis_eval == FALSE) {
  warning("No collections in this project isolated C. tropicalis samples")
}
tropicalis_df <- df %>% dplyr::filter(species_id == "Caenorhabditis tropicalis") %>%
  dplyr::select(c_label,s_label,species_id,proc_ambient_temperature,ambient_humidity,
                proc_substrate_temperature,collection_altitude, substrate,
                landscape, collection_by)

DT::datatable(tropicalis_df, rownames = FALSE, colnames = c("C-label", "S-label",
                                                            "SpeciesID", "AmbientTemp",
                                                            "AmbientHumidity",
                                                            "SubstrateTemp", "Altitude", 
                                                            "Substrate", "Landscape",
                                                            "CollectedBy"),
              filter = "none", options = list(dom = 'ftrp', scrollX = '200px'))
target_df <- df %>%
  dplyr::group_by(c_label, species_id) %>%
  dplyr::mutate(species_count = n()) %>%
  dplyr::mutate(`C. elegans count` = ifelse(species_id == "Caenorhabditis elegans" & !is.na(species_id), species_count, 0),
                `C. briggsae count` = ifelse(species_id == "Caenorhabditis briggsae" & !is.na(species_id), species_count, 0),
                `C. tropicalis count` = ifelse(species_id == "Caenorhabditis tropicalis" & !is.na(species_id), species_count, 0)) %>%
  dplyr::ungroup() %>%
  dplyr::distinct(c_label, species_id, .keep_all =T) %>%
  dplyr::arrange(c_label, desc(species_count)) %>%
  dplyr::group_by(c_label) %>%
  dplyr::mutate(`C. elegans count` = sum(`C. elegans count`),
                `C. briggsae count` = sum(`C. briggsae count`),
                `C. tropicalis count` = sum(`C. tropicalis count`)) %>%
  dplyr::ungroup() %>%
  dplyr::filter(species_id == "Caenorhabditis tropicalis") %>%
  dplyr::distinct(c_label, .keep_all =T) %>%
  dplyr::mutate(color = case_when(species_id == "Caenorhabditis elegans" ~ "red",
                                  species_id == "Caenorhabditis briggsae" ~ "blue",
                                  species_id == "Caenorhabditis tropicalis" ~ "orange")) 

target_map <-  map_target_species(target_df,"color")

target_map
stat_df <- df%>%
  dplyr::filter(species_id == "Caenorhabditis tropicalis") %>%
  dplyr::distinct(project, c_label, species_id, .keep_all = T) %>%
  dplyr::select(project, c_label, s_label, species_id, proc_ambient_temperature,
                ambient_humidity, proc_substrate_temperature, collection_altitude) %>%
  dplyr::mutate(species_id = forcats::as_factor(species_id),
                species_id = forcats::fct_relevel(species_id,
                                                  "Caenorhabditis elegans",
                                                  "Caenorhabditis tropicalis",
                                                  "Caenorhabditis briggsae")) %>%
  tidyr::gather(env_par, value, proc_ambient_temperature,ambient_humidity,
                proc_substrate_temperature, collection_altitude)

plot_atemp <- ggplot(data = stat_df %>% dplyr::filter(env_par == "proc_ambient_temperature")) +
  geom_boxplot(aes(x = species_id, y = value, fill = species_id), outlier.color = NA) +
  scale_fill_manual(values=c("red","blue","orange")) +
  geom_jitter(aes(x = species_id, y = value, shape = project), size = 1.5, width = .25) +
  theme_bw() +
  theme(axis.title = element_text(size = 8, color = "black"),
        axis.text = element_text(size = 8, color = "black")) + 
  labs(fill = "Species", x = "", y = "Ambient temperature (°C)") +
  theme(axis.text.x = element_text(size = 9, face = "italic", color = "black"),
        axis.text.y = element_text(size = 9, face = "plain", color = "black"),
        axis.title.x = element_text(size = 9, face = "bold", color = "black"),
        axis.title.y = element_text(size = 9, face = "bold", color = "black"),
        plot.margin = unit(c(0,1,1,1), "lines"),
        legend.position = "none")
plot_stemp <- ggplot(data = stat_df %>% dplyr::filter(env_par == "proc_substrate_temperature")) +
  geom_boxplot(aes(x = species_id, y = value, fill = species_id), outlier.color = NA) +
  scale_fill_manual(values=c("red","blue","orange")) +
  geom_jitter(aes(x = species_id, y = value, shape = project), size = 1.5, width = .25) +
  theme_bw() +
  theme(axis.title = element_text(size = 8, color = "black"),
        axis.text = element_text(size = 8, color = "black")) + 
  labs(fill = "Species", x = "", y = "Substrate temperature (°C)") +
  theme(axis.text.x = element_text(size = 9, face = "italic", color = "black"),
        axis.text.y = element_text(size = 9, face = "plain", color = "black"),
        axis.title.x = element_text(size = 9, face = "bold", color = "black"),
        axis.title.y = element_text(size = 9, face = "bold", color = "black"),
        plot.margin = unit(c(0,1,1,1), "lines"),
        legend.position = "none")
plot_ahum <- ggplot(data = stat_df %>% dplyr::filter(env_par == "ambient_humidity")) +
  geom_boxplot(aes(x = species_id, y = value, fill = species_id), outlier.color = NA) +
  scale_fill_manual(values=c("red","blue","orange")) +
  geom_jitter(aes(x = species_id, y = value, shape = project), size = 1.5, width = .25) +
  theme_bw() +
  theme(axis.title = element_text(size = 8, color = "black"),
        axis.text = element_text(size = 8, color = "black")) + 
  labs(fill = "Species", x = "", y = "Ambient humidity (%)") +
  theme(axis.text.x = element_text(size = 9, face = "italic", color = "black"),
        axis.text.y = element_text(size = 9, face = "plain", color = "black"),
        axis.title.x = element_text(size = 9, face = "bold", color = "black"),
        axis.title.y = element_text(size = 9, face = "bold", color = "black"),
        plot.margin = unit(c(0,1,1,1), "lines"),
        legend.position = "none")
plot_elev <- ggplot(data = stat_df %>% dplyr::filter(env_par == "collection_altitude")) +
  geom_boxplot(aes(x = species_id, y = value, fill = species_id), outlier.color = NA) +
  scale_fill_manual(values=c("red","blue","orange")) +
  geom_jitter(aes(x = species_id, y = value, shape = project), size = 1.5, width = .25) +
  theme_bw() +
  theme(axis.title = element_text(size = 8, color = "black"),
        axis.text = element_text(size = 8, color = "black")) + 
  labs(fill = "Species", x = "", y = "Elevation (m)") +
  theme(axis.text.x = element_text(size = 9, face = "italic", color = "black"),
        axis.text.y = element_text(size = 9, face = "plain", color = "black"),
        axis.title.x = element_text(size = 9, face = "bold", color = "black"),
        axis.title.y = element_text(size = 9, face = "bold", color = "black"),
        plot.margin = unit(c(0,1,1,1), "lines"),
        legend.position = "none")

cowplot::plot_grid(plot_atemp, plot_stemp, plot_ahum, plot_elev,
                   ncol = 2, nrow = 2, align = "hv", axis = "l")


AndersenLab/easyfulcrum documentation built on Aug. 23, 2023, 2:35 a.m.