In this vignette is discussed about outliers in Whittaker biomes plots using the plotbiomes package.

About outliers

Some precipitation-temperature points may end up outside of the Whittaker biome polygons. This is not unusual and Ricklefs (2008) mentiones that

"Most of the points fall within a triangular region that includes almost the full range of climates. Only the climates of high mountains do not fall within the triangle."

(Ricklefs, R. E. (2008), The economy of nature. W. H. Freeman and Company.; Chapter 5, Biological Communities, The biome concept; Caption of Figure 5.5)

Nevertheless, one might want to check such points. For example, temperature and precipitation values could be biased because the spatial location was not correctly specified (errors in coordinates). In such cases is very helpful to know the indices of the outliers. This is where the get_outliers() and map_outliers() functions from plotbiomes package come in handy.

As presented here one can extract temperature and precipitation values and construct a plot like the one below (note the outliers):

library(raster)
library(maptools)

# ===== Prepare raster stack
# Read temperature and precipitation as raster stack.
# Low resolution raster datasets come with 'plotbiomes' package.
path <- system.file("extdata", "temp_pp.tif", package = "plotbiomes")
temp_pp <- raster::stack(path)
names(temp_pp) <- c("temperature", "precipitation")

# ===== Generate random locations
data(wrld_simpl) # load world polygons from maptools
# Eliminate Antarctica - it is not represented in the raster datasets.
wrld_simpl <- wrld_simpl[wrld_simpl$NAME != "Antarctica", ]
# Create random locations within world's polygons.
set.seed(66) # random number generator
points <- sp::spsample(x = wrld_simpl, n = 50, type = "random")

# ===== Extract from raster stack
# Extract temperature and precipitation values from the raster datasets
extractions <- raster::extract(temp_pp, points, df = TRUE)
# Adjust temperature values to normal scale because WorldClim temperature data
# has a scale factor of 10 (integer storage for saving space).
extractions$temperature <- extractions$temperature/10
# Convert precipitation from mm to cm
extractions$precipitation <- extractions$precipitation/10
library(plotbiomes)
library(ggplot2)

whittaker_base_plot() +
  # add the temperature - precipitation points
  geom_point(data = extractions,
             aes(x = temperature,
                 y = precipitation),
             size   = 3,
             shape  = 21,
             colour = "gray95",
             bg     = "black",
             stroke = 1,
             alpha  = 0.5) +
  theme_bw()

Get the outliers

The function get_outliers() does exactly what its name suggests:

my_outliers <- get_outliers(tp = extractions[, 2:3])
my_outliers

Below is some code to color/underline the outliers on the Whittaker biome plot (check more coloring options in the vignette Whittaker_biomes_examples.html):

library(plotbiomes)
library(ggplot2)

# Color the outliers
extractions$status <- ifelse(extractions$ID %in% my_outliers$row_idx, "out", "in")

# Note that `fill` cannot be used again in the aesthetics 
# because is already in use for biome polygons color filling.
whittaker_base_plot() +
  geom_point(data = extractions,
             aes(x = temperature,
                 y = precipitation),
             shape  = 21,
             stroke = 1, # acts as the thickness of the boundary line
             colour = "gray95", # acts as the color of the boundary line
             size   = 3.5) +
  geom_point(data = extractions,
             aes(x = temperature,
                 y = precipitation,
                 color = status),
             shape = 16,
             size  = 3,
             alpha = 0.5) +
  scale_color_manual(name   = "Status",
                     breaks = c("in", "out"),
                     values = c("in"  = "black",
                                "out" = "red"),
                     labels = c("in"  = "inside",
                                "out" = "outlier")) +
  theme_bw()

Map the outliers

The function map_outliers() does exactly what its name suggests. This function makes use of the mapview package functionality to produce interactive views of temperature-precipitation outliers.

# With RMarkdown, `mapview` needs to be attached to actually make the map appear.
# This should not be needed when scripting though because
# `plotbiomes` package loads `mapview` and there is no need to call `library(mapview)`
library(mapview) 
map_outliers(tp =  extractions[, 2:3], xy = points@coords)

.

map_outliers() is a simple wrapper of mapview::mapView() function to which arguments can be passed:

map_outliers(tp =  extractions[, 2:3], xy = points@coords, 
             col.regions = "red", basemaps = c("OpenTopoMap", "Esri.WorldImagery"))


valentinitnelav/plotbiomes documentation built on Oct. 22, 2022, 6:22 a.m.