关于plot:R 中的累积曲线

accumulation curve in R

我有几个月内在 4 个地点的物种数据。我已经使用 R 中的 vegan 包成功创建了累积图,但我想在一张图上绘制所有 4 个站点。

起初我有一个包含所有站点和月份的数据表,但是当我绘制 specaccum 时,结果是所有数据的累积曲线,而不考虑站点。

因此,我将每个站点拆分成一个单独的数据表,然后加载到 R 中。在每个数据表中,第一行是物种名称,下面的每一行是一个月。

例如,我加载了我的网站"FMR"之一的数据。然后我做了以下事情:

1
2
FMR <-specaccum(FMRJ2F,"random")
plot(FMR)

我对其他网站PPDUXPM也做了同样的事情。如何将所有 4 行放在一个图上?


你可以在 plot.specaccum(...)

中使用 add=T 参数

1
2
3
4
5
library(vegan)
data(BCI)
df <- lapply(c(1,21,41,61,81),function(i)specaccum(BCI[,seq(i,i+19)], method="random"))
plot(df[[1]])
for (i in 2:5) plot(df[[i]],add=T, col=i)

></p>
<p>此代码片段仅加载 <wyn>vegan</wyn> 中的内置 BSI 数据集,并通过在 BCI 中的列子集上运行 <wyn>specaccum(...)</wyn> 来创建 5<wyn>specaccum</wyn> 个对象的列表。你不需要这样做,因为你已经有了 specaccum 对象。</p>
<p>然后,我们创建第一个图,并用 <wyn>add=T</wyn>.</p>
<p> 添加每条新曲线</p>
<hr>
<p>好的,所以@jlhoward 的解决方案当然要简单得多,也更明智。但是,由于我没有想到显而易见的事情,并对此进行了编码,我想我不妨分享一下。对于手头的函数不接受 <wyn>add</wyn>.</p>
<p> 的相关问题,它可能很有用</p>
<p>加载库和一些示例数据:</p>
<div class=

1
2
3
4
5
6
7
8
library(vegan)
data(BCI)
sp1 <- specaccum(BCI, 'random')

# random modification to BCI data to create data for a second curve
BCI2 <- as.matrix(BCI)
BCI2[sample(prod(dim(BCI2)), 10000)] <- 0
sp2 <- specaccum(BCI2, 'random')

绘图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# Combine the specaccum objects into a list
l <- list(sp1, sp2)

# Calculate required y-axis limits
ylm <- range(sapply(l, '[[', 'richness') +
           sapply(l, '[[', 'sd') * c(-2, 2))

# Apply a plotting function over the indices of the list
sapply(seq_along(l), function(i) {
  if (i==1) { # If it's the first list element, use plot()
    with(l[[i]], {
      plot(sites, richness, type='l', ylim=ylm,
           xlab='Sites', ylab='random', las=1)
      segments(seq_len(max(sites)), y0=richness - 2*sd,
               y1=richness + 2*sd)
    })    
  } else {
    with(l[[i]], { # for subsequent elements, use lines()
      lines(sites, richness, col=i)
      segments(seq_len(max(sites)), y0=richness - 2*sd,
               y1=richness + 2*sd, col=i)
    })    
  }
})

legend('bottomright', c('Site 1', 'Site 2'), col=1:2, lty=1,
       bty='n', inset=0.025)

enter image description here