gpt4 book ai didi

r - 如何在rarecurve(纯素包)中单独为线条着色

转载 作者:行者123 更新时间:2023-12-02 04:32:36 27 4
gpt4 key购买 nike

我正在使用 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)

enter image description here

# 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)

enter image description here

问题出在 vegan::rarecurve 中的这些行

for (ln in seq_len(length(out))) {
N <- attr(out[[ln]], "Subsample")
lines(N, out[[ln]], ...)

其中每行由 lines 单独制作,而 lines 又仅采用在 ... 传递的颜色参数中看到的第一种颜色,该颜色为蓝色你的情况。对此循环应用简单的修改后:

for (ln in seq_len(length(out))) {
N <- attr(out[[ln]], "Subsample")
lines(N, out[[ln]], col = cols[ln], ...)

并在 rarecurve 函数中指定一个新参数 cols,而不是将 col 传递到 plot 上,并且:

cols = c(rep('红色', nrow(x)/2),rep('蓝色', 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)
}

关于r - 如何在rarecurve(纯素包)中单独为线条着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22714775/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com