2013-04-30 34 views
6

我正在使用ggplot來繪製比例堆積條形圖。而我得到的情節是這樣的: enter image description hereR:ggplot更好的漸變顏色

這是我使用的自寫功能:

df <- data.frame(id=letters[1:3],val0=1:3,val1=4:6,val2=7:9, val3=2:4, val4=1:3, val5=4:6, val6=10:12, val7=12:14) 

PropBarPlot<-function(df, mytitle=""){ 
    melteddf<-melt(df, id=names(df)[1], na.rm=T) 
    ggplot(melteddf, aes_string(x=names(df)[1], y="value", fill="variable")) + 
    geom_bar(position="fill") + 
    theme(axis.text.x = element_text(angle=90, vjust=1)) + 
    labs(title=mytitle) 
} 

print(PropBarPlot(df)) 

這裏val4val5都不是很不同的。

但由於顏色有些不能區分。有人能告訴我如何選擇更好的顏色,以便它們可以區分嗎?

謝謝。

+0

您可以提供一個示例數據框,以便更容易地測試繪圖嗎?無論如何,我的想法是從ggplot中查看scale_colour_brewer,它允許您使用來自http://colorbrewer2.org的調色板 – zelite 2013-04-30 08:37:28

+0

@zelite已添加。這裏基本上'val4'和'val5'沒有明顯的不同。 – 2013-04-30 08:45:09

回答

1

謝謝@zelite和@ SimonO101的幫助。這是你提出的兩個更簡單的版本。爲了完整性在這裏添加。

library(ggplot2) 
library(reshape2) 
library(RColorBrewer) 

getColors<-function(n){ 
    mypal<-colorRampPalette(brewer.pal(12, "Paired")) 
    sample(mypal(n), n, replace=FALSE) 
} 

PropBarPlot<-function(df, mytitle=""){ 
    melteddf<-melt(df, id=names(df)[1], na.rm=T) 
    n<-length(levels(factor(melteddf$variable))) 

    ggplot(melteddf, aes_string(x=names(df)[1], y="value", fill="variable")) + 
     geom_bar(position="fill") + 
     scale_fill_manual(values=getColors(n)) + 
     theme(axis.text.x = element_text(angle=90, vjust=1)) + 
     labs(title=mytitle) 
} 

df <- data.frame(id=letters[1:3], 
      val0=1:3, 
      val1=4:6, 
      val2=7:9, 
      val3=2:4, 
      val4=1:3, 
      val5=4:6, 
      val6=10:12, 
      val7=12:14) 

print(PropBarPlot(df)) 

謝謝。

14

如何使用scale_fill_brewer使用ColorBrewer網站的調色板,由包RColorBrewer執行?

ggplot(diamonds, aes(clarity, fill=cut)) + 
geom_bar() + 
scale_fill_brewer(type = "div" , palette = "RdBu") 

enter image description here

有許多不同的發散調色板您可以從中選擇。

require(RColorBrewer) 
?brewer.pal 

如果你需要更多的顏色,你可以使用colorRampPalette功能的一些顏色之間插值(我會用一個brewer.pal調色板這一點)。你可以這樣做,像這樣:

# Create a function to interpolate between some colours 
mypal <- colorRampPalette(brewer.pal(6 , "RdBu")) 
# Run function asking for 19 colours 
mypal(19) 
[1] "#B2182B" "#C2373A" "#D35749" "#E47658" "#F0936D" "#F4A989" "#F8BFA5" 
[8] "#FCD6C1" "#F3DDD0" "#E7E0DB" "#DAE2E6" "#CBE1EE" "#ADD1E5" "#90C0DB" 
[15] "#72AFD2" "#5B9DC9" "#478BBF" "#3478B5" "#2166AC" 

在你的榜樣,需要8種顏色你scale_fill_manual()一個用這樣的:

PropBarPlot<-function(df, mytitle=""){ 
    melteddf<-melt(df, id=names(df)[1], na.rm=T) 
    ggplot(melteddf, aes_string(x=names(df)[1], y="value", fill="variable")) + 
    geom_bar(position="fill") + 
    theme(axis.text.x = element_text(angle=90, vjust=1)) + 
    labs(title=mytitle)+ 
    scale_fill_manual(values = mypal(8)) 
} 

print(PropBarPlot(df)) 
+0

我檢查了不同的調色板,我得到的最好是最大12變化,而在我的情況下,我需要至少19種顏色。所有的調色板都有問題。 – 2013-04-30 08:54:32

+0

@RachitAgrawal好的,我更新了一些可能適合你的東西。檢查編輯。 – 2013-04-30 09:04:24

+0

患有上述相同的問題。其中一些無法分辨。 – 2013-04-30 09:08:57

1

借用一些代碼@ SimonO101

library(ggplot2) 
library(reshape2) 
library(RColorBrewer) 
mypal <- colorRampPalette(brewer.pal(9 , "Set1")) #you can try using different palete instead 
#of "Set1" until it looks good to you 

intercalate <- function(n){ #my crude attempt to shuffle the colors 
    c(rbind(1:(n/2), n:(n/2+1))) #it will only work for even numbers 
} 

PropBarPlot<-function(df, mytitle=""){ 
    melteddf<-melt(df, id=names(df)[1], na.rm=T) 
    ggplot(melteddf, aes_string(x=names(df)[1], y="value", fill="variable")) + 
    geom_bar(position="fill") + 
    theme(axis.text.x = element_text(angle=90, vjust=1)) + 
    labs(title=mytitle)+ 
    scale_fill_manual(values = mypal(8)[intercalate(8)]) 
    #better would be to calculate the different number of categories 
    #you have and put that instead of the number 8 
} 

df <- data.frame(id=letters[1:3], 
       val0=1:3, 
       val1=4:6, 
       val2=7:9, 
       val3=2:4, 
       val4=1:3, 
       val5=4:6, 
       val6=10:12, 
       val7=12:14) 

print(PropBarPlot(df)) 

看看這是否能更好地滿足您的需求。

+0

我想出了一個更簡單的解決方案來混合顏色。下面添加。謝謝您的幫助。 – 2013-05-02 07:40:54

0

我會使用scale_fill_manual = C(「紅」,「綠」),就可以把更多的顏色,如果你想 或 scale_fill_brewer(調色板=「紅人」),我喜歡這一個。您可以在這裏使用調色板