我想做的是采用这个矩阵:
> partb
0.5 1.5 1a 1b -2 -3
A1FCLYRBAB430F 0.26 0.00 0.74 0.00 0.00 0.00
A1SO604B523Q68 0.67 0.33 0.00 0.00 0.00 0.00
A386SQL39RBV7G 0.00 0.33 0.33 0.33 0.00 0.00
A3GTXOXRSE74WD 0.41 0.00 0.08 0.03 0.05 0.44
A3OOD9IMOHPPFQ 0.00 0.00 0.33 0.00 0.33 0.33
A8AZ39QM2A9SO 0.13 0.54 0.18 0.13 0.00 0.03
然后制作一个热图,其中包含现在有色单元格中的每个值。
制作热图很容易:
> heatmap( partb, Rowv=NA, Colv=NA, col = heat.colors(256), margins=c(5,10))
但是对于我的生活,我无法弄清楚如何将价值放在每个细胞中。
我错过了什么?当然这是常见的事情。
试试heatmap.2
套餐中的gplots
。 cellnote和notecol参数控制放置在单元格中的文本。你可能也想要dendrogram = "none"
。
例如:
m <- matrix(1:30, ncol=6)
colnames(m) <- paste("C", 1:6, sep="")
rownames(m) <- paste("R", 1:5, sep="")
m
image(1:ncol(m), 1:nrow(m), t(m), col = terrain.colors(60), axes = FALSE)
axis(1, 1:ncol(m), colnames(m))
axis(2, 1:nrow(m), rownames(m))
for (x in 1:ncol(m))
for (y in 1:nrow(m))
text(x, y, m[y,x])
你可以使用image
和text
。我个人喜欢来自image.plot
包的fields
,因为它在侧面增加了一个传奇,但你也可以和image
一起使用它。
所以举个例子
require(fields)
# Make a 10x10 matrix
m = matrix(rnorm(100), nrow=10)
image.plot(m)
for (x in 1:10)
for (y in 1:10)
text((x-1)/9, (y-1)/9, sprintf("%0.2f", m[x,y]))
来自levelplot()
包装的lattice
将为您提供一个彩色传奇。不完全是你想要的,但要考虑的事情。
继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)