Introduction

I like the “xtable”" package very much. But when I use a large font or “flushright” latex environment, there is a caption size discrepancy and misposition of caption.

xtable.png

That’s why I developed ztable package.

ztable.png

Package “ztable” consist of one function: ztable. It’s main function is creating zebra zebra striping tables(tables with alternating row colors) in both Latex and html formats easily from mainly data.frame or an R object such as matrix, lm, aov, anova, glm and coxph objects. It is fully customizable and you can get similar tables in both latex and html format without changing source. The default output is Latex format, but you can get html format by adding just one sentence.

options(ztable.type="html")

It’s usage is somewhat similar to xtable, but very simple.

data.frame

Basic Use

It’s use is very simple. Just use ‘ztable()’ function. You can get the zebra sripig table by default.(default value zebra=1; striping on odd rows).

require(ztable)
options(ztable.type="html")
options(ztable.colnames.bold=TRUE)
ztable(head(mtcars),caption="Table 1. Basic Use")
0 0 0 0 0 0 0 0 0 0 0 0
Table 1. Basic Use
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

Tailoring zebra striping

You can get non-zebra table by change parameter zebra=NULL or change zebra striping on even rows by zebra=2.

ztable(head(mtcars),zebra=NULL,size=3,
       caption="Table 2. Non-zebra Table with small size")
0 0 0 0 0 0 0 0 0 0 0 0
Table 2. Non-zebra Table with small size
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

Customize the caption and the font size

You can change the position of table by using parameter position. You can use “r” for right position, “l” for left position and “c” for center position(default). You can change the color of zebra striping by change the parameter zebra.color. You can also change the size of font from 1 to 10(default is 5). You can change the caption.placement(“top” or “bottom”) and caption.position(“c” for center / “r” for right/ “l” for left).

ztable(head(mtcars[c(1:7)]),zebra=2,zebra.color="lightcyan",size=7,
       caption="Table 3. Left-sided caption at botom with large font",
       caption.placement="bottom",caption.position="l") 
0 0 0 0 0 0 0 0
Table 3. Left-sided caption at botom with large font
mpg cyl disp hp drat wt qsec
Mazda RX4 21.00 6.00 160.00 110.00 3.90 2.62 16.46
Mazda RX4 Wag 21.00 6.00 160.00 110.00 3.90 2.88 17.02
Datsun 710 22.80 4.00 108.00 93.00 3.85 2.32 18.61
Hornet 4 Drive 21.40 6.00 258.00 110.00 3.08 3.21 19.44
Hornet Sportabout 18.70 8.00 360.00 175.00 3.15 3.44 17.02
Valiant 18.10 6.00 225.00 105.00 2.76 3.46 20.22

aov object

‘ztable()’ can be used for ‘aov’ object. When used for ‘aov’ object, the function call is added as footer to the table. The parameter ‘show.footer’ can be used whether or not include footer in the table. Dafault value is TRUE.

out <- aov(mpg ~ ., data=mtcars)
ztable(out)
0 0 0 0 0 0
Df Sum Sq Mean Sq F value Pr(>F)
cyl 1 817.71 817.71 116.42 0.0000
disp 1 37.59 37.59 5.35 0.0309
hp 1 9.37 9.37 1.33 0.2610
drat 1 16.47 16.47 2.34 0.1406
wt 1 77.48 77.48 11.03 0.0032
qsec 1 3.95 3.95 0.56 0.4617
vs 1 0.13 0.13 0.02 0.8932
am 1 14.47 14.47 2.06 0.1659
gear 1 0.97 0.97 0.14 0.7137
carb 1 0.41 0.41 0.06 0.8122
Residuals 21 147.49 7.02
Call: aov(formula = mpg \(\sim\) ., data = mtcars)

Linear model : ‘lm’ object

‘ztable()’ can be used for ‘lm’ object. When used for ‘lm’ object, the function call is added as footer to the table, too.

fit <- lm(mpg ~ cyl + disp + wt + drat + am, data=mtcars)
ztable(fit)
0 0 0 0 0
Estimate Std. Error t value Pr(>|t|)
(Intercept) 41.2964 7.5384 5.48 0.0000
cyl -1.7940 0.6505 -2.76 0.0105
disp 0.0074 0.0123 0.60 0.5546
wt -3.5870 1.2105 -2.96 0.0064
drat -0.0936 1.5488 -0.06 0.9523
am 0.1730 1.5300 0.11 0.9109
Call: lm(formula = mpg \(\sim\) cyl + disp + wt + drat + am, data = mtcars)

Analysis of Variance Table : ‘anova’ object

‘ztable()’ can be used for ‘anova’ object to show the anova table. When used for ‘anova’ object, headings of anova are added as headings to the table. The parameter ‘show.footer’ can be used whether or not include footer in the table. Dafault value is TRUE.

a=anova(fit)
ztable(a)
0 0 0 0 0 0
Analysis of Variance Table
Response: mpg
Df Sum Sq Mean Sq F value Pr(>F)
cyl 1 817.71 817.71 112.85 0.0000
disp 1 37.59 37.59 5.19 0.0312
wt 1 82.25 82.25 11.35 0.0024
drat 1 0.00 0.00 0.00 0.9939
am 1 0.09 0.09 0.01 0.9109
Residuals 26 188.40 7.25

This is examples of another ‘anova’ object. The models in this anova tables showed as table headings. You can decide whether or not include the headings in the tableby using parameter ‘show.heading’(default: TRUE).

fit2 <- lm(mpg ~ cyl+wt, data=mtcars)
b=anova(fit2,fit)
ztable(b)
0 0 0 0 0 0 0
Analysis of Variance Table
Model 1: mpg \(\sim\) cyl + wt
Model 2: mpg \(\sim\) cyl + disp + wt + drat + am
Res.Df RSS Df Sum of Sq F Pr(>F)
1 29.0 191.17
2 26.0 188.40 3.0 2.77 0.13 0.9429
ztable(b,show.heading=FALSE)
0 0 0 0 0 0 0
Res.Df RSS Df Sum of Sq F Pr(>F)
1 29.0 191.17
2 26.0 188.40 3.0 2.77 0.13 0.9429

Generalized linear model ; ‘glm’ object

‘ztable()’ can be used for ‘glm’(generalized linear model) object. In this time, ‘ztable()’ shows the odds ratio(OR) and 95% confidence interval as well as atandard R output.

require(survival)
## Loading required package: survival
## Loading required package: splines
data(colon)
attach(colon)
out <- glm(status ~ rx+obstruct+adhere+nodes+extent, data=colon, family=binomial)
ztable(out)
0 0 0 0 0 0 0 0
Estimate Std. Error z value Pr(>|z|) OR lcl ucl
(Intercept) -2.3642 0.3426 -6.90 0.0000 0.09 0.05 0.18
rxLev -0.0712 0.1203 -0.59 0.5538 0.93 0.74 1.18
rxLev+5FU -0.6135 0.1231 -4.98 0.0000 0.54 0.42 0.69
obstruct 0.2320 0.1251 1.85 0.0636 1.26 0.99 1.61
adhere 0.4164 0.1429 2.91 0.0036 1.52 1.15 2.01
nodes 0.1845 0.0183 10.06 0.0000 1.20 1.16 1.25
extent 0.6238 0.1142 5.46 0.0000 1.87 1.50 2.34
Call: glm(formula = status \(\sim\) rx + obstruct + adhere + nodes + extent, family = binomial, data = colon)

Again, ‘ztable()’ also shows the anova table of this model.

ztable(anova(out))
0 0 0 0 0
Analysis of Deviance Table
Model: binomial, link: logit
Response: status
Terms added sequentially (first to last)
Df Deviance Resid. Df Resid. Dev
NULL 1821 2525.40
rx 2 34.84 1819 2490.56
obstruct 1 3.66 1818 2486.90
adhere 1 11.74 1817 2475.16
nodes 1 145.01 1816 2330.15
extent 1 32.59 1815 2297.55

More ‘aov’ object

op <- options(contrasts = c("contr.helmert", "contr.poly"))
npk.aov <- aov(yield ~ block + N*P*K, npk) 
ztable(npk.aov,zebra=1)
0 0 0 0 0 0
Df Sum Sq Mean Sq F value Pr(>F)
block 5 343.29 68.66 4.45 0.0159
N 1 189.28 189.28 12.26 0.0044
P 1 8.40 8.40 0.54 0.4749
K 1 95.20 95.20 6.17 0.0288
N:P 1 21.28 21.28 1.38 0.2632
N:K 1 33.14 33.14 2.15 0.1686
P:K 1 0.48 0.48 0.03 0.8628
Residuals 12 185.29 15.44
Call: aov(formula = yield \(\sim\) block + N * P * K, data = npk)

More ‘lm’ object

ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
group <- gl(2, 10, 20, labels = c("Ctl","Trt"))
weight <- c(ctl, trt)
lm.D9 <- lm(weight ~ group)
ztable(lm.D9)
0 0 0 0 0
Estimate Std. Error t value Pr(>|t|)
(Intercept) 4.8465 0.1557 31.12 0.0000
group1 -0.1855 0.1557 -1.19 0.2490
Call: lm(formula = weight \(\sim\) group)
ztable(anova(lm.D9))
0 0 0 0 0 0
Analysis of Variance Table
Response: weight
Df Sum Sq Mean Sq F value Pr(>F)
group 1 0.69 0.69 1.42 0.2490
Residuals 18 8.73 0.48

More ‘glm’ object

counts <- c(18,17,15,20,10,20,25,13,12)
outcome <- gl(3,1,9)
treatment <- gl(3,3)
d.AD <- data.frame(treatment, outcome, counts)
glm.D93 <- glm(counts ~ outcome + treatment, family = poisson())
ztable(glm.D93)
0 0 0 0 0 0 0 0
Estimate Std. Error z value Pr(>|z|) OR lcl ucl
(Intercept) 2.7954 0.0831 33.64 0.0000 16.37 13.84 19.18
outcome1 -0.2271 0.1011 -2.25 0.0246 0.80 0.65 0.97
outcome2 -0.0220 0.0592 -0.37 0.7106 0.98 0.87 1.10
treatment1 -0.0000 0.1000 -0.00 1.0000 1.00 0.82 1.22
treatment2 -0.0000 0.0577 -0.00 1.0000 1.00 0.89 1.12
Call: glm(formula = counts \(\sim\) outcome + treatment, family = poisson())

Principal Components Analysis : ‘prcomp’ object

‘ztable()’ can be used in principal components analysis. Followings are examples of ztable() of ‘prcomp’ object.

data(USArrests)
pr1 <- prcomp(USArrests) 
ztable(pr1)
0 0 0 0 0
Rotation:
PC1 PC2 PC3 PC4
Murder 0.0417 -0.0448 0.0799 -0.9949
Assault 0.9952 -0.0588 -0.0676 0.0389
UrbanPop 0.0463 0.9769 -0.2005 -0.0582
Rape 0.0752 0.2007 0.9741 0.0723
ztable(summary(pr1))
0 0 0 0 0
Importance of components:
PC1 PC2 PC3 PC4
Standard deviation 83.7324 14.2124 6.4894 2.4828
Proportion of Variance 0.9655 0.0278 0.0058 0.0008
Cumulative Proportion 0.9655 0.9933 0.9991 1.0000

Survival Analysis : ‘coxph’ object

‘ztable()’ can be used in principal components analysis. When used for Cox proportional hazard model, ‘ztable()’ showed the hazard ratio and 95% confidence interval ready for publication to medical journal.

colon$TS = Surv(time,status==1) 
out=coxph(TS~rx+obstruct+adhere+differ+extent+surg+node4,data=colon)
ztable(out)
0 0 0 0 0 0 0
HR lcl ucl se(coef) z Pr(>|z|)
rx1 0.999 0.925 1.079 0.039 -0.030 0.9764
rx2 0.871 0.829 0.915 0.025 -5.464 0.0000
obstruct 1.267 1.079 1.489 0.082 2.885 0.0039
adhere 1.181 0.991 1.409 0.090 1.856 0.0634
differ 1.219 1.067 1.394 0.068 2.906 0.0037
extent 1.523 1.298 1.787 0.082 5.152 0.0000
surg 1.274 1.104 1.469 0.073 3.319 0.0009
node4 2.359 2.059 2.702 0.069 12.383 0.0000
Call: coxph(formula = TS \(\sim\) rx + obstruct + adhere + differ + extent + surg + node4, data = colon)

Customize the zebra striping colors

If you wanted to use several colors for zebra striping, you can set the parameter ‘zebra’ to zero(e.g. zebra=0) and set the ‘zebra.color’ parameter with vector of your favorite colors. Your favorite colors are used to zebra striping. For your convienience, ten colors are predifned for this purpose. The predefined colors are: c(“peach”,“peach-orange”,“peachpuff”,“peach-yellow”,“pear”,“pearl”,“peridot”,“periwinkle”,“pastelred”,“pastelgray”).

ztable(head(mtcars,15),zebra=0) 
0 0 0 0 0 0 0 0 0 0 0 0
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
Duster 360 14.30 8.00 360.00 245.00 3.21 3.57 15.84 0.00 0.00 3.00 4.00
Merc 240D 24.40 4.00 146.70 62.00 3.69 3.19 20.00 1.00 0.00 4.00 2.00
Merc 230 22.80 4.00 140.80 95.00 3.92 3.15 22.90 1.00 0.00 4.00 2.00
Merc 280 19.20 6.00 167.60 123.00 3.92 3.44 18.30 1.00 0.00 4.00 4.00
Merc 280C 17.80 6.00 167.60 123.00 3.92 3.44 18.90 1.00 0.00 4.00 4.00
Merc 450SE 16.40 8.00 275.80 180.00 3.07 4.07 17.40 0.00 0.00 3.00 3.00
Merc 450SL 17.30 8.00 275.80 180.00 3.07 3.73 17.60 0.00 0.00 3.00 3.00
Merc 450SLC 15.20 8.00 275.80 180.00 3.07 3.78 18.00 0.00 0.00 3.00 3.00
Cadillac Fleetwood 10.40 8.00 472.00 205.00 2.93 5.25 17.98 0.00 0.00 3.00 4.00

The color names used for this purpose are predefined in the data ‘zcolors’ included in ‘ztable’ package. Please type ‘?zcolors’ in R console for help file or just type ‘zcolors’. You can see 749 color names defined in data ‘zcolors’.