आर

2012-11-11 24 views
5

में आर छवि फ़ंक्शन मैं आर में संलग्न छवि फ़ंक्शन का उपयोग कर रहा हूं। मैं इसे गति के लिए हीटमैप का विरोध करने के रूप में उपयोग करना पसंद करता हूं, क्योंकि मैं इसे विशाल मैट्रिस (~ 400000 से 400) के लिए उपयोग करता हूं।आर

मेरे फ़ंक्शन में समस्या कलर पैलेट के लिए गतिशील रेंज है, जो मेरे मामले में केवल नीली और पीला है। मैंने कलर्रैम्प लाइन में कई बदलावों की कोशिश की है लेकिन किसी ने मुझे वांछित आउटपुट नहीं दिया है।

पिछले रंग रैंप विकल्प है, जो उचित परिणाम दे रहा ColorRamps बुलाया आर में एक अच्छा पैकेज का उपयोग किया गया था की कोशिश की है:

library("colorRamps") 
ColorRamp = blue2green2red(400) 
ColorLevels <- seq(min, max, length=length(ColorRamp)) 

हालांकि, अभी भी matlab रंग रैंप विकल्प के रूप में के रूप में लचीला नहीं है।

मैं इस बात से परिचित नहीं हूं कि इसे बेहतर तरीके से कैसे दिखाना है और अधिक रेंज के साथ, जैसे फोटो संलग्न है। enter image description here

कृपया मुझे सलाह दें कि तस्वीर में एक जैसी मेरी छवि को देखने के लिए मेरे छवि फ़ंक्शन को बदलना संभव होगा।

आर समारोह मैं साजिश रचने छवियों के लिए उपयोग करते हैं, रेखापुंज साथ = गति के लिए सही प्रकार है:

# ----- Define a function for plotting a matrix ----- # 
myImagePlot <- function(x, filename, ...){ 
    dev = "pdf" 
    #filename = '/home/unix/dfernand/test.pdf' 
    if(dev == "pdf") { pdf(filename, version = "1.4") } else{} 
    min <- min(x) 
    max <- max(x) 
    yLabels <- rownames(x) 
    xLabels <- colnames(x) 
    title <-c() 
    # check for additional function arguments 
    if(length(list(...))){ 
    Lst <- list(...) 
    if(!is.null(Lst$zlim)){ 
     min <- Lst$zlim[1] 
     max <- Lst$zlim[2] 
    } 
    if(!is.null(Lst$yLabels)){ 
     yLabels <- c(Lst$yLabels) 
    } 
    if(!is.null(Lst$xLabels)){ 
     xLabels <- c(Lst$xLabels) 
    } 
    if(!is.null(Lst$title)){ 
     title <- Lst$title 
    } 
    } 
# check for null values 
if(is.null(xLabels)){ 
    xLabels <- c(1:ncol(x)) 
} 
if(is.null(yLabels)){ 
    yLabels <- c(1:nrow(x)) 
} 

layout(matrix(data=c(1,2), nrow=1, ncol=2), widths=c(4,1), heights=c(1,1)) 

# Red and green range from 0 to 1 while Blue ranges from 1 to 0 
ColorRamp <- rgb(seq(0,1,length=256), # Red 
        seq(0,1,length=256), # Green 
        seq(1,0,length=256)) # Blue 
ColorLevels <- seq(min, max, length=length(ColorRamp)) 

# Reverse Y axis 
reverse <- nrow(x) : 1 
yLabels <- yLabels[reverse] 
x <- x[reverse,] 

# Data Map 
par(mar = c(3,5,2.5,2)) 
image(1:length(xLabels), 1:length(yLabels), t(x), col=ColorRamp, xlab="", 
ylab="", axes=FALSE, zlim=c(min,max), useRaster=TRUE) 
if(!is.null(title)){ 
    title(main=title) 
} 
# Here we define the axis, left of the plot, clustering trees.... 
#axis(BELOW<-1, at=1:length(xLabels), labels=xLabels, cex.axis=0.7) 
# axis(LEFT <-2, at=1:length(yLabels), labels=yLabels, las= HORIZONTAL<-1, 
# cex.axis=0.7) 

# Color Scale (right side of the image plot) 
par(mar = c(3,2.5,2.5,2)) 
image(1, ColorLevels, 
     matrix(data=ColorLevels, ncol=length(ColorLevels),nrow=1), 
     col=ColorRamp, 
     xlab="",ylab="", 
     xaxt="n", useRaster=TRUE) 

layout(1) 
    if(dev == "pdf") { 
    dev.off() } 
} 
# ----- END plot function ----- # 
+0

कुशलतापूर्वक साजिश के लिए इन्हें दोहराएं, यह छवि – mdsumner

उत्तर

7

आप colorRampPalette में एक पूर्वाग्रह परिभाषित कर सकते हैं।

Z <- t(as.matrix(1:100)) 

pal.1 <- colorRampPalette(c("blue", "cyan", "yellow", "red"), bias=1) 
pal.2 <- colorRampPalette(c("blue", "cyan", "yellow", "red"), bias=3) 
pal.3 <- color.palette(c("blue", "cyan", "yellow", "red"), n.steps.between=c(10,1,10)) 

    x11() 
par(mfcol=c(1,3)) 
image(Z, col=pal.1(100)) 
image(Z, col=pal.2(100)) 
image(Z, col=pal.3(100)) 

:

#This is a wrapper function for colorRampPalette. It allows for the 
#definition of the number of intermediate colors between the main colors. 
#Using this option one can stretch out colors that should predominate 
#the palette spectrum. Additional arguments of colorRampPalette can also 
#be added regarding the type and bias of the subsequent interpolation. 
color.palette <- function(steps, n.steps.between=NULL, ...){ 

if(is.null(n.steps.between)) n.steps.between <- rep(0, (length(steps)-1)) 
if(length(n.steps.between) != length(steps)-1) stop("Must have one less n.steps.between value than steps") 

fill.steps <- cumsum(rep(1, length(steps))+c(0,n.steps.between)) 
RGB <- matrix(NA, nrow=3, ncol=fill.steps[length(fill.steps)]) 
RGB[,fill.steps] <- col2rgb(steps) 

for(i in which(n.steps.between>0)){ 
    col.start=RGB[,fill.steps[i]] 
    col.end=RGB[,fill.steps[i+1]] 
    for(j in seq(3)){ 
    vals <- seq(col.start[j], col.end[j], length.out=n.steps.between[i]+2)[2:(2+n.steps.between[i]-1)] 
    RGB[j,(fill.steps[i]+1):(fill.steps[i+1]-1)] <- vals 
    } 
} 

    new.steps <- rgb(RGB[1,], RGB[2,], RGB[3,], maxColorValue = 255) 
pal <- colorRampPalette(new.steps, ...) 
return(pal) 
} 

यहाँ (मैं सियान और पीले रंग के बीच चरणों की संख्या निचोड़ा है) दोनों का एक उदाहरण है: मैं भी color.palette में रंगों के बीच चरणों की संख्या को परिभाषित करने के समारोह ढाल लिया है enter image description here

इसके अलावा, आपके रुचि रखते हैं, I wrote a function है कि एक रंग पैमाने भूखंडों और image रूप में एक ही तर्क का उपयोग करता है। यदि आप अपना प्लॉट लेआउट सही तरीके से सेट अप करते हैं, तो यह आपके मैट्रिस और संबंधित रंग स्केल को साजिश करने का एक तेज़ तरीका भी होगा।

+0

के लिए बहुत अधिक जानकारी है यह मीठा है। धन्यवाद! – Dnaiel

+0

@Dnaiel - यदि यह आपके प्रश्न का उत्तर नहीं देता है, तो कृपया बताएं कि अन्य लोग इसमें क्यों सुधार कर सकते हैं। –

+0

यह निश्चित रूप से इसका उत्तर देता है! ;-) बस इसे स्वीकार कर लिया। – Dnaiel