knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.height = 5, fig.width = 4, fig.align = 'center' )
Once you have transformed species distribution data into a presence absence matrix (PAM) in PresenceAbsence
format, you may wish to enhance it by incorporating additional variables. These variables typically exist in raster format, such as WorldClim bioclimatic data, or in shapefile format, for instance, global ecoregions.
To add variables in raster format to a PresenceAbsence
object we can use the function lets.addvar
from the letsR
package. This function takes a raster
object with any resolution and extent, and transform it to match the information in your PresenceAbsence
object. Subsequently, the variables are included as additional columns containing the aggregate/summarize value of the variable(s) in each cell of the presence-absence matrix. Let's see an example using the bioclimatic data from WorldClim.
library(letsR)
Here we will use the Average temperature raster in Celsius degrees (multiplied by 100) for the world in 10 arc min of resolution.
data(temp) r <- terra::unwrap(temp) # example data plot(r)
Here I will use the PresenceAbsence
object for Phyllomedusa species previously generated.
data(PAM) plot(PAM, main = "Phyllomedusa\nRichness")
We can now run the lets.addvar
function. Just make sure that the two objects are on the same projection before using the function. Also, note that the climatic data have a higher resolution than our PAM. In this case, we could choose a function to aggregate the values with the argument fun
. In most of the situations, people will be interested in averaging values to aggregate multiple cells, but in some specific cases you may want to sum them, or get the standard deviation, or use any another function.
PAM_env <- lets.addvar(PAM, r, fun = mean)
The result is a presence absence matrix, where the last columns now include the raster values. Check the table:
library(knitr) library(dplyr) library(kableExtra)
head(PAM_env)
kable(head(PAM_env), "html") %>% kable_styling() %>% scroll_box(width = "800px", height = "400px")
If you do not want the coordinates and species included you can set the argument onlyvar = TRUE
.
climate <- lets.addvar(PAM, r, fun = mean, onlyvar = TRUE)
head(climate)
kable(head(climate), "html") %>% kable_styling()
Now that we have the variables, we can use it to relate to our species data in many ways. For example, you could graph the relationship between temperature and species richness.
library(ggplot2)
rich <- rowSums(PAM$P[, -(1:2)]) mpg1 <- data.frame("Temperature" = climate[, 1]/10, "Richness" = rich) ggplot(mpg1, aes(Temperature, Richness)) + geom_smooth() + geom_point(col = rgb(0, 0, 0, .6)) + theme_bw()
Data in shapefile format like ecorregions, conservation units or countries, can be added to a PAM using the function lets.addpoly
. This function adds polygons' attributes as columns at the right-end of the matrix. The values represent the percentage of the cell covered by the polygon attribute used. As an example, we can use the South America countries map available in the package maptools
.
data("wrld_simpl") SA <- c("Brazil", "Colombia", "Argentina", "Peru", "Venezuela", "Chile", "Ecuador", "Bolivia", "Paraguay", "Uruguay", "Guyana", "Suriname", "French Guiana") south_ame <- wrld_simpl[wrld_simpl$NAME %in% SA, ] ggplot(data = south_ame) + geom_sf() + geom_sf_text(aes(label = ISO3)) + theme_bw()
Now we can add this variables to our PAM.
PAM_pol <- lets.addpoly(PAM, south_ame, "NAME")
head(PAM_pol)
kable(head(PAM_pol), "html") %>% kable_styling() %>% scroll_box(width = "800px", height = "400px")
This information can be used to calculate the number of species per country for example.
vars_col <- (ncol(PAM$P) + 1):ncol(PAM_pol) n <- length(vars_col) rich_count <- numeric(n) for (i in 1:n) { rich_count[i] <- sum(colSums(PAM$P[PAM_pol[, vars_col[i]] > 0, -(1:2)]) > 0) } labs <- as.factor(colnames(PAM_pol)[vars_col]) names(rich_count) <- labs
mpg <- data.frame("Richness" = rich_count, "Country" = as.factor(labs)) g <- ggplot(mpg, aes(labs, Richness)) g + geom_bar(stat = "identity") + labs(x = "") + theme(axis.text.x = element_text(angle = 90, hjust = 1))
To cite letsR in publications use: Bruno Vilela and Fabricio Villalobos (2015). letsR: a new R package for data handling and analysis in macroecology. Methods in Ecology and Evolution. DOI: 10.1111/2041-210X.12401
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.