好的,所以@jlhoward 的解决方案当然要简单得多,也更明智。但是,由于我没有想到显而易见的,并将其编码,我想我不妨分享一下。对于手头的功能不接受的相关问题,它可能很有用add
。
加载库和一些示例数据:
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')
绘图
# 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)