From 57bfc05ac56abef36bfb2ee6b470198676c8d7a0 Mon Sep 17 00:00:00 2001 From: Nikki Moore Date: Fri, 13 Oct 2023 12:21:39 +0200 Subject: [PATCH] updating plotting functions & shared functions --- R/00_shared-functions.R | 102 +++++++++++++++++++++++++++++ R/01_plotting-functions.R | 132 +++++++++++++++++++++++++++++++++++--- 2 files changed, 224 insertions(+), 10 deletions(-) create mode 100644 R/00_shared-functions.R diff --git a/R/00_shared-functions.R b/R/00_shared-functions.R new file mode 100644 index 0000000..12434da --- /dev/null +++ b/R/00_shared-functions.R @@ -0,0 +1,102 @@ +## basic functions that will be shared and called by other functions + +## function to fitler by common arguments and throw errors if filtering parameters are wrongly specified +filter_by_arguments <- function(.fishbase_names = NULL, + .taxo_level = NULL, + .taxo_levels = NULL, + .datasets = NULL, + .realms = NULL, + .taxo_scale_observation = NULL, + .trait_names = NULL, + .trait_types = NULL, + .geographic_region = NULL, + .data = data) { + + ## filter the full Phenofish dataset according to arguments passed to function + ## if taxonomic level AND species names are specified, throw error: + if(!is.null(.fishbase_names) & !is.null(.taxo_level)) { + print("Error: either a taxonomic level OR a vector of species names can be specified, not both.") + return() + } + ## if trait names AND trait types are specified, throw error: + if(!is.null(.trait_names) & !is.null(.trait_types)) { + print("Error: either a vector of trait names OR a vector of trait types can be specified, not both.") + return() + } + + data_sub = data + + ## filter to species list + if(!is.null(.fishbase_names)) { + data_sub <- data[which(data$fishbase_names %in% .fishbase_names),] + } + ## filter by taxonomic levels requested + else if(!is.null(.taxo_level)) { + if(taxo_level == "genus") { + if(length(which(!data_sub$genus %in% .taxo_levels)) == nrow(data_sub)) { + print("Error: no requested genus is in dataset") + return() + } + else { + data_sub <- data_sub[which(data_sub$genus %in% .taxo_levels),] + } + } + else if(.taxo_level == "family") { + if(length(which(!data_sub$family %in% taxo_levels)) == nrow(data_sub)) { + print("Error: no requested family is in dataset") + return() + } + else { + data_sub <- data_sub[which(data_sub$family %in% .taxo_levels),] + } + } + else if(.taxo_level == "order") { + if(length(which(!data_sub$order %in% .taxo_levels)) == nrow(data_sub)) { + print("Error: no requested order is in dataset") + return() + } + else { + data_sub <- data_sub[which(data_sub$order %in% .taxo_levels),] + } + } + else if(length(which(!data_sub$class %in% .taxo_levels)) == nrow(data_sub)) { + if(!data_sub$class %in% .taxo_levels) { + print("Error: no requested class is in dataset") + return() + } + else { + data_sub <- data_sub[which(data_sub$class %in% .taxo_levels),] + } + } + + ## filter by datasets + if(!is.null(.datasets)) { + data_sub <- data_sub[which(data_sub$dataset %in% .datasets),] + } + ## filter by realms + if(!is.null(.realms)) { + data_sub <- data_sub[which(data_sub$realm %in% .realms),] + } + ## filter by the taxonomic scale of observation + if(!is.null(.taxo_scale_observation)) { + data_sub <- data_sub[which(data_sub$taxo_scale_observation %in% .taxo_scale_observation),] + } + ## filter to traits of interest + if(!is.null(.trait_names)) { + data_sub <- data_sub[which(data_sub$trait_name %in% .trait_names),] + } + ## filter to traits of interest + if(!is.null(.trait_types)) { + data_sub <- data_sub[which(data_sub$trait_types %in% .trait_types),] + } + + ## filter to geographic region + if(.geographic_region != "global") { + ## make sure specified coordinates make sense + ## check lat min < lat max, within -90 to 90 + ## check lon min < lon max, within -180 to 180 + } + + } + return(data_sub) +} diff --git a/R/01_plotting-functions.R b/R/01_plotting-functions.R index 70eb991..80b5326 100644 --- a/R/01_plotting-functions.R +++ b/R/01_plotting-functions.R @@ -1,27 +1,138 @@ ## writing functions to map the data library(tidyverse) +## source shared functions +source("R/00_shared-functions.R") + ## read in the test data load("data-raw/test_phenofish.RData") - data <- test_phenofish -plot_observations <- function(fishbase_name = NULL, +## convert lat lon to numeric +data$latitude <- as.numeric(as.character(data$latitude)) +data$longitude <- as.numeric(as.character(data$longitude)) + +## add fake classification info +data$genus = c("Bloops") +data$family = c("Bloopidae") +data$order = c("Fisherii") +data$class = c("Swimmyfishii") +data$realm = rep(c("Freshwater", "Marine"), 1000)[1:nrow(data)] + + +## set all arguments to NULL by default +fishbase_names = NULL +taxo_level = NULL +taxo_levels = NULL +datasets = NULL +realms = NULL +taxo_scale_observation = NULL +trait_names = NULL +trait_types = NULL +geographic_region = NULL + +## set arguments to different things to test +fishbase_names <- unique(data$fishbase_name)[1:10] +taxo_level = "family" +taxo_levels = "Bloopidae" +realms <- c("Freshwater") +geographic_region = "global" + + +## test the filter by arguments function +test <- filter_by_arguments(fishbase_names = NULL, + taxo_level = taxo_level, + taxo_levels = taxo_levels, + datasets = datasets, + realms = realms, + taxo_scale_observation = taxo_scale_observation, + trait_names = trait_names, + trait_types = trait_types, + geographic_region = geographic_region, + data = data) + +plot_observations <- function(fishbase_names = NULL, taxo_level = NULL, - dataset = NULL, - realm = NULL, + taxo_levels = NULL, + datasets = NULL, + realms = NULL, taxo_scale_observation = NULL, - trait_name = NULL, - trait_type = NULL, - global = NULL) { + trait_names = NULL, + trait_types = NULL, + geographic_region = geographic_region, + data = data) { + + ## filter Phenofish data by arguments: + data_sub <- filter_by_arguments(.fishbase_names = fishbase_names, + .taxo_level = taxo_level, + .taxo_levels = taxo_levels, + .datasets = datasets, + .realms = realms, + .taxo_scale_observation = taxo_scale_observation, + .trait_names = trait_names, + .trait_types = trait_types, + .geographic_region = geographic_region, + .data = data) + + ## if dataset is null + if(is.null(data_sub)) { + print("Cannot plot observations! Error in data filtering") + return() + } + ## or if filtering resulted in dataset with no geo referenced data + else if(length(which(is.na(data_sub$latitude))) == nrow(data_sub)) { + print("Cannot plot observations! No observations in filtered dataset contain geographic coordinates") + return() + } - ## filter the full Phenofish dataset according to arguments passed to function + ## otherwise, make the ggplot + library(ggplot2) + countries <- map_data("world") - + ## remove missing points + data_sub <- data_sub[which(!is.na(data_sub$latitude)) & which(!is.na(data_sub$longitude)), ] - return() + map <- ggplot(countries, aes(x=long, y=lat, group = group)) + theme_minimal() + + geom_polygon(fill = "grey") + + coord_fixed() + + geom_point(data = data_sub, aes(y = latitude, x = longitude), size = 1, + inherit.aes = F) + + labs(x = "Latitude", y = "Longitude") + + theme(legend.title = element_text(size = 9)) + + return(map) } + +## test it +plot_observations(fishbase_names = fishbase_names, + taxo_level = NULL, + taxo_levels = NULL, + datasets = datasets, + realms = realms, + taxo_scale_observation = taxo_scale_observation, + trait_names = trait_names, + trait_types = trait_types, + geographic_region = geographic_region, + data = data) + +plot_observations(fishbase_names = fishbase_names, + taxo_level = NULL, + taxo_levels = NULL, + datasets = c("fishmorph"), + realms = realms, + taxo_scale_observation = taxo_scale_observation, + trait_names = trait_names, + trait_types = trait_types, + geographic_region = geographic_region, + data = data) + + + + + + + plot_phylo <- function() {} plot_diagnostics <- function() {} @@ -30,5 +141,6 @@ plot_by_ecoregion <- function() {} +## add "save" as a parameter