[英]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", ""))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.