将矩阵(包括值)显示为热图

发布于 2024-09-24 12:10:12 字数 593 浏览 5 评论 0原文

我想做的是采用这个矩阵:

> 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))

但是对于我来说,我无法弄清楚如何将值放入每个单元格中。

我缺少什么?当然,这是很常见的事情。

What I'd like to do is take this matrix:

> 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

And then make a heatmap that has each of the values in the now colored cells.

Making a heatmap is easy:

> heatmap( partb, Rowv=NA, Colv=NA, col = heat.colors(256),  margins=c(5,10))

But for the life of me I can't figure out how to put the value in each of the cells.

What am I missing? Surely this is a common thing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

永不分离 2024-10-01 12:10:12

例如:

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])

For example:

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])
鼻尖触碰 2024-10-01 12:10:12

尝试 gplots 中的 heatmap.2 包。 cellnote 和 notecol 参数控制放置在单元格中的文本。您可能还需要 dendrogram = "none"

Try heatmap.2 from the gplots package. The cellnote and notecol parameters control the text placed in cells. You'll probably want dendrogram = "none" as well.

御守 2024-10-01 12:10:12

您可以使用图像文本。我个人喜欢 fields 包中的 image.plot,因为它在侧面添加了图例,但您也可以将它与 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]))

You can use image and text. I personally like image.plot from the fields package, because it adds a legend on the side, but you can use it with image too.

So for instance

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]))
晨曦÷微暖 2024-10-01 12:10:12

来自 lattice 包的 levelplot() 将为您提供颜色图例。不完全是你想要的,但值得思考。

levelplot() from the lattice package will give you a color legend. Not exactly what you want but something to think about.

枕梦 2024-10-01 12:10:12

还有另一种更简单的方法来制作带有值的热图。您可以使用 pheatmap 来执行此操作。

dat <- matrix(rnorm(100, 3, 1), ncol=10)
names(dat) <- paste("X", 1:10)
install.packages('pheatmap') # if not installed already
library(pheatmap)
pheatmap(dat, display_numbers = T)

这会给你一个像这样的情节

带值的热图

There is another simpler way to make heatmaps with values. You can use pheatmap to do this.

dat <- matrix(rnorm(100, 3, 1), ncol=10)
names(dat) <- paste("X", 1:10)
install.packages('pheatmap') # if not installed already
library(pheatmap)
pheatmap(dat, display_numbers = T)

This will give you a plot like this

Heatmap with values

眼眸 2024-10-01 12:10:12

在 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)

Following lcgong (unfortunately I can post a direct comment) the pure transposition led to problems with the color representation. Accordingly, I rotated the matrix again and it worked. You can find the function as follows. Please make sure that the selected heatmap color scale works with n between 3 and 11. If desired, you can simply select another one here.

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)
究竟谁懂我的在乎 2024-10-01 12:10:12

要更新,stats::heatmap() 现在在基础 R 中执行此函数

To update, stats::heatmap() now performs this function in base R

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文