2013-06-04 21 views
1

我有一個在直方圖上疊加了幾個觀測值的圖。觀察結果按順序收集,我需要觀察它們收集的順序。使用scale_colour_brewer很簡單。問題是,順序brewer調色板的最大長度是9.我有多達20個觀察的例子,我不知道如何使用內插顏色。以下是一些用少於10分表示我所需輸出的代碼。插入一個連續的brewer調色板作爲ggplot2的圖例

# Setting this to be > 9 will cause a warning and not produce the desired result. 
observations = 9 
subset <-1:observations 
res = data.frame(x_data = rnorm(5000),TestID=1:5000) 
ggplot(res,aes(x=x_data)) + 
    stat_bin(aes(y=..density..))+ 
    stat_density(colour="blue", fill=NA)+ 
    geom_point(data = res[res$TestID %in% subset,], 
      aes(x = x_data, 
       y = 0, 
       colour = as.factor(res$TestID[res$TestID %in% subset]) 
      ), 
      size = 5) +scale_colour_brewer("Fancy title", type="seq", palette='Reds') 

我知道,隨着觀測數量變大,這個圖將變得難以閱讀。但是,我相信有多達20種顏色,應該可以在我的應用程序中解釋結果。

+0

您將需要使用'scale_colour_gradient2'或使用'scale_colour_manual'滾動自己的色階。啤酒調色板不會超過9類(正確的)。 – joran

回答

4

擴大對我的評論,你需要使用colorRampPalette

library(RColorBrewer) 
blues_fun <- colorRampPalette(brewer.pal(9,"Blues")) 
> blues_fun(20) 
[1] "#F7FBFF" "#ECF4FB" "#E1EDF8" "#D7E6F4" "#CDE0F1" "#C1D9ED" "#B0D2E7" "#A0CAE1" "#8BBFDC" "#75B3D8" "#62A8D2" "#519CCB" 
[13] "#4090C5" "#3282BD" "#2474B6" "#1966AD" "#0E59A2" "#084B94" "#083D7F" "#08306B" 

,然後通過scale_colour_manual建設規模:

ggplot(res,aes(x=x_data)) + 
    stat_bin(aes(y=..density..))+ 
    stat_density(colour="blue", fill=NA)+ 
    geom_point(data = res[res$TestID %in% subset,], 
      aes(x = x_data, 
       y = 0, 
       colour = as.factor(res$TestID[res$TestID %in% subset]) 
      ), 
      size = 5) + 
    scale_colour_manual("Fancy title",values = blues_fun(9)) 

您只需將手離開產生的顏色的values的說法。

+0

謝謝,但您可以詳細說明如何使用scale_colour_manual?我儘可能使用colorRampPalette,但使用scale_colour_manual時不清楚。 – user2111827

+0

@ user2111827看我的編輯。 – joran

+0

我剛剛就此添加了博文:[如何使用ggplot和RColorBrewer擴展調色板](http://novyden.blogspot.com/2013/09/how-to-expand-color-palette-with-ggplot。 HTML) – topchef

相關問題