【问题标题】:How do I colour lines separately in rarecurve (vegan package)如何在稀有曲线(素食包)中单独着色线条
【发布时间】:2014-03-28 13:52:45
【问题描述】:

我正在使用rarecurve (vegan) 为九个样本生成稀疏曲线,但我希望它们以三个为一组进行着色。

rarecurve 的参数为:

rarecurve(x, step = 1, sample, xlab = "Sample Size", ylab = "Species", label = TRUE, ...)

使用... 将参数传递给“情节”。但是,当我用col=c(rep("blue",3), rep("red",3), rep("darkgreen",3)) 替换省略号时,所有行都显示为蓝色。如何单独为线条着色?

计算每张图需要将近三个小时,所以试错测试有点费力!

【问题讨论】:

  • 仅供参考:我通过与@rawr 的答案中显示的类似更改将其作为revision 2867 中的vegan 的功能添加。

标签: r vegan


【解决方案1】:
## 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 单独制作,而lines 仅采用它在... 传递的颜色参数中看到的第一种颜色,在您的情况下为蓝色。在对这个循环应用一个简单的 hack 之后:

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

并在rarecurve 函数中指定一个新参数cols,而不是将col 传递给plotlines

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

【讨论】:

  • 谢谢。如果其他纯素开发者对此感到满意,我可能会将此添加到纯素开发版本中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-02
  • 1970-01-01
  • 2012-08-27
  • 2017-07-02
  • 2018-11-20
相关资源
最近更新 更多