Learn R Programming

sjPlot (version 2.1.0)

sjp.likert: Plot likert scales as centered stacked bars

Description

Plot likert scales as centered stacked bars.

Usage

sjp.likert(items, title = NULL, legend.title = NULL, legend.labels = NULL, axis.titles = NULL, axis.labels = NULL, catcount = NULL, cat.neutral = NULL, sort.frq = NULL, weight.by = NULL, title.wtd.suffix = NULL, wrap.title = 50, wrap.labels = 30, wrap.legend.title = 30, wrap.legend.labels = 28, geom.size = 0.6, geom.colors = "BrBG", cat.neutral.color = "grey70", intercept.line.color = "grey50", reverse.colors = FALSE, values = "show", show.n = TRUE, show.legend = TRUE, show.prc.sign = FALSE, grid.range = 1, grid.breaks = 0.2, expand.grid = TRUE, digits = 1, 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.
catcount
optional, amount of categories of items (e.g. "strongly disagree", "disagree", "agree" and "strongly agree" would be catcount = 4). Note that this argument only applies to "valid" answers, i.e. if you have an additional neutral category (see cat.neutral) like "don't know", this won't count for catcount (e.g. "strongly disagree", "disagree", "agree", "strongly agree" and neutral category "don't know" would still mean that catcount = 4). See 'Note'.
cat.neutral
if there's a neutral category (like "don't know" etc.), specify the index number (value) for this category. Else, set cat.neutral = NULL (default). The proportions of neutral category answers are plotted as grey bars on the left side of the figure.
sort.frq
indicates whether the items of items should be ordered by total sum of positive or negative answers.
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.
title.wtd.suffix
suffix (as string) for the title, if weight.by is specified, e.g. title.wtd.suffix=" (weighted)". Default is NULL, so title will not have a suffix when cases are weighted.
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.
cat.neutral.color
color of the neutral category, if plotted (see cat.neutral).
intercept.line.color
color of the vertical intercept line that divides positive and negative values.
reverse.colors
logical, if TRUE, the color scale from geom.colors will be reversed, so positive and negative values switch colors.
values
determines style and position of percentage value labels on the bars:
show.n
logical, if TRUE, adds total number of cases for each group or category to the labels.
show.legend
logical, if TRUE, and depending on plot type and function, a legend is added to the plot.
show.prc.sign
logical, if TRUE, %-signs for value labels are shown.
grid.range
numeric, limits of the x-axis-range, as proportion of 100. Default is 1, so the x-scale ranges from zero to 100% on both sides from the center. You can use values beyond 1 (100%) in case bar labels are not printed because they exceed the axis range. E.g. grid.range = 1.4 will set the axis from -140 to +140%, however, only (valid) axis labels from -100 to +100% are printed. Neutral categories are adjusted to the most left limit.
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.
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.neg for the negative values, df.pos for the positive values and df.neutral for the neutral category values).

See Also

sjPlot manual: sjp.likert

Examples

Run this code
# prepare data for dichotomous likert scale, 5 items
likert_2 <- data.frame(
  as.factor(sample(1:2, 500, replace = TRUE, prob = c(0.3,0.7))),
  as.factor(sample(1:2, 500, replace = TRUE, prob = c(0.6,0.4))),
  as.factor(sample(1:2, 500, replace = TRUE, prob = c(0.25,0.75))),
  as.factor(sample(1:2, 500, replace = TRUE, prob = c(0.9,0.1))),
  as.factor(sample(1:2, 500, replace = TRUE, prob = c(0.35,0.65))))
  
# create labels
levels_2 <- c("Agree", "Disagree")
                       
# prepare data for 4-category likert scale, with neutral category 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("Strongly agree", "Agree", "Disagree", 
              "Strongly Disagree", "Don't know")

# prepare data for 6-category likert scale, 5 items
likert_6 <- data.frame()

Q1 <- as.factor(sample(1:6, 500, replace = TRUE, prob = c(0.2,0.1,0.1,0.3,0.2,0.1)))
Q2 <- as.factor(sample(1:6, 500, replace = TRUE, prob = c(0.15,0.15,0.3,0.1,0.1,0.2)))
Q3 <- as.factor(sample(1:6, 500, replace = TRUE, prob = c(0.2,0.25,0.05,0.2,0.2,0.2)))
Q4 <- as.factor(sample(1:6, 500, replace = TRUE, prob = c(0.2,0.1,0.1,0.4,0.1,0.1)))
Q5 <- as.factor(sample(1:6, 500, replace = TRUE, prob = c(0.1,0.4,0.1,0.3,0.05,0.15)))

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

# create labels
levels_6 <- c("Very strongly agree", "Strongly agree", "Agree",
              "Disagree", "Strongly disagree", "Very strongly disagree")

# create item labels
items <- c("Q1", "Q2", "Q3", "Q4", "Q5")

# plot dichotomous likert scale, ordered by "negative" values
sjp.likert(likert_2, geom.colors = c("green", "red"), legend.labels = levels_2, 
           axis.labels = items, sort.frq = "neg.desc")

# plot 4-category-likert-scale, no order
sjp.likert(likert_4, cat.neutral = 5, legend.labels = levels_4, 
           axis.labels = items, grid.range = 1.2, expand.grid = FALSE,
           values = "sum.outside", show.prc.sign = TRUE)

# plot 6-category-likert-scale, ordered by positive values,
# in brown color scale
sjp.likert(likert_6,  legend.labels = levels_6, axis.labels = items, 
           sort.frq = "pos.asc", digits = 0, show.prc.sign = TRUE,
           values = "sum.inside")

Run the code above in your browser using DataLab