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.zebra=1)
options(ztable.zebra.color="platinum")
options(ztable.colnames.bold=TRUE)
ztable(head(mtcars))
  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 1. Non-zebra Table with small size")
Table 1. 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 2. Left-sided caption at botom with large font",
       caption.placement="bottom",caption.position="l") 
Table 2. 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)
  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)
  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)
str(a)

Classes ‘anova’ and ‘data.frame’: 6 obs. of 5 variables: $ Df : int 1 1 1 1 1 26 $ Sum Sq : num 8.18e+02 3.76e+01 8.22e+01 4.29e-04 9.26e-02 … $ Mean Sq: num 8.18e+02 3.76e+01 8.22e+01 4.29e-04 9.26e-02 … $ F value: num 1.13e+02 5.19 1.14e+01 5.93e-05 1.28e-02 … $ Pr(>F) : num 5.94e-11 3.12e-02 2.36e-03 9.94e-01 9.11e-01 … - attr(*, “heading”)= chr “Analysis of Variance Table” “Response: mpg”

ztable(a)
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)
str(b)

Classes ‘anova’ and ‘data.frame’: 2 obs. of 6 variables: $ Res.Df : num 29 26 $ RSS : num 191 188 $ Df : num NA 3 $ Sum of Sq: num NA 2.77 $ F : num NA 0.128 $ Pr(>F) : num NA 0.943 - attr(*, “heading”)= chr “Analysis of Variance Table” “Model 1: mpg ~ cyl + wt2: mpg ~ cyl + disp + wt + drat + am”

ztable(b)
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)
  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)
  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))
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)
  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)
  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),align="|c|rrrr|r|")
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)
  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)
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))
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)
  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,zebra.color=NULL) 
  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’.

Place two or more ztables or figures side by side

If you wanted to place two or more ztables or figures side by side, you can use function parallelTables(). Function parallelTables() takes three parameters. The first parameter width is a numeric vector specifies the width to which the tables or figures should be scaled. The second parameter is a list of ztable or names of valid figure. The 3rd parameter ‘type’ is the type of table to produce. Possible values for type are “latex” or “html”. Default value is “latex”. See the examples.

z=ztable(head(mtcars[1:3]),tabular=TRUE,zebra.color="peach-orange")
z1=ztable(head(iris[1:3]),tabular=TRUE,zebra=2)

parallelTables(width=c(0.5,0.5),list(z,z1),type="html")
  mpg cyl disp
Mazda RX4 21.00 6.00 160.00
Mazda RX4 Wag 21.00 6.00 160.00
Datsun 710 22.80 4.00 108.00
Hornet 4 Drive 21.40 6.00 258.00
Hornet Sportabout 18.70 8.00 360.00
Valiant 18.10 6.00 225.00
  Sepal.Length Sepal.Width Petal.Length
1 5.10 3.50 1.40
2 4.90 3.00 1.40
3 4.70 3.20 1.30
4 4.60 3.10 1.50
5 5.00 3.60 1.40
6 5.40 3.90 1.70
parallelTables(width=c(0.5,0.5),list(z,"figures/ztable3.png"),type="html")
  mpg cyl disp
Mazda RX4 21.00 6.00 160.00
Mazda RX4 Wag 21.00 6.00 160.00
Datsun 710 22.80 4.00 108.00
Hornet 4 Drive 21.40 6.00 258.00
Hornet Sportabout 18.70 8.00 360.00
Valiant 18.10 6.00 225.00

Vertical striping

If you wanted to vertical striping table, you can get it by set the parameter zebra.type 2. You can change the ztables parameters when printing.

z1=ztable(head(iris),zebra=2)
z1
  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
print(z1,zebra.type=2)
  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
print(z1,zebra=1,zebra.type=2,zebra.colnames=TRUE)
  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

More tailoring zebra striping

You can update parameters of ztable with ‘update_ztable’ function.

options(ztable.zebra.color=NULL)
(z1=ztable(head(iris),zebra=0,zebra.type=2))
  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

You can change the background color of colnames rows by setting zebra.colnames=TRUE.

update_ztable(z1,colnames.bold=TRUE,zebra.colnames=TRUE)
  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

You can customize the striping when printing.

print(z1,zebra.color=c(rep("white",5),"peach"),zebra.colnames=TRUE)
  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

Change the background color of all cells

You can change the background color of all cells by setting the zebra.type=0.

ztable(head(iris),zebra=0,zebra.type=0)
  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
ztable(head(iris),zebra=0,zebra.type=0,zebra.color=zcolors$name,zebra.colnames=TRUE)
  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

Diagonal striping

You can make diagonal striping with use of zebra.color greater/lesser than column length by 1.

ztable(head(iris),zebra=0,zebra.type=0,zebra.color=1:7,zebra.colnames=TRUE)
  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
ztable(head(mtcars[,1:9]),zebra=0,zebra.type=0,zebra.color=1:9,zebra.colnames=TRUE)
  mpg cyl disp hp drat wt qsec vs am
Mazda RX4 21.00 6.00 160.00 110.00 3.90 2.62 16.46 0.00 1.00
Mazda RX4 Wag 21.00 6.00 160.00 110.00 3.90 2.88 17.02 0.00 1.00
Datsun 710 22.80 4.00 108.00 93.00 3.85 2.32 18.61 1.00 1.00
Hornet 4 Drive 21.40 6.00 258.00 110.00 3.08 3.21 19.44 1.00 0.00
Hornet Sportabout 18.70 8.00 360.00 175.00 3.15 3.44 17.02 0.00 0.00
Valiant 18.10 6.00 225.00 105.00 2.76 3.46 20.22 1.00 0.00

Two hundred background colors

This is demonstration of 200 background colors. All 749 colors are available in package ztable. Please type ?zcolors.

mycolor=rep("white",6)
for(i in 1:40){
    mycolor=c(mycolor,"white",zcolors$name[((i-1)*5+1):((i-1)*5+5)])
}
a=c(zcolors$name[1:5])
for(i in 2:40){
    a=rbind(a,zcolors$name[((i-1)*5+1):((i-1)*5+5)])
}
a=data.frame(a,stringsAsFactors=FALSE,row.names=NULL)
ztable(a,zebra=0,zebra.type=0,zebra.color=mycolor,include.rownames=FALSE,
       include.colnames=FALSE,longtable=TRUE)
airforceblue aliceblue alizarin almond amaranth
amber ambersaeece americanrose amethyst anti-flashwhite
antiquebrass antiquefuchsia antiquewhite ao aoenglish
applegreen apricot aqua aquamarine armygreen
arsenic arylideyellow ashgrey asparagus atomictangerine
auburn aureolin aurometalsaurus awesome azurecolorwheel
azurewebazuremist babyblue babyblueeyes babypink ballblue
bananamania bananayellow battleshipgrey bazaar beaublue
beaver beige bisque bistre bittersweet
black blanchedalmond bleudefrance blizzardblue blond
blue bluemunsell bluencs bluepigment blueryb
bluebell bluegray blue-green blue-violet blush
bole bondiblue bostonuniversityred brandeisblue brass
brickred brightcerulean brightgreen brightlavender brightmaroon
brightpink brightturquoise brightube brilliantlavender brilliantrose
brinkpink britishracinggreen bronze browntraditional brownweb
bubblegum bubbles buff bulgarianrose burgundy
burlywood burntorange burntsienna burntumber byzantine
byzantium cadet cadetblue cadetgrey cadmiumgreen
cadmiumorange cadmiumred cadmiumyellow calpolypomonagreen cambridgeblue
camel camouflagegreen canaryyellow candyapplered candypink
capri caputmortuum cardinal caribbeangreen carmine
carminepink carminered carnationpink carnelian carolinablue
carrotorange ceil celadon celestialblue cerise
cerisepink cerulean ceruleanblue chamoisee champagne
charcoal chartreusetraditional chartreuseweb cherryblossompink chestnut
chocolatetraditional chocolateweb chromeyellow cinereous cinnabar
cinnamon citrine classicrose cobalt cocoabrown
columbiablue coolblack coolgrey copper copperrose
coquelicot coral coralpink coralred cordovan
corn cornellred cornflowerblue cornsilk cosmiclatte
cottoncandy cream crimson crimsonglory cyan
cyanprocess daffodil dandelion darkblue darkbrown
darkbyzantium darkcandyapplered darkcerulean darkchampagne darkchestnut
darkcoral darkcyan darkelectricblue darkgoldenrod darkgray
darkgreen darkjunglegreen darkkhaki darklava darklavender
darkmagenta darkmidnightblue darkolivegreen darkorange darkorchid
darkpastelblue darkpastelgreen darkpastelpurple darkpastelred darkpink
darkpowderblue darkraspberry darkred darksalmon darkscarlet
darkseagreen darksienna darkslateblue darkslategray darkspringgreen

Add row colors, column colors and cell colors

options(ztable.zebra=NULL)
z=ztable(head(mtcars))
z
  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
z=addRowColor(z,c(4,7),color="pink")
z=addColColor(z,7,color="amber")
z=addCellColor(z,cols=7,rows=c(4,7),color="orange")
z
  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

Add row groups

rgroup=c("Group A","Group B")
n.rgroup=c(4,2)
z=addrgroup(z,rgroup=rgroup,n.rgroup=n.rgroup)
z
  mpg cyl disp hp drat wt qsec vs am gear carb
Group A
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
Group B
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
z=addrgroup(z,rgroup=rgroup,n.rgroup=n.rgroup,cspan.rgroup=1)
z=addColColor(z,7,color="amber")
z
  mpg cyl disp hp drat wt qsec vs am gear carb
Group A
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
Group B
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

Add column groups

z=ztable(head(mtcars),digits=0)
z=addColColor(z,10,color="platinum")
cgroup=c("Group A","Group B","Group C")
n.cgroup=c(3,4,4)
cgroupcolor=c("white","white","white","platinum")
z=addcgroup(z,cgroup=cgroup,n.cgroup=n.cgroup,cgroupcolor=cgroupcolor)
z
Group A   Group B   Group C
  mpg cyl disp   hp drat wt qsec   vs am gear carb
Mazda RX4 21 6 160 110 4 3 16 0 1 4 4
Mazda RX4 Wag 21 6 160 110 4 3 17 0 1 4 4
Datsun 710 23 4 108 93 4 2 19 1 1 4 1
Hornet 4 Drive 21 6 258 110 3 3 19 1 0 3 1
Hornet Sportabout 19 8 360 175 3 3 17 0 0 3 2
Valiant 18 6 225 105 3 3 20 1 0 3 1

Add complex column groups

z=ztable(head(iris))
cgroup=c("Total",NA,NA)
cgroup1=c("Group","Species",NA)
cgroup2=c("SEPAL","PETAL","Species")
cgroup=rbind(cgroup,cgroup1,cgroup2)
n.cgroup=matrix(c(5,NA,NA,4,1,NA,2,2,1),byrow=TRUE,nrow=3)
z=addcgroup(z,cgroup=cgroup,n.cgroup=n.cgroup)
z
Total
Group   Species
SEPAL   PETAL   Species
  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

Add complex column groups and row groups with column groups color

cgroupcolor=matrix(c("white","white","white","white","white","platinum","white","white",
                     "white","cyan","platinum","white"),byrow=TRUE,nrow=3)
z=addcgroup(z,cgroup=cgroup,n.cgroup=n.cgroup,cgroupcolor=cgroupcolor)
z=addrgroup(z,rgroup=c("1to4","5to6"),n.rgroup=c(4,2),cspan.rgroup=1)
z=addColColor(z,3,"cyan")
z=addColColor(z,5,"platinum")
z
Total
Group   Species
SEPAL   PETAL   Species
  Sepal.Length Sepal.Width   Petal.Length Petal.Width   Species
1to4
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
5to6
5 5.00 3.60 1.40 0.20 setosa
6 5.40 3.90 1.70 0.40 setosa