sjt.xtab {sjPlot}

This document shows examples for using the sjt.xtab function of the sjPlot package.

Ressources:

(back to table of content)

Basics of the sjt-functions

Please refer to this document

Data initialization

Please refer to this document.

# load package
library(sjPlot)
library(sjmisc)
# load sample data set.
data(efc)

Printing contingency tables

The basic function call just needs two variables (row and column) as arguments. By default, the value labels are automatically set (see this document). A summary row with Chi-square, df, Phi or Cramer’s V and p-value is always shown by default.

sjt.xtab(efc$e42dep, efc$e16sex)
elder’s dependency elder’s gender Total
male female
independent 23 43 66
slightly dependent 70 154 224
moderately dependent 109 197 306
severely dependent 93 211 304
Total 295 605 900
Χ2=2.147 · df=3 · Φc=.049 · p=.542

Adding percentage values

sjt.xtab(efc$e42dep, efc$e16sex, show.exp = TRUE, show.row.prc = TRUE, 
         show.cell.prc = TRUE, show.col.prc = TRUE)
elder’s dependency elder’s gender Total
male female
independent 23
22
34.8 %
7.8 %
2.6 %
43
44
65.2 %
7.1 %
4.8 %
66
66
100 %
7.4 %
7.4 %
slightly dependent 70
73
31.2 %
23.7 %
7.8 %
154
151
68.8 %
25.5 %
17.1 %
224
224
100 %
24.9 %
24.9 %
moderately dependent 109
100
35.6 %
36.9 %
12.1 %
197
206
64.4 %
32.6 %
21.9 %
306
306
100 %
34 %
34 %
severely dependent 93
100
30.6 %
31.5 %
10.3 %
211
204
69.4 %
34.9 %
23.4 %
304
304
100 %
33.7 %
33.7 %
Total 295
295
32.8 %
100 %
32.8 %
605
605
67.2 %
100 %
67.2 %
900
900
100 %
100 %
100 %
Χ2=2.147 · df=3 · Φc=.049 · p=.542

Shortening labels

With the wrap.labels argument you can specify at which length a new line in labels is inserted.

sjt.xtab(efc$e42dep, efc$c172code, wrap.labels = 10)
elder’s
dependency
carer’s
level of
education
Total
low level
of
education
intermediate
level of
education
high level
of
education
independent 21 76 10 107
slightly
dependent
72 238 68 378
moderately
dependent
106 289 103 498
severely
dependent
118 296 84 498
Total 317 899 265 1481
Χ2=8.658 · df=6 · Φc=.072 · p=.194

Adding a legend

A “legend” below the table can be added with show.legend = TRUE.

sjt.xtab(efc$e42dep, efc$c172code, show.row.prc = TRUE, 
         show.cell.prc = TRUE, show.legend = TRUE)
elder’s dependency carer’s level of
education
Total
low level of
education
intermediate level
of education
high level of
education
independent 21
19.6 %
1.4 %
76
71 %
5.1 %
10
9.3 %
0.7 %
107
100 %
7.2 %
slightly dependent 72
19 %
4.9 %
238
63 %
16.1 %
68
18 %
4.6 %
378
100 %
25.6 %
moderately dependent 106
21.3 %
7.2 %
289
58 %
19.5 %
103
20.7 %
7 %
498
100 %
33.7 %
severely dependent 118
23.7 %
8 %
296
59.4 %
20 %
84
16.9 %
5.7 %
498
100 %
33.7 %
Total 317
21.5 %
21.5 %
899
60.7 %
60.7 %
265
18 %
18 %
1481
100 %
100 %
Χ2=8.658 · df=6 · Φc=.072 · p=.194

observed values
% within elder’s dependency
% of total

Adding NA’s

If you want to get information about missing values, add show.na = TRUE as argument.

sjt.xtab(efc$e42dep, efc$e16sex, show.exp = TRUE, show.row.prc = TRUE, 
         show.cell.prc = TRUE, show.col.prc = TRUE, show.na = TRUE)
elder’s dependency elder’s gender Total
male female NA
independent 23
22
34.8 %
7.8 %
2.5 %
43
44
65.2 %
7.1 %
4.7 %
0
1
0 %
0 %
0 %
66
66
100 %
7.2 %
7.2 %
slightly dependent 70
73
31.1 %
23.6 %
7.7 %
154
150
68.4 %
25.5 %
17 %
1
2
0.4 %
14.3 %
0.1 %
225
225
100 %
24.8 %
24.8 %
moderately dependent 109
100
35.6 %
36.8 %
12 %
197
204
64.4 %
32.6 %
21.7 %
0
2
0 %
0 %
0 %
306
306
100 %
33.7 %
33.7 %
severely dependent 93
99
30.6 %
31.4 %
10.2 %
211
203
69.4 %
34.9 %
23.2 %
0
2
0 %
0 %
0 %
304
304
100 %
33.4 %
33.4 %
NA 1
2
14.3 %
0.3 %
0.1 %
0
5
0 %
0 %
0 %
6
0
85.7 %
85.7 %
0.7 %
7
7
100 %
0.8 %
0.8 %
Total 296
296
32.5 %
100 %
32.5 %
605
605
66.6 %
100 %
66.6 %
7
7
0.8 %
100 %
0.8 %
908
908
100 %
100 %
100 %
Fisher’s p=.552 · df=3 · Φc=.049

Note that if we have table cells with less than 5 observations, the Fisher’s exact test is computed.

Changing colors

Beside customizing the table output with CSS there are some convenient arguments to easily change the colors of expected and percentage values. Use tdcol.n, tdcol.expected, tdcol.cell, tdcol.col and tdcol.row as arguments and assign a (CSS) valid color code. Furthermore, you can highlight the total column and row with emph.total and change the color with emph.color.

sjt.xtab(efc$e42dep, efc$e16sex, show.exp = TRUE, show.row.prc = TRUE, 
         show.cell.prc = TRUE, show.col.prc = TRUE, tdcol.n = "blue", 
         tdcol.expected = "red", tdcol.row = "#336699", tdcol.cell = "#996633", 
         tdcol.col = "#33cc66", emph.total = TRUE)
elder’s dependency elder’s gender Total
male female
independent 23
22
34.8 %
7.8 %
2.6 %
43
44
65.2 %
7.1 %
4.8 %
66
66
100 %
7.4 %
7.4 %
slightly dependent 70
73
31.2 %
23.7 %
7.8 %
154
151
68.8 %
25.5 %
17.1 %
224
224
100 %
24.9 %
24.9 %
moderately dependent 109
100
35.6 %
36.9 %
12.1 %
197
206
64.4 %
32.6 %
21.9 %
306
306
100 %
34 %
34 %
severely dependent 93
100
30.6 %
31.5 %
10.3 %
211
204
69.4 %
34.9 %
23.4 %
304
304
100 %
33.7 %
33.7 %
Total 295
295
32.8 %
100 %
32.8 %
605
605
67.2 %
100 %
67.2 %
900
900
100 %
100 %
100 %
Χ2=2.147 · df=3 · Φc=.049 · p=.542