2012-07-24 92 views
3

我試圖想出一個一致的方式來表示繪製它們時我的數據集中的每個因素。因此,例如,我可以用的藍色每次深淺不同,我作圖涉及詞性東西代表「詞性」的水平:ggplot2:繪製離散因子與一組相似的顏色

eg.dat <- data.frame(rt=c(530, 540, 555), 
        part.of.speech=c("Verb", "Noun", "Both") 
        ) 

ggplot(eg.dat, aes(part.of.speech, rt, fill=part.of.speech)) + 
    geom_bar(stat="identity", colour="black") + 
    scale_fill_manual(values=c("cyan", "blue", "darkblue")) 

Specifying colours manually

未來與花哨然而,每個因素的顏色名稱都很困難,所以我一直在尋找更多的自動化解決方案。一個相當hackish的解決方法是使用alpha

ggplot(eg.dat, aes(part.of.speech, rt, alpha=part.of.speech)) + 
    geom_bar(stat="identity", colour="black", fill="blue") + 
    scale_alpha_discrete(range=c(0.4, 1)) 

Using alpha

但我一直在想,是否有選擇的相似的顏色像這樣的短程任何簡單的方法。在ggplot2scale_colour_gradient類型功能不會離散因素,如這些工作,它似乎並不特別容易得到自定義顏色出來rainbowheat.colors。理想的功能應該是這樣的: shades(n, central_colour="blue"),返回n顏色值。任何建議的最佳方式來實現這一目標?

+1

'訪問?scale_fill_brewer'使用'R'包'RColorBrewer'它實現上顯示的想法網站[http://colorbrewer2.org/](http://colorbrewer2.org/)。 – mnel 2012-07-24 06:14:50

+0

更好''scale_fill_hue' – mnel 2012-07-24 06:16:40

+0

作爲一個便箋(我知道,我是'那個傢伙'),通常不建議像這樣使用顏色(詞性已被不同列分隔)分心的價值觀。看看這個pdf格式的第2頁和第3頁的條形圖:http://www.perceptualedge.com/articles/visual_business_intelligence/rules_for_using_color.pdf – 2012-07-24 06:42:08

回答

8

scale_fill_discretescale_fill_hue會做到這一點。您可以定義h,cl(色調,色度和亮度)。參見?hcl以獲得更多細節

例如。

ggplot(eg.dat, aes(part.of.speech, rt, fill=part.of.speech)) + 
    geom_bar(stat="identity", colour="black") + 
    scale_fill_discrete(h =c(220,260)) 

enter image description here

scale_fill_hue將給(在這種情況下)相同的結果。

您也可以使用scale_fill_brewer它使用RColorBrewer包,給人以colorbrewer調色板

ggplot(eg.dat, aes(part.of.speech, rt, fill=part.of.speech)) + 
    geom_bar(stat="identity", colour="black") + 
    scale_fill_brewer(palette = 'Blues') 

enter image description here

+0

這裏我們可能已經進入了主觀領域,但是對於我來說,你離開'scale_fill_hue'的色調看起來不如'scale_fill_alpha'好 - 它們太相似了,或者它們變得不太明顯是相同顏色的陰影。 'alpha'的副作用之一可能是色度和亮度在變化,可惜你不能用'scale_fill_hue'來做同樣的事情。 – Marius 2012-07-24 06:30:58

+0

你將不得不與h,c和l一起玩,以獲得'好'的範圍。「啤酒」方法更簡單,因爲調色板是預定義的,並且是科學研究的結果,可以獲得最佳顏色。 – mnel 2012-07-24 06:33:25

+2

@Marius,Color Brewer'sequential'調色板經過優化,可以順序分離。您可以通過更改調色板號輕鬆實驗所有19種可能性:'scale_fill_brewer(type =「seq」,palette = 1)' – bdemarest 2012-07-24 06:35:58