在 lcgong 之后(不幸的是,我可以发表直接评论)纯换位导致颜色表示出现问题。因此,我再次旋转了矩阵并且它起作用了。您可以找到如下功能。请确保所选的热图色阶适用于 3 到 11 之间的 n。如果需要,您可以在此处简单地选择另一个。
heatmap <- function(data, rowN, colN, xTitle = "", yTitle = "", numColors)
{
# transpose and rotate matrix clockswise 90 degrees
dataAdjusted <- t(apply(data,2,rev))
image(1:ncol(data), 1:nrow(data), xlab = xTitle, ylab = yTitle, dataAdjusted, col = rev(brewer.pal(numColors,"RdYlBu")), axes = FALSE)
axis(1, 1:ncol(data), colN)
axis(2, 1:nrow(data), rowN)
for (x in 1:ncol(data))
for (y in 1:nrow(data))
# add text values into matrix based on transposed/rotated indices + round values to two digits
text(x, y, round(dataAdjusted[x,y],2))
}
# required lib
library(RColorBrewer)
# Make a 8x8 matrix
m = matrix(rnorm(64), nrow=8)
# row names
rowN <- c("row 01", "row 02", "row 03", "row 04", "row 05", "row 06", "row 07", "row 08");
# column names
colN <- c("col 01", "col 02", "col 03", "col 04", "col 05", "col 06", "col 07", "col 08");
# without axis titles
heatmap(m, rowN, colN, numColors = 10)
# alternatively with titles
heatmap(m, rowN, colN, xTitle = "xTest", yTitle = "yTest", numColors = 10)