Search code examples
rggplot2heatmapheatmaply

Heatmap in R with raw values


I have this dataframe:

df <- data.frame(PatientID = c("3454","345","5","348","567","79"),
                 clas1 = c(1, 0, 5, NA, NA, 4),
                 clas2 = c(4, 1, 0, 3, 1, 0),
                 clas3 = c(1, NA, 0, 5, 5, 5), stringsAsFactors = F)

I would like to create a heatmap, with patient ID in the x axis and clas1, clas2 and clas3 in the y axis. The values represented in the heat map would be the raw value of each "clas". Here I post a drawing of what I would like

enter image description here

I apologise because I don't have available more colours to represent this, but this is only an example and any colour scale could be used. An important thing is that I would like to distinguish between zeros and NAs so ideally NAs have their own colour or appear in white (empty).

I hope this is understandable enough.

But any questions just ask

Many thanks!


Solution

  • Preparing the data

    I'll give 4 options, in all four you need to assign the rownames and remove the id column. I.e.:

    df <- data.frame(PatientID = c("3454","345","5","348","567","79"),
                     clas1 = c(1, 0, 5, NA, NA, 4),
                     clas2 = c(4, 1, 0, 3, 1, 0),
                     clas3 = c(1, NA, 0, 5, 5, 5), stringsAsFactors = F)
    rownames(df) <- df$PatientID
    df$PatientID <- NULL
    df
    

    The output is:

    > df
         clas1 clas2 clas3
    3454     1     4     1
    345      0     1    NA
    5        5     0     0
    348     NA     3     5
    567     NA     1     5
    79       4     0     5
    

    Base R

    With base R (decent output):

    heatmap(as.matrix(df))
    

    enter image description here

    gplots

    With gplots (a bit ugly, but many more parameters to control):

    library(gplots)
    heatmap.2(as.matrix(df))
    

    enter image description here

    heatmaply

    With heatmaply you have nicer defaults to use for the dendrograms (it also organizes them in a more "optimal" way).

    You can learn more about the package here.

    Static

    Static heatmap with heatmaply (better defaults, IMHO)

    library(heatmaply)
    ggheatmap(df)
    

    enter image description here

    Now with colored dendrograms

    library(heatmaply)
    ggheatmap(df, k_row = 3, k_col = 2)
    

    enter image description here

    With no dendrogram:

    library(heatmaply)
    ggheatmap(df, dendrogram = F)
    

    enter image description here

    Interactive

    Interactive heatmap with heatmaply (hover tooltip, and the ability to zoom - it's interactive!):

    library(heatmaply)
    heatmaply(df)
    

    And anything you can do with the static ggheatmap you can also do with the interactive heatmaply version.

    enter image description here