2013-04-30 13 views
6

मैं आनुपातिक स्टैक्ड बार प्लॉट प्लॉट करने के लिए ggplot का उपयोग कर रहा हूं। और प्लॉट मैं हो रही है कुछ इस तरह है: enter image description hereआर: 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)) 

यहाँ val4 और val5 बहुत अलग नहीं हैं।

लेकिन रंगों के कारण उनमें से कुछ अलग-अलग नहीं हैं। क्या कोई मुझे बता सकता है कि बेहतर रंग कैसे चुनें ताकि वे अलग-अलग हों?

धन्यवाद।

+0

क्या आप प्लॉटिंग का परीक्षण करना आसान बनाने के लिए एक उदाहरण डेटाफ्रेम प्रदान कर सकते हैं? वैसे भी, मेरा विचार ggplot से scale_colour_brewer में एक नज़र रखना है, जो आपको http://colorbrewer2.org – zelite

+0

@zelite जोड़ा गया पैलेट का उपयोग करने की अनुमति देता है। यहां मूल रूप से 'val4' और' val5' स्पष्ट रूप से बहुत अलग नहीं हैं। –

उत्तर

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 विविधता मिलती है, जबकि मेरे मामले में मुझे कम-से-कम 1 9 रंगों की आवश्यकता होती है। सभी पैलेट के साथ, यह समस्या हो रही है। –

+0

@RachitAgrawal ठीक है, मैंने कुछ ऐसा अपडेट किया जो आपके लिए काम कर सकता है। संपादन की जांच करें। –

+0

ऊपर वर्णित एक ही समस्या से पीड़ितों। उनमें से कुछ को प्रतिष्ठित नहीं किया जा सकता है। –

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

मैं रंगों को मिश्रण करने के लिए एक सरल समाधान के साथ आया हूँ। नीचे जोड़ा गया। सहायता के लिए धन्यवाद। –

0

मैं प्रयोग करेंगे scale_fill_manual = c ("लाल", "हरी"), आप और अधिक रंग अगर आप या scale_fill_brewer चाहते डाल सकते हैं (पैलेट = "रेड्स"), मैं इस एक पसंद है। आप यहां पैलेट का उपयोग कर सकते हैं

संबंधित मुद्दे