我正在使用 R 中附加的图像函数。为了速度,我更喜欢使用它而不是热图,因为我将它用于巨大的矩阵(~ 400000 x 400)。
我的函数中的问题是调色板的动态范围,在我的例子中它只有蓝色和黄色。我尝试了对 colorramp 线的多次更改,但没有一个给我带来所需的输出。
我尝试的最后一个颜色渐变选项是在 R 中使用一个很好的包,称为颜色渐变 https://i.stack.imgur.com/K2D4k.png,给出合理结果的是:
library("colorRamps")
ColorRamp = blue2green2red(400)
ColorLevels <- seq(min, max, length=length(ColorRamp))
然而,仍然不如 matlab 色带选项灵活。
I am not very familiar on how to make it look better and with more range, such as in the photo attached.
请告诉我是否可以更改我的图像功能以使我的图像看起来像照片中的图像。
我用于绘制图像的 R 函数,其中 raster = TRUE 表示速度,如下所示:
# ----- 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 ----- #