Skip to content

Commit

Permalink
version 0.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
dlizcano committed Mar 29, 2024
1 parent 509a21a commit 5902f68
Show file tree
Hide file tree
Showing 46 changed files with 548 additions and 207 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
^codecov\.yml$
^doc$
^Meta$
^cran-comments\.md$
21 changes: 14 additions & 7 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
Package: mammalcol
Title: Access to the List of Mammals Species of Colombia
Version: 0.1.0
Title: Access to the List of Mammal Species of Colombia
Version: 0.2.1
Authors@R:
c(
person("Diego J.", "Lizcano", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0000-0002-9648-0576"))
Date: 2024
comment = c(ORCID = "0000-0002-9648-0576")),
person("Baltazar", "González", , "[email protected]", role = c("ctb"),
comment = c(ORCID = "0000-0001-5805-8107")),
person("Alejandra", "Bonilla-Sánchez", , "[email protected]", role = c("ctb"),
comment = c(ORCID = "0000-0003-0803-5884"))
)
Date: 2024-03-28
Maintainer: Diego J. Lizcano <[email protected]>
Description: Allows easy access to the data found in the species list featured
in the renowned "Ramírez-Chaves, et al. 2021. Checklist of the Mammals
(Mammalia) of Colombia”. Mammalogy Notes 7(2),253.
<https://doi.org/10.47603/mano.v7n2.253>. This publication stands as
<doi:10.47603/mano.v7n2.253>. This publication stands as
one of Colombia's most comprehensive reviews of mammal diversity. The
dataset incorporates detailed species accounts and has been
meticulously structured for effortless utilization within the R
environment.
License: MIT + file LICENSE
URL: https://github.com/dlizcano/mamalcol,
URL:
https://github.com/dlizcano/mammalcol,
https://dlizcano.github.io/mammalcol/
BugReports: https://github.com/dlizcano/mammalcol/issues
Depends:
R (>= 2.10)
Imports:
ggplot2,
magrittr
magrittr,
Suggests:
dplyr,
finch,
Expand Down
13 changes: 12 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
# mammalcol 0.2.1

* Added new option to function mammalmap() to retrieve the ggplot object and select if want the legend or not, as suggested by Baltazar González.
* Some typos corrected.
* Contributors Baltazar González y Alejandra Bonilla-Sánchez added.


# mammalcol 0.2.0

* Added sp_by_depto() function

# mammalcol 0.1.0

* Initial CRAN submission.
* Added vignettes
* Added search_mammalcol() function
* Added mammalmap() function
* Added sp_by_depto() function

17 changes: 11 additions & 6 deletions R/colmap.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#' colmap Dataset
#'
#' The colmap dataset Simple feature collection with 33 features and 11 fields. This version
#' was obtained from: GDAM using:
#' The colmap dataset is a simple feature collection with 33 features and 11 fields. This version
#' was obtained from: GDAM using the sf package.
#'
#' colmap <- sf::st_as_sf(gadm(country="COL", level=1, path=tempdir()))
#' al later apply st_simplify with a tolerance of 1km:
#'
#' and later apply a polygon simplify using st_simplify with a tolerance of 1km:
#'
#' colmap <- st_simplify(colmap, preserveTopology = FALSE, dTolerance = 1000)
#'
#' @format Simple feature collection with 33 features and 11 fields:
Expand All @@ -23,10 +26,11 @@
#' }
#'
#'
#' @details This dataset is designed to provide users with a companion map to
#' plot the mammal distribution per departamento
#' @details This dataset is designed to provide users of mammalcol package with a companion map to
#' plot the mammal distribution per departamento.
#'
#' @examples
#' \dontrun{
#' # Load the mammalcol package
#' library(mammalcol)
#'
Expand All @@ -35,6 +39,7 @@
#'
#' # Display the first few rows
#' head(colmap)
#'}
#'
#' @seealso
#' For more information about the "mammalcol" package and the data sources, visit
Expand All @@ -46,5 +51,5 @@
#' @author
#' Data compilation: Ramírez-Chaves 2021, Package implementation: Diego J. Lizcano
#'
#' @keywords dataset
#' @keywords internal
"colmap"
13 changes: 8 additions & 5 deletions R/distribution.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
#' regarding mammal species distribution documented in Colombia, sourced from the "List of the
#' mammals of Colombia" compiled by Ramírez-Chaves 2021. It encompasses various
#' details, including locality, source, threat status and appendix CITES of each species.
#' The table is a variant of the distribution table from https://doi.org/10.15472/kl1whs
#' The table is a variant of the distribution table from: <doi:10.15472/kl1whs>
#'
#'
#' @format A data frame with 548 rows and 8 columns:
#' \describe{
Expand All @@ -20,9 +21,9 @@
#'
#'
#' @details This dataset is designed to provide users with comprehensive
#' information about the mammal species found in Colombia, as documented
#' by Ramírez-Chaves 2021. It is organized for easy access and utilization within
#' the R environment.
#' information about the mammal species found in each departamento of Colombia,
#' as documented by Ramírez-Chaves 2021. The data set is organized for easy
#' access and utilization within the R environment.
#'
#' @seealso
#' For more information about the "mammalcol" package and the data sources, visit
Expand All @@ -34,5 +35,7 @@
#' @author
#' Data compilation: Ramírez-Chaves 2021, Package implementation: Diego J. Lizcano
#'
#' @keywords dataset
#' @keywords internal
"distribution"


67 changes: 47 additions & 20 deletions R/mammalmap.R
Original file line number Diff line number Diff line change
@@ -1,35 +1,46 @@

#' Plot a map Data for one species of the List of the Mammals of Colombia
#' Plot a map for one species of the List of the Mammals of Colombia
#'
#' This function retrieve the locality data for one species in
#' the MammalCol pacakage dataset, and returns a map of Departamentos
#' where the species has been recorded.
#' the mammalcol pacakage dataset, and returns a map using ggplot2 of the
#' departamentos where the species has been recorded.
#'
#' The function does not allows fuzzy matching, so the specie name should be correct.
#' It is advised to run first the search_mammalcol function.
#'
#' @param species A character string containing the name of the species to plot the map.
#' @param species A character string containing the name of one species present in
#' Colombia to plot a map of presence by departamento.
#'
#' @param legend A logical (TRUE or FALSE) to get the legend in the map when is TRUE,
#' or not if it is FALSE.
#'
#'
#' @return A map for the species.
#' @return A ggplot2 map for the species.
#'
#' @examples
#'
#' species <- "Tapirus pinchaque"
#' mammalmap(species)
#' mammalmap(species, legend=FALSE)
#'
#' @importFrom mammalcol taxon
#' @importFrom mammalcol colmap
#' @export
mammalmap <- function(species){
mammalmap <- function(species, legend=TRUE){

if (!requireNamespace("ggplot2", quietly = TRUE))
install.packages("ggplot2")
if (!requireNamespace("sf", quietly = TRUE))
install.packages("sf")

if (missing(species)) {
stop("Argument species was not included")
}

if (!is.character(species)) {
stop(paste0("Argument species must be a character, not ", class(Species)))
stop(paste0("Argument species must be a character, not ", class(species)))
}

if (!is.logical(legend)) {
stop(paste0("Argument legend must be logical, not ", class(legend)))
}

# require("ggplot2")
Expand All @@ -38,6 +49,9 @@ mammalmap <- function(species){
#load("data/colmap.rda")
#load("data/taxon.rda")

#data(mammalcol::taxon)
#data(mammalcol::colmap)

distribution_list <-
strsplit(taxon$distribution, "\\|") # trimws () removes spaces

Expand All @@ -53,14 +67,27 @@ mammalmap <- function(species){
}

# make the map
ggplot2::ggplot(colmap) +
ggplot2::geom_sf(ggplot2::aes(fill = NAME_1)) +
ggplot2::scale_fill_manual(values = deptos$fill) +
# ggtitle(taxon$scientificName[25]) + #species name number
ggplot2::labs(subtitle = taxon$scientificName[sp_id])+
ggplot2::theme(legend.position="right", # locatio legend
legend.title = ggplot2::element_blank(),#element_text(size=7),#,
legend.text = ggplot2::element_text(size=7,), # text depto size
plot.subtitle = ggplot2::element_text(face = "italic") # italica
)#,
}
# if legend true
if(legend==TRUE) {
mapa <- ggplot2::ggplot(colmap) +
ggplot2::geom_sf(ggplot2::aes(fill = NAME_1)) +
ggplot2::scale_fill_manual(values = deptos$fill) +
# ggtitle(taxon$scientificName[25]) + #species name number
ggplot2::labs(subtitle = taxon$scientificName[sp_id])+
ggplot2::theme(legend.position="right", # location legend
legend.title = ggplot2::element_blank(),#element_text(size=7),#,
legend.text = ggplot2::element_text(size=8,), # text depto size
plot.subtitle = ggplot2::element_text(face = "italic") # italica
)
}else{ # if legend false
mapa <- ggplot2::ggplot(colmap) +
ggplot2::geom_sf(ggplot2::aes(fill = NAME_1), show.legend = FALSE) + # removes legend
ggplot2::scale_fill_manual(values = deptos$fill) +
# ggtitle(taxon$scientificName[25]) + #species name number
ggplot2::labs(subtitle = taxon$scientificName[sp_id]) +
ggplot2::theme(plot.subtitle = ggplot2::element_text(face = "italic")
)# italica
}

return(mapa)
} # end function
98 changes: 98 additions & 0 deletions R/search_mammalcol.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@

#' Retrieve Data from the List of the Mammals of Colombia
#'
#' This function takes a list of Mammal species names, searches for their data in
#' the MammalCol pacakage dataset, and returns a data frame containing the relevant
#' information for each species.
#'
#' The function allows fuzzy matching for species names with a maximum
#' distance threshold to handle potential typos or variations in species names.
#'
#' @param splist A character vector containing the names of the species to search for.
#' @param max_distance The maximum allowed distance for fuzzy matching of species names.
#' Defaults to 0.2.
#'
#' @return A data frame containing the retrieved information for each species.
#'
#' @examples
#'
#' splist <- c("Tapirus bairdii", "Tapirus pinchaque", "Tapirus terrestris",
#' "Tapirus terrestris", "tapir terrestre", "Pudu mephistophiles")
#'
#' search_mammalcol(splist)
#'
#' @importFrom mammalcol taxon
#'
#' @export
search_mammalcol <- function(splist, max_distance = 0.2) {
# Defensive function here, check for user input errors
if (is.factor(splist)) {
splist <- as.character(splist)
}
# Fix species name
splist_st <- standardize_names(splist)
dupes_splist_st <- find_duplicates(splist_st)

if(length(dupes_splist_st) != 0 ){
message("The following names are repeated in the 'splist': ",
paste(dupes_splist_st, collapse = ", "))
}
splist_std <- unique(splist_st)

# create an output data container
output_matrix <- matrix(nrow = length(splist_std), ncol = 21) # two more
colnames(output_matrix) <- c("name_submitted",
names(taxon),
"Distance")

# loop code to find the matching string

for (i in seq_along(splist_std)) {
# Standardise max distance value
if (max_distance < 1 & max_distance > 0) {
max_distance_fixed <- ceiling(nchar(splist_std[i]) * max_distance)
} else {
max_distance_fixed <- max_distance
}

# fuzzy and exact match
matches <- agrep(splist_std[i],
taxon$scientificName, # base data column
max.distance = max_distance_fixed,
value = TRUE)

# check non matching result
if (length(matches) == 0) {
row_data <- rep("nill", 19) # number of columns
}
else if (length(matches) != 0){ # match result
dis_value <- as.numeric(utils::adist(splist_std[i], matches))
matches1 <- matches[dis_value <= max_distance_fixed]
dis_val_1 <- dis_value[dis_value <= max_distance_fixed]

if (length(matches1) == 0){
row_data <- rep("nill", 19) # number of columns
}
else if (length(matches1) != 0){
row_data <- as.matrix(taxon[taxon$scientificName %in% matches1,])
}
}

# distance value
if(is.null(nrow(row_data))){
dis_value_1 <- "nill"
} else{
dis_value_1 <- utils::adist(splist_std[i], row_data[,2])
}

output_matrix[i, ] <-
c(splist_std[i], row_data, dis_value_1)
}

# Output
output <- as.data.frame(output_matrix)
# rownames(output) <- NULL
output <- output[,-2]# delete the id column
return(output[output$scientificName != "nill",])
}

5 changes: 4 additions & 1 deletion R/sp_by_depto.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
#' occur only in all provided (no more, no less) \code{states} and \code{all} matches
#' mammal species that occur at least in all \code{states} passed. See examples.
#' @param taxa optional character vector to match against the departamentos. Use the order name
#' @export
#'
#' @return a data frame
#'
#' @examples
#' \dontrun{
#' occ.any <- sp_by_depto(c("Arauca", "Norte de Santander"), type = "any")
Expand All @@ -23,10 +24,12 @@
#' head(occ.taxa)
#' }
#'
#' @export
sp_by_depto <- function(states, type = c("any", "only", "all"), taxa = NULL) {
if (length(states) == 0) stop("Please provide at least one Colombian Departamento")
type <- match.arg(type)
states <- sort(states)

# states <- paste("BR-", states, sep = "")
if (length(states) == 0) stop("Please provide at least one Colombian Departamento")
# res <- lapply(occurrences, match, states)
Expand Down
Binary file added R/sysdata.rda
Binary file not shown.
Loading

0 comments on commit 5902f68

Please sign in to comment.