title: "rangeMapper" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{rangeMapper} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8}


library(knitr)
opts_chunk$set(
  warning = FALSE, 
  message = FALSE, 
  fig.height = 5,   
  collapse = TRUE,
  comment = "#>"
  )

Packages and data

require(rangeMapper)
require(sf)
require(data.table)
require(ggplot2)
require(viridis)

data(dem)
wrens = read_wrens()
wrens$breeding_range_area = st_area(wrens)

A bare bone rangeMapper project

The project contains nothing but several system tables.

# path is not specified so an in-memory file is created.
con = rmap_connect()

Wrens breeding ranges are imported.

rmap_add_ranges(con, x = wrens, ID = 'sci_name')
rmap_prepare(con, 'hex', cellsize = 500)
rmap_add_bio(con, wrens, 'sci_name')

Do larger wren species have smaller clutches ?

lm(clutch_size~log(body_mass), wrens)  %>%  summary

Does the slope clutch size ~ body mass vary spatially?

First we save a species richness map.

rmap_save_map(con)

Then we construct a subset table with all assemblages with a richness of at least 10 species.

rmap_save_subset(con,'sset1', species_richness = 'species_richness > 10')

Now we can construct a clutch size ~ body mass map with assemblages containing at least 10 species.

linmod = function(x) {
  lm(clutch_size ~ log(body_mass), x) %>% 
    summary %>% coefficients %>% data.table %>% .[-1] }

rmap_save_map(con, fun= linmod, subset= 'sset1', src='wrens', dst='slope_clutch_size')

We get the map as a sf data.frame and plot it with ggplot.

x = rmap_to_sf(con)

ggplot() + 
  geom_sf(data = x, aes(fill = Estimate),  size= 0.05) + 
  scale_fill_gradientn(colours =  viridis(10, option = 'E'), na.value= 'grey80') + 
  theme_bw()

Here is the "answer" to the question above.

xy = st_centroid(x)  %>% st_coordinates
x = cbind(x, xy )

ggplot(x , aes(y = Estimate, x = Y) ) + 
  geom_point() + 
  geom_smooth() +
  theme_bw() + 
  ylab('Clutch size ~ Body mass slope') + 
  xlab('Distance from equator (km)')


mpio-be/rangeMapper documentation built on Oct. 6, 2022, 7:42 p.m.