display a matrix, including the values, as a heatmap

后端 未结 6 1075
悲&欢浪女
悲&欢浪女 2020-12-05 13:29

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
A1SO604B523Q6         


        
相关标签:
6条回答
  • 2020-12-05 13:49

    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)
    
    0 讨论(0)
  • levelplot() from the lattice package will give you a color legend. Not exactly what you want but something to think about.

    0 讨论(0)
  • 2020-12-05 14:01

    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])
    
    0 讨论(0)
  • 2020-12-05 14:07

    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

    0 讨论(0)
  • 2020-12-05 14:08

    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.

    0 讨论(0)
  • 2020-12-05 14:08

    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]))
    
    0 讨论(0)
提交回复
热议问题