Make a Heatmap Table using ztable
Author: Keon-Woong Moon
Date: 2018-07-23

# Make a Heatmap Table using `ztable`

#### Keon-Woong Moon

#### 2018-07-23

## Installation

You can install R package “ztable” from CRAN. Current version is 0.1.8.

    install.packages("ztable")

To make a heatmap table, you have to install the developmental version of ztable from github. Current github version is 0.2.0.

    if(!require(devtools)) install.packages("devtools")
    devtools::install_github("cardiomoon/ztable")

## Introduction

A heat map (or heatmap) is a graphical representation of data where the individual values contained in a matrix are represented as colors. You can summarize the the diagnosis and smoking status of 857 patients of acute coronary syndrome(acs) using table() function.

    require(moonBook)
    x=table(acs$Dx,acs$smoking)
    x

                      Ex-smoker Never Smoker
      NSTEMI                 42    50     61
      STEMI                  66    97    141
      Unstable Angina        96   185    119

## Basic Table

You can make `html` or `LaTex` table easly with ztable.

    library(ztable)
    library(magrittr)
    options(ztable.type="html")
    z=ztable(x)
    print(z,caption="Table 1. Basic Table")

Table 1. Basic Table
| Ex-smoker | Never | Smoker
--- | --- | --- | ---
NSTEMI | 42 | 50 | 61
STEMI | 66 | 97 | 141
Unstable Angina | 96 | 185 | 119

## Formatting the Table

You can change the background color and font color of `ztable` using addCellColor function. For example, you can change the cell color of the 3rd row, 2nd column. Please keep in mind that the ztable count colname and rowname.

    z %>%
        addCellColor(4,3,bg="orange",color="white") %>%
        print(caption="Table 2. Add Cell Color")

Table 2. Add Cell Color
| Ex-smoker | Never | Smoker
--- | --- | --- | ---
NSTEMI | 42 | 50 | 61
STEMI | 66 | 97 | 141
Unstable Angina | 96 | 185 | 119

## Conditional Formatting

You can select rows with logical expression. You can select cols with column name.

    ztable(head(iris),caption="Table 3. Conditinoal Formatting: Sepal.Width >= 3.5") %>%
        addRowColor(rows=1,bg="#C90000",color="white") %>%
        addCellColor(condition=Sepal.Width>=3.5,cols=Sepal.Width,color="red")

Table 3. Conditinoal Formatting: Sepal.Width >= 3.5
| Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species
--- | --- | --- | --- | --- | ---
1 | 5.10 | 3.50 | 1.40 | 0.20 | setosa
2 | 4.90 | 3.00 | 1.40 | 0.20 | setosa
3 | 4.70 | 3.20 | 1.30 | 0.20 | setosa
4 | 4.60 | 3.10 | 1.50 | 0.20 | setosa
5 | 5.00 | 3.60 | 1.40 | 0.20 | setosa
6 | 5.40 | 3.90 | 1.70 | 0.40 | setosa

## Make a Heatmap Table

You can make a heatmap table in which background colors representing the values. With makeHeatmap() function, you can make a heatmap table easily. The namakeHeatmap() function apply the “Reds” palette from RColorBrewer package.

    z %>% makeHeatmap() %>% print(caption="Table 4. Heatmap Table")

Table 4. Heatmap Table
| Ex-smoker | Never | Smoker
--- | --- | --- | ---
NSTEMI | 42 | 50 | 61
STEMI | 66 | 97 | 141
Unstable Angina | 96 | 185 | 119

## Heatmap Table with desired palette

You can change the palette with palette argument. For example, you can use the “Blue” palette.

    ztable(head(mtcars)) %>%
        makeHeatmap(palette="Blues") %>%
        print(caption="Table 5. Heatmap table with 'Blue' palette")

Table 5. Heatmap table with ‘Blue’ palette
| mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb
--- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | ---
Mazda RX4 | 21.00 | 6.00 | 160.00 | 110.00 | 3.90 | 2.62 | 16.46 | 0.00 | 1.00 | 4.00 | 4.00
Mazda RX4 Wag | 21.00 | 6.00 | 160.00 | 110.00 | 3.90 | 2.88 | 17.02 | 0.00 | 1.00 | 4.00 | 4.00
Datsun 710 | 22.80 | 4.00 | 108.00 | 93.00 | 3.85 | 2.32 | 18.61 | 1.00 | 1.00 | 4.00 | 1.00
Hornet 4 Drive | 21.40 | 6.00 | 258.00 | 110.00 | 3.08 | 3.21 | 19.44 | 1.00 | 0.00 | 3.00 | 1.00
Hornet Sportabout | 18.70 | 8.00 | 360.00 | 175.00 | 3.15 | 3.44 | 17.02 | 0.00 | 0.00 | 3.00 | 2.00
Valiant | 18.10 | 6.00 | 225.00 | 105.00 | 2.76 | 3.46 | 20.22 | 1.00 | 0.00 | 3.00 | 1.00

## Heatmap Table with user-defined palette

With the gradientColor() function, you makes sequential colour graient palette easily.

    mycolor=gradientColor(low="yellow",mid="orange",high="red",n=20,plot=TRUE)

    mycolor
     [1] "#FFFF00" "#FFF500" "#FFEB00" "#FFE200" "#FFD800" "#FFCE00" "#FFC400"
     [8] "#FFBA00" "#FFAF00" "#FFA500" "#FF9B00" "#FF9000" "#FF8600" "#FF7A00"
    [15] "#FF6E00" "#FF6200" "#FF5300" "#FF4300" "#FF2E00" "#FF0000"

    ztable(head(mtcars[1:5])) %>%
        makeHeatmap(mycolor=mycolor) %>%
        print(caption="Table 6. Heatmap table with user-defined palette")

Table 6. Heatmap table with user-defined palette
| mpg | cyl | disp | hp | drat
--- | --- | --- | --- | --- | ---
Mazda RX4 | 21.00 | 6.00 | 160.00 | 110.00 | 3.90
Mazda RX4 Wag | 21.00 | 6.00 | 160.00 | 110.00 | 3.90
Datsun 710 | 22.80 | 4.00 | 108.00 | 93.00 | 3.85
Hornet 4 Drive | 21.40 | 6.00 | 258.00 | 110.00 | 3.08
Hornet Sportabout | 18.70 | 8.00 | 360.00 | 175.00 | 3.15
Valiant | 18.10 | 6.00 | 225.00 | 105.00 | 2.76

## Heatmap Table with non-numeric data

You can make heatmap table with data containing non-numeric columns. Only columns with numeric data affected by this function.

    ztable(head(acs[1:10])) %>%
        makeHeatmap %>%
        print(caption="Table 7. Heatmap table with non-numeric data")

Table 7. Heatmap table with non-numeric data
| age | sex | cardiogenicShock | entry | Dx | EF | height | weight | BMI | obesity
--- | --- | --- | --- | --- | --- | --- | --- | --- | --- | ---
1 | 62 | Male | No | Femoral | STEMI | 18.00 | 168.00 | 72.00 | 25.51 | Yes
2 | 78 | Female | No | Femoral | STEMI | 18.40 | 148.00 | 48.00 | 21.91 | No
3 | 76 | Female | Yes | Femoral | STEMI | 20.00 |  |  |  | No
4 | 89 | Female | No | Femoral | STEMI | 21.80 | 165.00 | 50.00 | 18.37 | No
5 | 56 | Male | No | Radial | NSTEMI | 21.80 | 162.00 | 64.00 | 24.39 | No
6 | 73 | Female | No | Radial | Unstable Angina | 22.00 | 153.00 | 59.00 | 25.20 | Yes

## Selected Columnwise Heatmap Table

You can make selected columnwise heatmap table. You can select columns with `cols` argument. To make columnwise heatmap table, set the `margin` argument 2.

    ztable(head(mtcars)) %>%
        makeHeatmap(palette="YlOrRd",cols=c(1,3,4),margin=2) %>%
        print(caption="Table 8. Columnwise heatmap table")

Table 8. Columnwise heatmap table
| mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb
--- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | ---
Mazda RX4 | 21.00 | 6.00 | 160.00 | 110.00 | 3.90 | 2.62 | 16.46 | 0.00 | 1.00 | 4.00 | 4.00
Mazda RX4 Wag | 21.00 | 6.00 | 160.00 | 110.00 | 3.90 | 2.88 | 17.02 | 0.00 | 1.00 | 4.00 | 4.00
Datsun 710 | 22.80 | 4.00 | 108.00 | 93.00 | 3.85 | 2.32 | 18.61 | 1.00 | 1.00 | 4.00 | 1.00
Hornet 4 Drive | 21.40 | 6.00 | 258.00 | 110.00 | 3.08 | 3.21 | 19.44 | 1.00 | 0.00 | 3.00 | 1.00
Hornet Sportabout | 18.70 | 8.00 | 360.00 | 175.00 | 3.15 | 3.44 | 17.02 | 0.00 | 0.00 | 3.00 | 2.00
Valiant | 18.10 | 6.00 | 225.00 | 105.00 | 2.76 | 3.46 | 20.22 | 1.00 | 0.00 | 3.00 | 1.00

## Selected Rowwise Heatmap Table

You can make selected columnwise heatmap table. You can select rows with `rows` argument. To make rowwise heatmap table, set the `margin` argument 1.

    ztable(t(head(mtcars))) %>%
        makeHeatmap(palette="YlOrRd",rows=c(1,3,4),margin=1) %>%
        print(caption="Table 9. Rowwise heatmap table")

Table 9. Rowwise heatmap table
| Mazda RX4 | Mazda RX4 Wag | Datsun 710 | Hornet 4 Drive | Hornet Sportabout | Valiant
--- | --- | --- | --- | --- | --- | ---
mpg | 21.00 | 21.00 | 22.80 | 21.40 | 18.70 | 18.10
cyl | 6.00 | 6.00 | 4.00 | 6.00 | 8.00 | 6.00
disp | 160.00 | 160.00 | 108.00 | 258.00 | 360.00 | 225.00
hp | 110.00 | 110.00 | 93.00 | 110.00 | 175.00 | 105.00
drat | 3.90 | 3.90 | 3.85 | 3.08 | 3.15 | 2.76
wt | 2.62 | 2.88 | 2.32 | 3.21 | 3.44 | 3.46
qsec | 16.46 | 17.02 | 18.61 | 19.44 | 17.02 | 20.22
vs | 0.00 | 0.00 | 1.00 | 1.00 | 0.00 | 1.00
am | 1.00 | 1.00 | 1.00 | 0.00 | 0.00 | 0.00
gear | 4.00 | 4.00 | 4.00 | 3.00 | 3.00 | 3.00
carb | 4.00 | 4.00 | 1.00 | 1.00 | 2.00 | 1.00