传说中的venn图在venneuler

时间:2012-02-03 00:11:33

标签: r venn-diagram

我想创建一个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])

1 个答案:

答案 0 :(得分: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")

enter image description here