6

我想为venneuler venn 图创建一个图例。这应该是直截了当的,因为函数 venneuler 将使用的颜色返回给控制台。颜色的值介于 0 和 1 之间。我想知道如何将存储在 $colors 中的这些数字值转换为可用于填充图例中的填充参数的东西。

我通过使用从venneuler 中提取的$colors 并从colors() 中进行索引来尝试此操作。我知道这是不正确的,因为 colors() 是用间隔值索引的,但把它放进去显示我想要的。

set.seed(20)
x <- matrix(sample(0:1, 100, replace = TRUE), 10, 10)
colnames(x) <- LETTERS[1:10]
rownames(x) <- letters[1:10]

require(venneuler)
y <- venneuler(x)
plot(y)

y$colors

legend(.05, .9, legend = colnames(x), fill = colors()[y$colors])
4

1 回答 1

8

通过仔细阅读plot.VennDiagram及其默认值,您可以看到它如何将数字转换y$colors为 rgb 颜色字符串。(试着getAnywhere("plot.VennDiagram")自己看看。)

在这里,我将处理颜色(在您的情况下)的两位代码收集到一个将为您进行转换的函数中。传说的定位可能会有所改善,但这是另一个问题......

col.fn <- function(col, alpha=0.3) {
    col<- hcl(col * 360, 130, 60)
    col <- col2rgb(col)/255
    col <- rgb(col[1, ], col[2, ], col[3, ], alpha)
    col
}

COL <- col.fn(y$colors)
# The original order of columns in x is jumbled in the object returned
# by venneuler. This code is needed to put the colors and labels back
# in the original order (here alphabetical).
LABS <- y$labels
id <-  match(colnames(x), LABS)

plot(y)
legend(.05, .9, legend = LABS[id], fill = COL[id], x="topleft")

在此处输入图像描述

于 2012-02-03T00:28:46.043 回答