r - 控制 r 图中的颜色

标签 r plot vegan

nestednodf(vegan 包的一个函数)的输出可以很容易地绘制出来。我想用不同的颜色突出显示选定的行,但我不知道如何在单个图中指定它。假设我希望第 1,3 行和第 5 行为蓝色,第 2 行和第 4 行为红色(默认颜色)。此代码允许将第二个图与蓝色的第 1、3、5 行重叠,但不会在第一个图中插入选定的行:

library(vegan)
df=data.frame(a=c(0,1,1,1,0), b=c(1,0,0,0,1), c=c(1,1,1,1,0), d=c(1,0,1,0,1), e=c(0,0,0,1,1))
plot(nestednodf(df))
plot(nestednodf(df[c(1,3,5),]), col='blue', add=T)

有什么办法可以控制行颜色吗?像这样的事情:

plot(nestednodf(df), row.col=c('blue', '', 'blue', '', 'blue'))

最佳答案

您可以通过输入vegan:::plot.nestednodf来查看函数的源代码。确实没有机会两次调整行颜色。但是您可以看到该功能非常简单,因此您可以编写自己的版本

myplot <- function (x, col = "red", names = FALSE, ...) 
{
    z <- x$comm
    z <- t(z[nrow(z):1, ])

    if (length(col) == 1) 
        col <- c(NA, col)
    else if ( length(col)>1) {
        z <- z*((col(z)-1)%%2+1)
    }
    image(z, axes = FALSE, col = col, ...)
    box()
    if (length(names) == 1) 
        names <- rep(names, 2)
    if (names[1]) {
        axis(2, at = seq(1, 0, len = ncol(z)), labels = rev(colnames(z)), 
            las = 2, ...)
    }
    if (names[2]) {
        axis(3, at = seq(0, 1, len = nrow(z)), labels = rownames(z), 
            las = 2, ...)
    }
}

这里我刚刚添加了一行来更改颜色以在指定的值之间交替。比较

plot(nestednodf(df))
myplot(nestednodf(df), col=c(NA,'red','blue'))

enter image description here

请注意,我传递了三种颜色,因为第一种颜色用于矩阵中的“0”值

关于r - 控制 r 图中的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37711183/

相关文章:

R metaMDS 排序距离

r - 将列添加到 R 列表中的多个数据框

R 中的随机森林 : Is there a possibility of calculating casewise confidence intervals?

r - 使用fread读取带有双引号和不正确转义字符的数据

python - 使用 Python,文件

3d - 等轴 : how to keep z axis on screen? 的 gnuplot 图

r - 如果 "OR"有多个条件

r - 如何仅在没有 NA 的情况下检查一组变量中是否存在某个值?

plot - 如何在 gnuplot 中为 xticlabels 提供自定义字符串参数?

output - 如果 NMDS(纯素)收敛不可能,这是否会使输出变得无用?