Skip to content

Commit

Permalink
updating plotting functions & shared functions
Browse files Browse the repository at this point in the history
  • Loading branch information
nicole-a-moore committed Oct 13, 2023
1 parent 8c1b575 commit 57bfc05
Show file tree
Hide file tree
Showing 2 changed files with 224 additions and 10 deletions.
102 changes: 102 additions & 0 deletions R/00_shared-functions.R
Original file line number Diff line number Diff line change
@@ -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)
}
132 changes: 122 additions & 10 deletions R/01_plotting-functions.R
Original file line number Diff line number Diff line change
@@ -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() {}
Expand All @@ -30,5 +141,6 @@ plot_by_ecoregion <- function() {}



## add "save" as a parameter


0 comments on commit 57bfc05

Please sign in to comment.