Skip to content

Instantly share code, notes, and snippets.

@fkeck
Created March 25, 2020 12:10
Show Gist options
  • Select an option

  • Save fkeck/a43b8f5d42e806b401c521e1309ea34e to your computer and use it in GitHub Desktop.

Select an option

Save fkeck/a43b8f5d42e806b401c521e1309ea34e to your computer and use it in GitHub Desktop.
#' Convert vegan rarecurve results to tidy format
#'
#' @param x a list of rarefy results returned by rarecurve
#' @param sites a vector of site names
#'
#' @return A data.frame in "long format".
#'
tidy_rarecurve <- function(x, sites) {
long_list <- mapply(function(x, sites) {
data.frame(
site = sites,
samp_names = names(x),
samp_size = attr(x, "Subsample"),
species = x)},
x = x, sites = sites, SIMPLIFY = FALSE)
do.call(rbind, long_list)
}
# DEMO
library(vegan)
library(ggplot2)
library(magrittr)
data(BCI)
# Fit rarefaction curves with vegan
rc <- rarecurve(BCI, step = 20, col = "blue", cex = 0.6)
# Tidying
tidy_rc <- tidy_rarecurve(rc, sites = rownames(BCI))
# Plot with ggplot
tidy_rc %>%
ggplot() +
geom_line(aes(samp_size, species, group = site))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment