ztable
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")
::install_github("cardiomoon/ztable") devtools
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)
=table(acs$Dx,acs$smoking)
x
x
-smoker Never Smoker
Ex42 50 61
NSTEMI 66 97 141
STEMI 96 185 119 Unstable Angina
You can make html
or LaTex
table easily with ztable.
library(ztable)
library(magrittr)
options(ztable.type="html")
=ztable(x)
zprint(z,caption="Table 1. Basic Table")
Ex-smoker | Never | Smoker | |
---|---|---|---|
NSTEMI | 42 | 50 | 61 |
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")
Ex-smoker | Never | Smoker | |
---|---|---|---|
NSTEMI | 42 | 50 | 61 |
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")
Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species | |
---|---|---|---|---|---|
1 | 5.10 | 3.50 | 1.40 | 0.20 | setosa |
You can make a heatmap table in which background colors representing the values. With makeHeatmap() function, you can make a heatmap table easily. The makeHeatmap() function apply the “Reds” palette from RColorBrewer package.
%>% makeHeatmap() %>% print(caption="Table 4. Heatmap Table") z
Ex-smoker | Never | Smoker | |
---|---|---|---|
NSTEMI | 42 | 50 | 61 |
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")
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 |
With the gradientColor() function, you makes sequential colour gradient palette easily.
=gradientColor(low="yellow",mid="orange",high="red",n=20,plot=TRUE) mycolor
mycolor1] "#FFFF00" "#FFF500" "#FFEB00" "#FFE100" "#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")
mpg | cyl | disp | hp | drat | |
---|---|---|---|---|---|
Mazda RX4 | 21.00 | 6.00 | 160.00 | 110.00 | 3.90 |
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")
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 |
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")
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 |
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")
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 |