Skip to content

Instantly share code, notes, and snippets.

@fkeck
Created December 17, 2021 14:21
Show Gist options
  • Select an option

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

Select an option

Save fkeck/05719cedacc931af1c427bb132f2edb1 to your computer and use it in GitHub Desktop.
tidy vegan specaccum plot for ggplot2
#' Convert vegan specaccum results to tidy format
#'
#' @param x result returned by specaccum
#'
#' @return A data.frame in "long format".
#'
tidy_specaccum <- function(x) {
data.frame(
site = x$sites,
richness = x$richness,
sd = x$sd)
}
library(vegan)
library(ggplot2)
library(magrittr)
data(BCI)
# Fit rarefaction curves with vegan
sac <- specaccum(BCI)
# Tidying
tidy_sac <- tidy_specaccum(sac)
# Plot with ggplot
tidy_sac %>%
ggplot() +
geom_line(aes(site, richness)) +
geom_linerange(aes(x = site, ymin = richness - 2*sd, ymax = richness + 2*sd)) +
ylim(0, NA)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment