我想为维恩图创造一个传奇。这应该是直接的,因为venneuler函数会将使用的颜色返回给控制台。颜色的值介于0和1之间。我想知道如何将存储在$colors中的那些数值转换为可以用来填充图例中的fill参数的值。
下面我尝试使用从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])发布于 2012-02-03 08:28:46
通过仔细阅读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")

https://stackoverflow.com/questions/9121956
复制相似问题