메뉴 건너뛰기

웹에서 하는 R 통계

Make a Heatmap Table using ztable

2018.05.23 00:33

cardiomoon 조회 수:19

Make a Heatmap Table using ztable

Keon-Woong Moon

2018-05-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-smokerNeverSmoker
NSTEMI425061
STEMI6697141
Unstable Angina96185119

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-smokerNeverSmoker
NSTEMI425061
STEMI6697141
Unstable Angina96185119

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.LengthSepal.WidthPetal.LengthPetal.WidthSpecies
15.103.501.400.20setosa
24.903.001.400.20setosa
34.703.201.300.20setosa
44.603.101.500.20setosa
55.003.601.400.20setosa
65.403.901.700.40setosa

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-smokerNeverSmoker
NSTEMI425061
STEMI6697141
Unstable Angina96185119

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
 mpgcyldisphpdratwtqsecvsamgearcarb
Mazda RX421.006.00160.00110.003.902.6216.460.001.004.004.00
Mazda RX4 Wag21.006.00160.00110.003.902.8817.020.001.004.004.00
Datsun 71022.804.00108.0093.003.852.3218.611.001.004.001.00
Hornet 4 Drive21.406.00258.00110.003.083.2119.441.000.003.001.00
Hornet Sportabout18.708.00360.00175.003.153.4417.020.000.003.002.00
Valiant18.106.00225.00105.002.763.4620.221.000.003.001.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
 mpgcyldisphpdrat
Mazda RX421.006.00160.00110.003.90
Mazda RX4 Wag21.006.00160.00110.003.90
Datsun 71022.804.00108.0093.003.85
Hornet 4 Drive21.406.00258.00110.003.08
Hornet Sportabout18.708.00360.00175.003.15
Valiant18.106.00225.00105.002.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
 agesexcardiogenicShockentryDxEFheightweightBMIobesity
162MaleNoFemoralSTEMI18.00168.0072.0025.51Yes
278FemaleNoFemoralSTEMI18.40148.0048.0021.91No
376FemaleYesFemoralSTEMI20.00No
489FemaleNoFemoralSTEMI21.80165.0050.0018.37No
556MaleNoRadialNSTEMI21.80162.0064.0024.39No
673FemaleNoRadialUnstable Angina22.00153.0059.0025.20Yes

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
 mpgcyldisphpdratwtqsecvsamgearcarb
Mazda RX421.006.00160.00110.003.902.6216.460.001.004.004.00
Mazda RX4 Wag21.006.00160.00110.003.902.8817.020.001.004.004.00
Datsun 71022.804.00108.0093.003.852.3218.611.001.004.001.00
Hornet 4 Drive21.406.00258.00110.003.083.2119.441.000.003.001.00
Hornet Sportabout18.708.00360.00175.003.153.4417.020.000.003.002.00
Valiant18.106.00225.00105.002.763.4620.221.000.003.001.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 RX4Mazda RX4 WagDatsun 710Hornet 4 DriveHornet SportaboutValiant
mpg21.0021.0022.8021.4018.7018.10
cyl6.006.004.006.008.006.00
disp160.00160.00108.00258.00360.00225.00
hp110.00110.0093.00110.00175.00105.00
drat3.903.903.853.083.152.76
wt2.622.882.323.213.443.46
qsec16.4617.0218.6119.4417.0220.22
vs0.000.001.001.000.001.00
am1.001.001.000.000.000.00
gear4.004.004.003.003.003.00
carb4.004.001.001.002.001.00
번호 제목 글쓴이 날짜 조회 수
공지 웹에서 하는 R 메타분석 [1] cardiomoon 2015.11.11 3856
공지 웹에서 하는 R 통계 게시판입니다. cardiomoon 2015.03.26 998
82 moonBook 패키지 업데이트 안내 cardiomoon 2018.05.26 58
» Make a Heatmap Table using ztable cardiomoon 2018.05.23 19
80 moonBook 패키지 업데이트 안내 cardiomoon 2018.05.18 1471
79 ztable 패키지 업데이트 안내 cardiomoon 2018.05.13 36
78 pptxList: A Shiny Module for Reproducible Research cardiomoon 2018.03.07 114
77 Nightingale's Rose Plot file cardiomoon 2017.11.16 215
76 Plot for distribution of common statistics and p-value cardiomoon 2017.11.11 300
75 R package gglotAssist cardiomoon 2017.11.11 373
74 설문조사데이터 cardiomoon 2017.10.14 537
73 R에서 데이터 편집을 하자 - editData 패키지 [1] cardiomoon 2017.09.25 878
72 dplyrAssist 패키지 : RStudio Addin으로 dplyr을 쉽게 배우기 [1] cardiomoon 2017.09.03 253
71 "틀리지않는법" 강의슬라이드(문건웅) cardiomoon 2017.08.31 783
70 연속형변수를 기준으로 같은 크기를 갖는 여러 개의 그룹으로 나누기 cardiomoon 2017.06.19 383
69 3D visualization of multiple regression analysis file cardiomoon 2017.06.15 287
68 Visualize multiple regression model cardiomoon 2017.06.12 481
67 Longest common substring cardiomoon 2017.05.29 436
66 파일을 업로드 했는데 예제데이터에서 대체가 안됩니다. [2] guriguribangbang 2017.05.16 564
65 샤이니 앱 : interactive ggplot [1] file cardiomoon 2017.02.28 1112
64 Procedural Programming vs Functional Programming(I) cardiomoon 2017.02.27 136
63 함수곡선 아래의 면적 구하기 cardiomoon 2016.12.22 543