2012-11-11 6 views
5

R에 첨부 된 이미지 함수를 사용하고 있습니다. 거대한 행렬 (~ 400000은 400까지)에 사용하기 때문에 속도에 대한 히트 맵에 반대하는 식으로 사용하는 것을 선호합니다.R 이미지 함수 in R

제 기능상의 문제는 색상 표의 동적 범위입니다. 제 경우에는 파란색과 노란색 만 있습니다. colorramp 라인에 몇 가지 변경을 시도했지만 원하는 결과를 얻지 못했습니다.

합리적인 결과를 내가 ColorRamps라는 R에 좋은 패키지를 사용했다려고 마지막 컬러 램프 옵션은 다음과 같습니다, 그러나

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

여전히 matlab에 컬러 램프 옵션으로 유연하지 않습니다.

첨부 사진과 같이 더보기 좋고 범위를 넓히는 방법에 익숙하지 않습니다. enter image description here

이미지 기능을 변경하여 내 이미지를 사진처럼 보이게 만들 수 있는지 알려주십시오. 다음

속도 TRUE I 래스터와 플로팅 이미지를 사용하는 R = 함수이다

# ----- 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) 
} 

모두의 예 (I 시안과 옐로우 사이의 단계의 수를 압착 한)이다 : 또한 color.palette의 색 사이의 단계의 수를 정의하는 함수를 적응 enter image description here

I wrote a function 또한 색상 표를 그려야하고 image과 같은 인수를 사용하는 경우. 플롯 레이아웃을 올바르게 설정하면 행렬 및 해당 색상 스케일을 빠르게 그릴 수 있습니다.

+0

이것은 매우 유용합니다. 감사! – Dnaiel

+0

@Dnaiel - 질문에 답변이되지 않으면 다른 사람들이 개선 할 수있는 이유를 설명하십시오. –

+0

확실히 답변입니다! ;-) 방금 받아 들였습니다. – Dnaiel