Learn R Programming

sjPlot (version 2.1.0)

sjp.stackfrq: Plot stacked proportional bars

Description

Plot items (variables) of a scale as stacked proportional bars. This function is useful when several items with identical scale/categoroies should be plotted to compare the distribution of answers.

Usage

sjp.stackfrq(items, title = NULL, legend.title = NULL, legend.labels = NULL, axis.titles = NULL, axis.labels = NULL, weight.by = NULL, sort.frq = NULL, wrap.title = 50, wrap.labels = 30, wrap.legend.title = 30, wrap.legend.labels = 28, geom.size = 0.5, geom.colors = "Blues", show.values = TRUE, show.n = TRUE, show.prc = TRUE, show.legend = TRUE, grid.breaks = 0.2, expand.grid = FALSE, digits = 1, vjust = "center", coord.flip = TRUE, prnt.plot = TRUE)

Arguments

items
data.frame with each column representing one item.
title
character vector, used as plot title. Depending on plot type and function, will be set automatically. If title = "", no title is printed.
legend.title
character vector, used as title for the plot legend.
legend.labels
character vector with labels for the guide/legend.
axis.titles
character vector of length one or two, defining the title(s) for the x-axis and y-axis.
axis.labels
character vector with labels used as axis labels. Optional argument, since in most cases, axis labels are set automatically.
weight.by
weight factor that will be applied to weight all cases. Must be a vector of same length as the input vector. Default is NULL, so no weights are used.
sort.frq
indicates whether the items should be ordered by by highest count of first or last category of items.
wrap.title
numeric, determines how many chars of the plot title are displayed in one line and when a line break is inserted.
wrap.labels
numeric, determines how many chars of the value, variable or axis labels are displayed in one line and when a line break is inserted.
wrap.legend.title
numeric, determines how many chars of the legend's title are displayed in one line and when a line break is inserted.
wrap.legend.labels
numeric, determines how many chars of the legend labels are displayed in one line and when a line break is inserted.
geom.size
size resp. width of the geoms (bar width, line thickness or point size, depending on plot type and function). Note that bar and bin widths mostly need smaller values than dot sizes.
geom.colors
user defined color for geoms. See 'Details' in sjp.grpfrq.
show.values
logical, whether values should be plotted or not.
show.n
logical, if TRUE, adds total number of cases for each group or category to the labels.
show.prc
If TRUE (default), the percentage values at the x-axis are shown.
show.legend
logical, if TRUE, and depending on plot type and function, a legend is added to the plot.
grid.breaks
numeric; sets the distance between breaks for the axis, i.e. at every grid.breaks'th position a major grid is being printed.
expand.grid
logical, if TRUE, the plot grid is expanded, i.e. there is a small margin between axes and plotting region. Default is FALSE.
digits
numeric, amount of digits after decimal point when rounding estimates and values.
vjust
character vector, indicating the vertical position of value labels. Allowed are same values as for vjust aesthetics from ggplot2: "left", "center", "right", "bottom", "middle", "top" and new options like "inward" and "outward", which align text towards and away from the center of the plot respectively.
coord.flip
logical, if TRUE, the x and y axis are swapped.
prnt.plot
logical, if TRUE (default), plots the results as graph. Use FALSE if you don't want to plot any graphs. In either case, the ggplot-object will be returned as value.

Value

(Insisibily) returns the ggplot-object with the complete plot (plot) as well as the data frame that was used for setting up the ggplot-object (df).

See Also

Examples

Run this code
# random data for 4-category likert scale, 5 items
Q1 <- as.factor(sample(1:4, 500, replace = TRUE, prob = c(0.2, 0.3, 0.1, 0.4)))
Q2 <- as.factor(sample(1:4, 500, replace = TRUE, prob = c(0.5, 0.25, 0.15, 0.1)))
Q3 <- as.factor(sample(1:4, 500, replace = TRUE, prob = c(0.25, 0.1, 0.4, 0.25)))
Q4 <- as.factor(sample(1:4, 500, replace = TRUE, prob = c(0.1, 0.4, 0.4, 0.1)))
Q5 <- as.factor(sample(1:4, 500, replace = TRUE, prob = c(0.35, 0.25, 0.15, 0.25)))

likert_4 <- data.frame(Q1, Q2, Q3, Q4, Q5)

# create labels
levels_4 <- c("Independent", "Slightly dependent", 
              "Dependent", "Severely dependent")

# plot stacked frequencies of 5 (ordered) item-scales
sjp.stackfrq(likert_4, legend.labels = levels_4)

# -------------------------------
# Data from the EUROFAMCARE sample dataset
# -------------------------------
library(sjmisc)
data(efc)
# recveive first item of COPE-index scale
start <- which(colnames(efc) == "c82cop1")
# recveive first item of COPE-index scale
end <- which(colnames(efc) == "c90cop9")
# auto-detection of labels
sjp.stackfrq(efc[, c(start:end)])


Run the code above in your browser using DataLab