如何在热图中绘制不同且独立的颜色渐变图例.2 (R)
How to plot different and independent color gradient legend in heatmap.2 (R)
我通过计算得到一个矩阵,其中的值在 0 到 4 之间。我使用函数 heatmap.2
绘制。
想法是有一个颜色代码和不同的单元格值函数渐变:
从 0 到 0.99:红色渐变
从 1 到 1.99:黄色渐变
从 2 到 2.99:绿色渐变
从 3 到 4:灰色渐变
如您所见,渐变不是独立的,颜色从一个范围平滑地变化到另一个范围(例如,我从红色到黄色得到橙色)。如何获得4个不同且独立的梯度?意思是,从 0 到 0.99 只有红色渐变,从 1 到 1.99 只有黄色渐变等
这里是我使用的代码(我为示例创建了一个随机矩阵):
library(gplots)
# create a matrix of random values from [0, 4]
random.matrix <- matrix(runif(100, min = 0, max = 4), nrow = 10)
quantile.range <- quantile(random.matrix, probs = seq(0, 1))
palette.breaks <- seq(quantile.range["0%"], quantile.range["100%"], 0.25)
color.palette <- colorRampPalette(c("red", "yellow", "green", "grey"))(length(palette.breaks)-1)
heatmap.2(
random.matrix,
dendrogram = "none",
scale = "none",
trace = "none",
symbreaks=FALSE,
symkey=FALSE,
key = TRUE,
key.title = 'class information',
key.xlab = 'class number',
keysize = 2,
#( "bottom.margin", "left.margin", "top.margin", "right.margin" )
key.par=list(mar=c(3,3.5,2,0)),
labRow = NA,
labCol = NA,
col = color.palette,
breaks = palette.breaks,
)
legend(x="topright", legend=c("class 1: city", "class 2: crops", "class 3: forest", "class 4: road"),fill=c("red", "yellow", "green", "grey", ""))
这里是剧情:
我是这样找到的:
# gplots contains the heatmap.2 function
library(gplots)
# create a matrix of random values from [0, 4]
random.matrix <- matrix(runif(100, min = 0, max = 4), nrow = 10)
quantile.range <- quantile(random.matrix, probs = seq(0, 1))
palette.breaks <- seq(quantile.range["0%"], quantile.range["100%"], 0.25)
colfunc1 <- colorRampPalette(c("white", "red"))
colfunc2 <- colorRampPalette(c("white", "yellow"))
colfunc3 <- colorRampPalette(c("white", "green"))
colfunc4 <- colorRampPalette(c("grey", "black"))
color.palette <- colorRampPalette(c(colfunc1(4), colfunc2(4), colfunc3(4), colfunc4(3)))(length(palette.breaks)-1)
heatmap.2(
random.matrix,
dendrogram = "none",
scale = "none",
trace = "none",
symbreaks=FALSE,
symkey=FALSE,
key = TRUE,
key.title = 'class information',
key.xlab = 'class number',
keysize = 2,
#( "bottom.margin", "left.margin", "top.margin", "right.margin" )
key.par=list(mar=c(3,3.5,2,0)),
labRow = NA,
labCol = NA,
col = color.palette,
breaks = palette.breaks,
)
legend(x="topright", legend=c("class 1: city", "class 2: crops", "class 3: forest", "class 4: road"),fill=c("red", "yellow", "green", "grey", ""))
我通过计算得到一个矩阵,其中的值在 0 到 4 之间。我使用函数 heatmap.2
绘制。
想法是有一个颜色代码和不同的单元格值函数渐变:
从 0 到 0.99:红色渐变
从 1 到 1.99:黄色渐变
从 2 到 2.99:绿色渐变
从 3 到 4:灰色渐变
如您所见,渐变不是独立的,颜色从一个范围平滑地变化到另一个范围(例如,我从红色到黄色得到橙色)。如何获得4个不同且独立的梯度?意思是,从 0 到 0.99 只有红色渐变,从 1 到 1.99 只有黄色渐变等
这里是我使用的代码(我为示例创建了一个随机矩阵):
library(gplots)
# create a matrix of random values from [0, 4]
random.matrix <- matrix(runif(100, min = 0, max = 4), nrow = 10)
quantile.range <- quantile(random.matrix, probs = seq(0, 1))
palette.breaks <- seq(quantile.range["0%"], quantile.range["100%"], 0.25)
color.palette <- colorRampPalette(c("red", "yellow", "green", "grey"))(length(palette.breaks)-1)
heatmap.2(
random.matrix,
dendrogram = "none",
scale = "none",
trace = "none",
symbreaks=FALSE,
symkey=FALSE,
key = TRUE,
key.title = 'class information',
key.xlab = 'class number',
keysize = 2,
#( "bottom.margin", "left.margin", "top.margin", "right.margin" )
key.par=list(mar=c(3,3.5,2,0)),
labRow = NA,
labCol = NA,
col = color.palette,
breaks = palette.breaks,
)
legend(x="topright", legend=c("class 1: city", "class 2: crops", "class 3: forest", "class 4: road"),fill=c("red", "yellow", "green", "grey", ""))
这里是剧情:
我是这样找到的:
# gplots contains the heatmap.2 function
library(gplots)
# create a matrix of random values from [0, 4]
random.matrix <- matrix(runif(100, min = 0, max = 4), nrow = 10)
quantile.range <- quantile(random.matrix, probs = seq(0, 1))
palette.breaks <- seq(quantile.range["0%"], quantile.range["100%"], 0.25)
colfunc1 <- colorRampPalette(c("white", "red"))
colfunc2 <- colorRampPalette(c("white", "yellow"))
colfunc3 <- colorRampPalette(c("white", "green"))
colfunc4 <- colorRampPalette(c("grey", "black"))
color.palette <- colorRampPalette(c(colfunc1(4), colfunc2(4), colfunc3(4), colfunc4(3)))(length(palette.breaks)-1)
heatmap.2(
random.matrix,
dendrogram = "none",
scale = "none",
trace = "none",
symbreaks=FALSE,
symkey=FALSE,
key = TRUE,
key.title = 'class information',
key.xlab = 'class number',
keysize = 2,
#( "bottom.margin", "left.margin", "top.margin", "right.margin" )
key.par=list(mar=c(3,3.5,2,0)),
labRow = NA,
labCol = NA,
col = color.palette,
breaks = palette.breaks,
)
legend(x="topright", legend=c("class 1: city", "class 2: crops", "class 3: forest", "class 4: road"),fill=c("red", "yellow", "green", "grey", ""))