sjt.stackfrq {sjPlot}

This document shows examples for using the sjt.stackfrq 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)

Preparing a stacked frequency table

The sjt.stackfrq function allows to print multiple variables that have the same categories. Typically you find this when you have scales with multiple items with answers from agreement to disagreement etc. For the following examples, I use selected items from the COPE index that are part of the sample dataset of the sjPlot package.

# receive first item of COPE-index scale
start <- which(colnames(efc) == "c82cop1")
# receive last item of COPE-index scale
end <- which(colnames(efc) == "c90cop9")

We can now access the items of the COPE index with efc[, start:end].

Simple stacked frequency table

For sjt.stackfrq you need a data frame of variables as parameter. These variables must all have the same amount of categories (in this example: “never”, “sometimes”, “often” and “always”).

sjt.stackfrq(efc[, start:end])
  never sometimes often always
do you feel you cope
well as caregiver?
0.33 % 10.77 % 65.59 % 23.31 %
do you find
caregiving too
demanding?
20.62 % 60.64 % 14.41 % 4.32 %
does caregiving
cause difficulties
in your relationship
with your friends?
57.21 % 27.94 % 9.09 % 5.76 %
does caregiving have
negative effect on
your physical
health?
45.55 % 38.53 % 9.47 % 6.46 %
does caregiving
cause difficulties
in your relationship
with your family?
69.40 % 23.39 % 5.54 % 1.66 %
does caregiving
cause financial
difficulties?
79.22 % 14.56 % 4.33 % 1.89 %
do you feel trapped
in your role as
caregiver?
37.33 % 41.56 % 12.56 % 8.56 %
do you feel
supported by
friends/neighbours?
34.74 % 26.30 % 26.75 % 12.21 %
do you feel
caregiving
worthwhile?
8.56 % 23.65 % 33.78 % 34.01 %

Sorting items

The items in the plot appear in the same order as they are stored in the data frame. However, you can change the order with the sort.frq parameter. This parameter lets you select whether the items are ordered according to their first or last category, in ascending or descending order.

The following figure orders the items according to the last category “always” in descending order (from most to less frequent answers given).

sjt.stackfrq(efc[, start:end], sort.frq = "last.desc")
  never sometimes often always
do you feel
caregiving
worthwhile?
8.56 % 23.65 % 33.78 % 34.01 %
do you feel you cope
well as caregiver?
0.33 % 10.77 % 65.59 % 23.31 %
do you feel
supported by
friends/neighbours?
34.74 % 26.30 % 26.75 % 12.21 %
do you feel trapped
in your role as
caregiver?
37.33 % 41.56 % 12.56 % 8.56 %
does caregiving have
negative effect on
your physical
health?
45.55 % 38.53 % 9.47 % 6.46 %
does caregiving
cause difficulties
in your relationship
with your friends?
57.21 % 27.94 % 9.09 % 5.76 %
do you find
caregiving too
demanding?
20.62 % 60.64 % 14.41 % 4.32 %
does caregiving
cause financial
difficulties?
79.22 % 14.56 % 4.33 % 1.89 %
does caregiving
cause difficulties
in your relationship
with your family?
69.40 % 23.39 % 5.54 % 1.66 %

Adding counts

You can also add cell and total counts to the table.

sjt.stackfrq(efc[, start:end], show.n = TRUE, show.total = TRUE)
  never sometimes often always N
do you feel you cope
well as caregiver?
3
(0.33 %)
97
(10.77 %)
591
(65.59 %)
210
(23.31 %)
901
do you find
caregiving too
demanding?
186
(20.62 %)
547
(60.64 %)
130
(14.41 %)
39
(4.32 %)
902
does caregiving
cause difficulties
in your relationship
with your friends?
516
(57.21 %)
252
(27.94 %)
82
(9.09 %)
52
(5.76 %)
902
does caregiving have
negative effect on
your physical
health?
409
(45.55 %)
346
(38.53 %)
85
(9.47 %)
58
(6.46 %)
898
does caregiving
cause difficulties
in your relationship
with your family?
626
(69.40 %)
211
(23.39 %)
50
(5.54 %)
15
(1.66 %)
902
does caregiving
cause financial
difficulties?
713
(79.22 %)
131
(14.56 %)
39
(4.33 %)
17
(1.89 %)
900
do you feel trapped
in your role as
caregiver?
336
(37.33 %)
374
(41.56 %)
113
(12.56 %)
77
(8.56 %)
900
do you feel
supported by
friends/neighbours?
313
(34.74 %)
237
(26.30 %)
241
(26.75 %)
110
(12.21 %)
901
do you feel
caregiving
worthwhile?
76
(8.56 %)
210
(23.65 %)
300
(33.78 %)
302
(34.01 %)
888

Alternating row colors

If you have long scales with many items, the altr.row.col parameter helps to get a better overview.

sjt.stackfrq(efc[, start:end], altr.row.col = TRUE)
  never sometimes often always
do you feel you cope
well as caregiver?
0.33 % 10.77 % 65.59 % 23.31 %
do you find
caregiving too
demanding?
20.62 % 60.64 % 14.41 % 4.32 %
does caregiving
cause difficulties
in your relationship
with your friends?
57.21 % 27.94 % 9.09 % 5.76 %
does caregiving have
negative effect on
your physical
health?
45.55 % 38.53 % 9.47 % 6.46 %
does caregiving
cause difficulties
in your relationship
with your family?
69.40 % 23.39 % 5.54 % 1.66 %
does caregiving
cause financial
difficulties?
79.22 % 14.56 % 4.33 % 1.89 %
do you feel trapped
in your role as
caregiver?
37.33 % 41.56 % 12.56 % 8.56 %
do you feel
supported by
friends/neighbours?
34.74 % 26.30 % 26.75 % 12.21 %
do you feel
caregiving
worthwhile?
8.56 % 23.65 % 33.78 % 34.01 %

Adding NA’s and more statistics

NAs can be displayed with show.na = TRUE. Furthermore, you can show information on each item’s skewness and kurstosis with show.skew = TRUE and show.kurtosis = TRUE.

sjt.stackfrq(efc[, start:end], 
             show.na = TRUE, 
             show.skew = TRUE, 
             show.kurtosis = TRUE,
             altr.row.col = TRUE)
  never sometimes often always NA Skew Kurtosis
do you feel you cope
well as caregiver?
0.33 % 10.68 % 65.09 % 23.13 % 0.77 % -0.12 0.25
do you find
caregiving too
demanding?
20.48 % 60.24 % 14.32 % 4.30 % 0.66 % 0.65 0.71
does caregiving
cause difficulties
in your relationship
with your friends?
56.83 % 27.75 % 9.03 % 5.73 % 0.66 % 1.30 0.84
does caregiving have
negative effect on
your physical
health?
45.04 % 38.11 % 9.36 % 6.39 % 1.10 % 1.05 0.47
does caregiving
cause difficulties
in your relationship
with your family?
68.94 % 23.24 % 5.51 % 1.65 % 0.66 % 1.76 2.84
does caregiving
cause financial
difficulties?
78.52 % 14.43 % 4.30 % 1.87 % 0.88 % 2.42 5.71
do you feel trapped
in your role as
caregiver?
37.00 % 41.19 % 12.44 % 8.48 % 0.88 % 0.82 -0.09
do you feel
supported by
friends/neighbours?
34.47 % 26.10 % 26.54 % 12.11 % 0.77 % 0.32 -1.14
do you feel
caregiving
worthwhile?
8.37 % 23.13 % 33.04 % 33.26 % 2.20 % -0.45 -0.84

Change style of Skew and Kurtosis column

The skewness and kurstosis columns have own style sheets, which can be modified to change the style of these columns (see this document for details on CSS parameter):

sjt.stackfrq(efc[, start:end], 
             show.na = TRUE, 
             show.skew = TRUE, 
             show.kurtosis = TRUE,
             altr.row.col = TRUE,
             CSS = list(css.kurtcol = 'font-style: italic; color: #999999;',
                        css.skewcol = 'font-style: italic; color: white; background-color: #99ccff;'))
  never sometimes often always NA Skew Kurtosis
do you feel you cope
well as caregiver?
0.33 % 10.68 % 65.09 % 23.13 % 0.77 % -0.12 0.25
do you find
caregiving too
demanding?
20.48 % 60.24 % 14.32 % 4.30 % 0.66 % 0.65 0.71
does caregiving
cause difficulties
in your relationship
with your friends?
56.83 % 27.75 % 9.03 % 5.73 % 0.66 % 1.30 0.84
does caregiving have
negative effect on
your physical
health?
45.04 % 38.11 % 9.36 % 6.39 % 1.10 % 1.05 0.47
does caregiving
cause difficulties
in your relationship
with your family?
68.94 % 23.24 % 5.51 % 1.65 % 0.66 % 1.76 2.84
does caregiving
cause financial
difficulties?
78.52 % 14.43 % 4.30 % 1.87 % 0.88 % 2.42 5.71
do you feel trapped
in your role as
caregiver?
37.00 % 41.19 % 12.44 % 8.48 % 0.88 % 0.82 -0.09
do you feel
supported by
friends/neighbours?
34.47 % 26.10 % 26.54 % 12.11 % 0.77 % 0.32 -1.14
do you feel
caregiving
worthwhile?
8.37 % 23.13 % 33.04 % 33.26 % 2.20 % -0.45 -0.84