knitr::opts_chunk$set( collapse = TRUE, comment = NA, message=FALSE )
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")
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
You can make html
or LaTex
table easily with ztable.
library(ztable) library(magrittr) options(ztable.type="html") z=ztable(x) print(z,caption="Table 1. Basic 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")
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")
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.
z %>% makeHeatmap() %>% print(caption="Table 4. Heatmap Table")
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")
With the gradientColor() function, you makes sequential colour gradient palette easily.
mycolor=gradientColor(low="yellow",mid="orange",high="red",n=20,plot=TRUE) mycolor
ztable(head(mtcars[1:5])) %>% makeHeatmap(mycolor=mycolor) %>% print(caption="Table 6. Heatmap table with user-defined palette")
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")
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")
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")
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.