[R-sig-eco] Rarefaction curves in ggplot
Drew Tyre
@tyre2 @ending from unl@edu
Mon Dec 17 18:57:56 CET 2018
Hi Ellen,
I was curious about this because I will be using a lot of vegan functions in a spring class. Turns out not to be trivial - as with all ggplot problems the trick is to get the data into a dataframe. I tried several things, but ended up writing my own function:
# rarefraction curves and ggplot
library("tidyverse")
library("vegan")
data(BCI)
out <- rarecurve(BCI, step = 10, sample = 400)
names(out) <- 1:50
as_tibble_rc <- function(x){
# convert rarecurve() output to data.frame
# bind_rows doesn't work because items are different lengths
# also need to extract sample sizes from attribute
# Allocate result dataframe
nsamples <- map_int(x, length)
total_samples <- sum(nsamples)
if(!is.null(names(x))){
sites <- names(x)
} else {
sites <- as.character(1:length(nsamples))
}
result <- data_frame(Site = rep("", total_samples),
Sample_size = rep(0, total_samples),
Species = rep(0, total_samples))
start <- 1
for (i in 1:length(nsamples)){
result[start:(start + nsamples[i]-1), "Site"] <- sites[i]
result[start:(start + nsamples[i]-1), "Sample_size"] <- attr(x[[i]], "Subsample")
result[start:(start + nsamples[i]-1), "Species"] <- x[[i]]
start <- start + nsamples[i]
}
result
}
out <- as_tibble_rc(out)
# add grouping variable
sitedata <- data_frame(Site = as.character(1:50),
Type = sample(LETTERS[1:2], 50, replace = TRUE))
alldata <- left_join(out, sitedata, by = "Site")
# and then it's trivial
ggplot(data = alldata,
mapping = aes(x = Sample_size, y = Species, color = Type, group = Site)) +
geom_line()
hope that helps.
--
Drew Tyre
School of Natural Resources
University of Nebraska-Lincoln
416 Hardin Hall, East Campus
3310 Holdrege Street
Lincoln, NE 68583-0974
phone: +1 402 472 4054
fax: +1 402 472 2946
email: atyre2 using unl.edu
http://snr.unl.edu/tyre
http://drewtyre.rbind.io
The point is that our true nature is not some ideal that we have to live up to. It’s who we are right now, and that’s what we can make friends with and celebrate.
Excerpted from: Awakening Loving-Kindness by Pema Chödrön
On 12/17/18, 10:14 AM, "R-sig-ecology on behalf of Ellen Pape" <r-sig-ecology-bounces using r-project.org on behalf of ellen.pape using gmail.com> wrote:
Hi all,
As I use ggplot2 for all my graphs, I would like to use ggplot2 to
construct rarefaction curves as well (I also want to combine 2 rarefaction
curves in 1 plot using cowplot which also vows for using ggplot2).
I made these rarefaction curves using the rarecurve function in vegan, but
I don't see how I can use these results and put these in ggplot2.
I found the following website during my search:
https://urldefense.proofpoint.com/v2/url?u=https-3A__www.fromthebottomoftheheap.net_2015_04_16_drawing-2Drarefaction-2Dcurves-2Dwith-2Dcustom-2Dcolours_&d=DwICAg&c=Cu5g146wZdoqVuKpTNsYHeFX_rg6kWhlkLF8Eft-wwo&r=aLEaSryyUcERVqcKVZl7lQ&m=ScKzUcnIZJ2da4_YAAzmizwFy-CbI90_F4SV_FKSLLc&s=-DiwE4s5JljqtJk5ZhKOE4Benr3yDXmDM19K7wNG3bI&e=
but it does not explain how to draw rarefaction curves with ggplot2.
This website mentions - and this is what I would like to be able to do in
ggplot2:
"Where I do think this sort of approach might work is if the samples in the
data set come from a few different groups and we want to colour the curves
by group.
col <- c("darkred", "forestgreen", "hotpink", "blue")set.seed(3)grp <-
factor(sample(seq_along(col), nrow(BCI2), replace = TRUE))cols <-
col[grp]
The code above creates a grouping factor grp for illustration purposes; in
real analyses you'd have this already as a factor variable in you data
somewhere. We also have to expand the col vector because we are plotting
each line in a loop. The plot code, reusing elements from the previous
plot, is shown below:
plot(c(1, max(Nmax)), c(1, max(Smax)), xlab = "Sample Size",
ylab = "Species", type = "n")abline(v = raremax)for (i in seq_along(out)) {
N <- attr(out[[i]], "Subsample")
lines(N, out[[i]], col = cols[i])}
[image: An attempt at rarefaction curves output with custom colours per
groups of curves.]An attempt at rarefaction curves output with custom
colours per groups of curves.
but I don't see how I adjust this code to make the same plot in ggplot2.
Anyone?
Thanks!
Ellen
[[alternative HTML version deleted]]
_______________________________________________
R-sig-ecology mailing list
R-sig-ecology using r-project.org
https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Dsig-2Decology&d=DwICAg&c=Cu5g146wZdoqVuKpTNsYHeFX_rg6kWhlkLF8Eft-wwo&r=aLEaSryyUcERVqcKVZl7lQ&m=ScKzUcnIZJ2da4_YAAzmizwFy-CbI90_F4SV_FKSLLc&s=w7PfOR1D2XX19m5JJqRubGPuMylTRoX3JYCpGkFoqnM&e=
More information about the R-sig-ecology
mailing list