我正在使用rarecurve
(vegan
)生成九个样本的稀疏曲线,但我希望它们以三个为一组进行着色。
的参数为rarecurve
:
rarecurve(x, step = 1, sample, xlab = "Sample Size", ylab = "Species", label = TRUE, ...)
带有...
传递给'plot'的参数。但是,当我将省略号替换为时col=c(rep("blue",3), rep("red",3), rep("darkgreen",3))
,所有行都显示为蓝色。如何分别为线条上色?
计算每个图形需要将近三个小时,因此反复测试有点麻烦!
## example from ?vegan::rarecurve
library(vegan)
data(BCI)
S <- specnumber(BCI)
(raremax <- min(rowSums(BCI)))
Srare <- rarefy(BCI, raremax)
plot(S, Srare, xlab = "Observed No. of Species", ylab = "Rarefied No. of Species")
abline(0, 1)
rarecurve(BCI, step = 20, sample = raremax, col = "blue", cex = 0.6)
# using new function
plot(S, Srare, xlab = "Observed No. of Species", ylab = "Rarefied No. of Species")
abline(0, 1)
rarec(BCI, step = 20, sample = raremax, cex = 0.6)
问题是在这些行中 vegan::rarecurve
for (ln in seq_len(length(out))) {
N <- attr(out[[ln]], "Subsample")
lines(N, out[[ln]], ...)
其中的每一行都是单独制作的,lines
而该行又仅采用在传递的color参数中看到的第一种颜色...
,在您的情况下为蓝色。将简单的技巧应用到此循环后:
for (ln in seq_len(length(out))) {
N <- attr(out[[ln]], "Subsample")
lines(N, out[[ln]], col = cols[ln], ...)
并cols
在rarecurve
函数中指定一个新的参数,而不是传递col
给plot
和lines
:
cols = c(rep('red', nrow(x) / 2), rep('blue', nrow(x) / 2))
这是新功能
rarec <- function (x, step = 1, sample, xlab = "Sample Size", ylab = "Species",
label = TRUE, cols = c(rep('red', nrow(x) / 2), rep('blue', nrow(x) / 2)), ...) {
tot <- rowSums(x)
S <- specnumber(x)
nr <- nrow(x)
out <- lapply(seq_len(nr), function(i) {
n <- seq(1, tot[i], by = step)
if (n[length(n)] != tot[i])
n <- c(n, tot[i])
drop(rarefy(x[i, ], n))
})
Nmax <- sapply(out, function(x) max(attr(x, "Subsample")))
Smax <- sapply(out, max)
plot(c(1, max(Nmax)), c(1, max(Smax)), xlab = xlab, ylab = ylab,
type = "n", ...)
if (!missing(sample)) {
abline(v = sample)
rare <- sapply(out, function(z) approx(x = attr(z, "Subsample"),
y = z, xout = sample, rule = 1)$y)
abline(h = rare, lwd = 0.5)
}
for (ln in seq_len(length(out))) {
N <- attr(out[[ln]], "Subsample")
lines(N, out[[ln]], col = cols[ln], ...)
}
if (label) {
ordilabel(cbind(tot, S), labels = rownames(x), ...)
}
invisible(out)
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句