shar | Species habitat associations in R

| CI | Development | CRAN | License | | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- | | R-CMD-check | Project Status | CRAN status | JOSS | | codecov | Lifecycle: stable | CRAN logs | License: GPL v3 |

Species-habitat associations in R is a R package to analyze species-habitat associations. Therefore, information about the location of the species is needed (as a point pattern) and about the environmental conditions (as a raster map). In order to analyse the data for significant habitat associations either the location data or the environmental data is randomized n-times. Then, counts within the habitats are compared between the randomized data and the observed data. Positive or negative associations are present if the observed counts is higher or lower than the randomized counts (using quantile thresholds). Methods are mainly described in Plotkin et al. (2000), Harms et al. (2001) and Wiegand & Moloney (2014). shar is mainly based on the spatstat (Baddeley et al. 2015) and raster (Hijmans 2017) package.


The shar package is part of our academic work. To cite the package or acknowledge its use in publications, please cite the following paper.

Hesselbarth, M.H.K., (2021). shar: A R package to analyze species-habitat associations using point pattern analysis. Journal of Open Source Software, 6(67), 3811.

The get a BibTex entry, please use citation("shar").


You can install the released version of shar from CRAN with:


And the development version from GitHub with:



This also automatically installs all non-base R package dependencies, namely the following packages: classInt, raster, spatstat.core, spatstat.geom.

How to use shar



shar comes with build-in example data sets. species_a and species_b are exemplary location of species, e.g. trees, as ppp-objects from the spatstat package. landscape contains exemplary continuous environmental data. However, all methods depend on discrete data. Therefore we need to classify the data first. However, all methods require “fully mapped data” in a sense that NA cells of the environmental data are allowed only if simultaneously these areas cannot accommodate any locations of the point pattern (e.g., a water body within a forest area). This needs to be reflected in the observation window of the point pattern. For the torus translation method, no NA values are allowed at all.

landscape_classified <- classify_habitats(raster = landscape, n = 5, style = "fisher")

There are two possibilities to randomize the environmental data, both described in Harms et al. (2001). The first shifts the habitat map in all 4 cardinal directions around a torus. The second one assigns the habitat values to an empty map using a random walk algorithm. Both functions return a list with randomized rasters and the observed one. For more information on the methods, please click here.

torus_trans <- translate_raster(raster = landscape_classified)

random_walk <- randomize_raster(raster = landscape_classified, n_random = 99)

To plot the randomized raster, you can use the plot function and specify the number of raster as as well as the color palette used for the discrete environmental data.

col_palette <- c("#440154FF", "#3B528BFF", "#21908CFF", "#5DC863FF", "#FDE725FF")

plot(torus_trans, n = 3, col = col_palette)

To randomize the point pattern, either use the Gamma test described by Plotkin et al. (2000) or pattern reconstruction (Kirkpatrick et al. 1983; Tscheschel & Stoyan 2006).

gamma_test <- fit_point_process(pattern = species_b, process = "cluster", n_random = 99)

# (this can takes some time)
reconstruction <- reconstruct_pattern(pattern = species_b, n_random = 99, e_threshold = 0.05)

Of course, there are several utility functions. For example, you can plot the summary function of the observed pattern and the simulation envelopes of randomized patterns (what = "sf") or some randomized and the observed pattern (what = "pp") using the plot function.

plot(reconstruction, what = "pp")

Another utility functions allows to calculate the differences between the observed pattern and the randomized patterns (also called energy using summary functions).

calculate_energy(reconstruction, return_mean = TRUE)
## [1] 0.04908566

The data was created that species_a has a negative association to habitat 4 and species_b has a positive association to habitat 5, which is reflected in the results.

Given the characteristics of the method, a positive association to one habitat inevitably leads to a negative association to at least one of the other habitats (and vice versa; Yamada et al. 2006). For example, a high amount of individual points in the positively associated habitat simultaneously mean that less individual points can be present in the other habitats.

Furthermore, please be aware that due to the randomization of the null model data, results might slightly differ between different randomization approaches (e.g., fit_point_process() vs. translate_raster()) and even for repetitions of the same approach. Thus, the exact lo and hi thresholds might be slightly different when re-running the examples. However, the counts of the observed data should be identical, and general results and trends should be similar.

significance_level <- 0.01

results_habitat_association(pattern = species_a, raster = torus_trans, significance_level = significance_level)
## > Input: randomized raster
## > Quantile thresholds: negative < 0.005 || positive > 0.995
##   habitat breaks count lo hi significance
## 1       1     NA    35 10 35         n.s.
## 2       2     NA    44 19 53         n.s.
## 3       3     NA    36 15 49         n.s.
## 4       4     NA     4 15 58     negative
## 5       5     NA    73 48 90         n.s.

results_habitat_association(pattern = reconstruction, raster = landscape_classified, significance_level = significance_level)
## > Input: randomized pattern
## > Quantile thresholds: negative < 0.005 || positive > 0.995
##   habitat breaks count    lo    hi significance
## 1       1     NA     6 21.96 49.02     negative
## 2       2     NA    18 32.47 64.51     negative
## 3       3     NA    18 26.98 56.10     negative
## 4       4     NA    21 17.98 40.00         n.s.
## 5       5     NA   129 24.96 52.02     positive

Contributing and Code of Conduct

Contributions to shar are highly welcomed and appreciated. This includes any form of feedback, bug reports, feature requests/suggestions, or general questions about the usage.

Please note that the shar package is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.

To see how to contribute to this project, please see the Contributing guidelines.


Baddeley, A., Rubak, E., Turner, R., 2015. Spatial point patterns: Methodology and applications with R. Chapman and Hall/CRC Press, London. ISBN 978-1-4822-1020-0

Harms, K.E., Condit, R., Hubbell, S.P., Foster, R.B., 2001. Habitat associations of trees and shrubs in a 50-ha neotropical forest plot. Journal of Ecology 89, 947–959.

Hijmans, R.J., 2019. raster: Geographic data analysis and modeling. R package version 2.9-5.

Kirkpatrick, S., Gelatt, C.D.Jr., Vecchi, M.P., 1983. Optimization by simulated annealing. Science 220, 671–680.

Plotkin, J.B., Potts, M.D., Leslie, N., Manokaran, N., LaFrankie, J.V., Ashton, P.S., 2000. Species-area curves, spatial aggregation, and habitat specialization in tropical forests. Journal of Theoretical Biology 207, 81–99.

Tscheschel, A., Stoyan, D., 2006. Statistical reconstruction of random point patterns. Computational Statistics and Data Analysis 51, 859–871.

Wiegand, T., Moloney, K.A., 2014. Handbook of spatial point-pattern analysis in ecology. Chapman and Hall/CRC Press, Boca Raton. ISBN 978-1-4200-8254-8

Yamada, T., Tomita, A., Itoh, A., Yamakura, T., Ohkubo, T., Kanzaki, M., Tan, S., Ashton, P.S., 2006. Habitat associations of Sterculiaceae trees in a Bornean rain forest plot. Journal of Vegetation Science 17, 559–566.

mhesselbarth/SHAR documentation built on Jan. 17, 2022, 2:02 p.m.