2012-02-03 41 views
6

我想創建一個venneuler維恩圖的圖例。這應該是直接的,因爲函數venneuler返回用於控制檯的顏色。顏色的值介於0和1之間。我想知道如何將存儲在$ colors中的那些數值變成我可以用來填充圖例中的填充參數的東西。venneuler中的傳奇維恩圖

我試圖通過使用從venneuler提取的$顏色和從顏色索引()的下面。我知道這是不正確的,因爲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]) 

回答

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

+0

奧布賴恩這是完美的。 – 2012-02-03 00:49:52

+0

我用 'id < - match(names(y $ colours,LAB))' 改爲 – 2014-01-13 21:27:50