sjp.stackfrq {sjPlot}

This document shows examples for using the sjp.stackfrq function of the sjPlot package.

Ressources:

(back to table of content)

Data initialization

Please refer to this document.

# load package
library(sjPlot)
library(sjmisc)
# load sample data set.
data(efc)
# set theme
sjp.setTheme(axis.title.size = .8,
             axis.textsize = .8,
             geom.label.size = 3,
             geom.label.color = "black",
             legend.size = .8, 
             legend.title.size = .8)

Customizing plot appearance

Please refer to this document

Preparing a stacked bar plot

The sjp.stackfrq function allows to plot multiple variables that have the same categories. Typically you find this when you have scale 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.

# recveive first item of COPE-index scale
start <- which(colnames(efc) == "c82cop1")
# recveive first 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 bar plot

For sjp.stackfrq you need a data frame of variables as parameter. These variables must have the same category length (in this example: “never”, “sometimes”, “often” and “always”). The plot then prints the stacked frequencies of all variables in a single plot.

sjp.stackfrq(efc[, start:end], expand.grid = TRUE)

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).

sjp.stackfrq(efc[, start:end],
             show.values = FALSE,
             sort.frq = "last.desc")