2013-07-18 7 views
7

मान लीजिए मैं इस साजिश है:ggplot2 रंग स्केल के लिए निरंतर स्केल को अलग करने का सबसे आसान तरीका?

ggplot(iris) + geom_point(aes(x=Sepal.Width, y=Sepal.Length, colour=Sepal.Length)) + scale_colour_gradient()

क्या रंग पैमाने discretize करने का सही तरीका, साजिश स्वीकार किए जाते हैं जवाब यहाँ (gradient breaks in a ggplot stat_bin2d plot) नीचे दिखाया गया है की तरह है?

ggplot सही मूल्यों को सही ढंग से पहचानता है और इनके लिए अलग-अलग स्केल का उपयोग करता है, लेकिन मेरा प्रश्न यह है कि यदि आपके पास लगातार डेटा है और आप इसके लिए एक अलग रंग बार चाहते हैं (प्रत्येक वर्ग के साथ एक वर्ग के साथ, और ढाल में रंगीन वर्ग), यह करने के लिए सबसे अच्छा तरीका क्या है? क्या डीजीप्लॉट के बाहर विघटनकारी/कताई होनी चाहिए और डेटाफ्रेम में एक अलग अलग-मूल्य वाले कॉलम के रूप में डालना चाहिए, या ggplot के भीतर ऐसा करने का कोई तरीका है? मैं के लिए क्या देख रहा हूँ का एक उदाहरण पैमाने यहाँ दिखाया गया है के समान है: enter image description here

को छोड़कर मैं एक बिखराव साजिश और कुछ geom_tile/हीटमैप की तरह नहीं की साजिश रचने कर रहा हूँ।

धन्यवाद।

उत्तर

9

समाधान थोड़ा जटिल है, क्योंकि आप एक अलग पैमाने चाहते हैं। अन्यथा आप शायद round का उपयोग कर सकते हैं।

library(ggplot2) 

bincol <- function(x,low,medium,high) { 
    breaks <- function(x) pretty(range(x), n = nclass.Sturges(x), min.n = 1) 

    colfunc <- colorRampPalette(c(low, medium, high)) 

    binned <- cut(x,breaks(x)) 

    res <- colfunc(length(unique(binned)))[as.integer(binned)] 
    names(res) <- as.character(binned) 
    res 
} 

labels <- unique(names(bincol(iris$Sepal.Length,"blue","yellow","red"))) 
breaks <- unique(bincol(iris$Sepal.Length,"blue","yellow","red")) 
breaks <- breaks[order(labels,decreasing = TRUE)] 
labels <- labels[order(labels,decreasing = TRUE)] 


ggplot(iris) + 
    geom_point(aes(x=Sepal.Width, y=Sepal.Length, 
       colour=bincol(Sepal.Length,"blue","yellow","red")), size=4) + 
    scale_color_identity("Sepal.Length", labels=labels, 
         breaks=breaks, guide="legend") 

enter image description here

+0

लेबल के आदेश काम करता है, तो ऋणात्मक संख्याओं रंग के लिए इस्तेमाल किया चर में मौजूद हैं? – gvrocha

5

आप निम्नलिखित की कोशिश कर सकते हैं, मैं अपने उदाहरण कोड उचित रूप से नीचे संशोधित किया है:

#I am not so great at R, so I'll just make a data frame this way 
#I am convinced there are better ways. Oh well. 
df<-data.frame() 
for(x in 1:10){ 
    for(y in 1:10){ 
    newrow<-c(x,y,sample(1:1000,1)) 
    df<-rbind(df,newrow) 
    } 
} 
colnames(df)<-c('X','Y','Val') 


#This is the bit you want 
p<- ggplot(df, aes(x=X,y=Y,fill=cut(Val, c(0,100,200,300,400,500,Inf)))) 
p<- p + geom_tile() + scale_fill_brewer(type="seq",palette = "YlGn") 
p<- p + guides(fill=guide_legend(title="Legend!")) 

#Tight borders 
p<- p + scale_x_continuous(expand=c(0,0)) + scale_y_continuous(expand=c(0,0)) 
p 

नोट कटौती के सामरिक उपयोग डेटा रंग के उपयोग के बाद discretize को चीज़ें सुंदर बनाने के लिए शराब।

परिणाम निम्नानुसार दिखता है।

2D heatmap with discretized colour

+0

मुझे यह पसंद है, लेकिन मुझे आश्चर्य है कि प्रश्न में दिखाए गए उदाहरण में पैमाने को चिह्नित करने का कोई तरीका है या नहीं। अधिक सटीक: रंग "स्ट्रेट" के बीच कट बिंदुओं पर दिखाई देने के लिए आप चरम सीमा कैसे प्राप्त करते हैं? – gvrocha

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