plotting specaccum{vegan} objects in ggplot2?

1,937 views
Skip to first unread message

Arne Erpenbach

unread,
Oct 15, 2012, 8:22:24 AM10/15/12
to ggplot2
Hi all,


has anyone out there plotted 'specaccum' and 'fitspecaccum' objects from
{vegan} with ggplot2 and would be willing to share some experience (and
code)?

I am posting some examples below, including a plot via
plotspecaccum{BiodiversityR} which I would like to rebuild in ggplot2.


Cheers,
a+


################################################################
library(vegan)
data(dune.env)
data(dune)

acc <- specaccum(dune, method="random", permutations=2)
# Low nr. of permutations just to check the structure!
str(acc)
# adds arrhenius (power) models to the randomized SACurves:
mod <- fitspecaccum(acc, "arrh")
class(mod)
str(mod) # very complicated stucture!
# plots the specaccum-object:
plot(acc)
# plots the nlm's:
plot(mod, col=rgb(255,0,0,50, maxColorValue=255), add=TRUE)
# gives number of model with best AIC:
best <- which((sapply(mod$models, AIC)) == min(sapply(mod$models, AIC)))
# summarizes the best model, including k and z values.
mod$models[best]

############################################################
# Plotting a single nl-model from fitspeccaccum{vegan} is complictated,
# this is only a quick-and-dirty workaround:
# (first, include more permutations!)
acc <- specaccum(dune, method="random", permutations=100)
mod <- fitspecaccum(acc, "arrh")

plot(acc)
plot(mod, col=rgb(0,0,255,20, maxColorValue=255), add=TRUE)

best <- which((sapply(mod$models, AIC)) == min(sapply(mod$models, AIC)))
# workaround to get the best model out of the object:
# recycle, to get a plottable object of the class speccacum:
workaround <- acc
# replace y-values with model fit:
workaround$richness <- mod$fitted[,best]
plot(workaround, col=rgb(255,0,0,150, maxColorValue=255), add=TRUE)

#### Some plots I'd like to rebuild using ggplot2:
plot(acc, ci.type="polygon", col=rgb(1,1,1), lwd=1, ci.lty=0,
ci.col=rgb(150,150,180,127, maxColorValue=255))
plot(workaround, add=TRUE, col=rgb(255,0,0,50, maxColorValue=255), lwd=2)
plot(workaround, ci.type="polygon", col=rgb(1,1,1), lwd=1, ci.lty=0,
ci.col=rgb(1,1,1,70, maxColorValue=255), add=TRUE)
mod$models[best]
# Side problem: how to access z-values from the object?
str(mod$models[best]) # It's a list!
#######################################################


################################################################################
# Approach of data handling using factors which I need to rebuild:
library(BiodiversityR)
accumcomp(dune, y=dune.env, factor="Management", method="exact",
permutations=100)
################################################################################

trichter

unread,
Jun 22, 2015, 1:53:51 PM6/22/15
to ggp...@googlegroups.com, arne.er...@gmx.de

A little late, but i encountered the same problem. The only google hit was this 3 year old thread btw :)

Anyway, here is what i came up with


#

library(vegan)
data(dune.env)
data(dune)

acc <- specaccum(dune, method="random", permutations=2)

#creating a dataframe for ggplot2
data <- data.frame(Sites=acc$sites, Richness=acc$richness, SD=acc$sd)

ggplot() +
  geom_point(data=total, aes(x=Sites, y=Richness)) +
  geom_line(data=total, aes(x=Sites, y=Richness)) +
  geom_ribbon(data=total ,aes(x=Sites, ymin=(Richness-2*SD),ymax=(Richness+2*SD)),alpha=0.2)


In my case, i improved from here

http://s28.postimg.org/nvg57pfbh/image.png

to here

http://s24.postimg.org/x496dnw4l/image.png

One could of course use ci.type="polygon" in the plot(acc) call, but i still like the ggplot2 version better.
Reply all
Reply to author
Forward
0 new messages