Learn R Programming

sjPlot (version 1.7)

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, legendLabels = NULL, orderBy = NULL, weightBy = NULL,
  weightByTitleString = NULL, hideLegend = FALSE, reverseOrder = TRUE,
  title = NULL, legendTitle = NULL, includeN = TRUE,
  axisLabels.y = NULL, breakTitleAt = 50, breakLabelsAt = 30,
  breakLegendTitleAt = 30, breakLegendLabelsAt = 28, gridBreaksAt = 0.2,
  expand.grid = FALSE, geom.size = 0.5, geom.colors = "Blues",
  axisTitle.x = NULL, axisTitle.y = NULL, showValueLabels = TRUE,
  showPercentageAxis = TRUE, jitterValueLabels = FALSE,
  showItemLabels = TRUE, showSeparatorLine = FALSE,
  separatorLineColor = "grey80", separatorLineSize = 0.3,
  coord.flip = TRUE, printPlot = TRUE)

Arguments

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 sample
# -------------------------------
# prepare data for 4-category likert scale, 5 items
likert_4 <- data.frame(as.factor(sample(1:4, 500, replace=TRUE, prob=c(0.2,0.3,0.1,0.4))),
                       as.factor(sample(1:4, 500, replace=TRUE, prob=c(0.5,0.25,0.15,0.1))),
                       as.factor(sample(1:4, 500, replace=TRUE, prob=c(0.25,0.1,0.4,0.25))),
                       as.factor(sample(1:4, 500, replace=TRUE, prob=c(0.1,0.4,0.4,0.1))),
                       as.factor(sample(1:4, 500, replace=TRUE, prob=c(0.35,0.25,0.15,0.25))))
# create labels
levels_4 <- list(c("Independent", "Slightly dependent", "Dependent", "Severely dependent"))

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

# plot stacked frequencies of 5 (ordered) item-scales
sjp.stackfrq(likert_4, legendLabels=levels_4, axisLabels.y=items)


# -------------------------------
# Data from the EUROFAMCARE sample dataset
# -------------------------------
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")

# retrieve variable and value labels
varlabs <- get_var_labels(efc)
vallabs <- get_val_labels(efc)

# create value labels. We need just one variable of
# the COPE-index scale because they have all the same
# level / categorie / value labels
levels <- vallabs['c82cop1']

# create item labels
items <- list(varlabs[c(start:end)])

sjp.stackfrq(efc[,c(start:end)], legendLabels=levels,
             axisLabels.y=items, jitterValueLabels=TRUE)

# -------------------------------
# auto-detection of labels
# -------------------------------
efc <- set_var_labels(efc, varlabs)
sjp.stackfrq(efc[,c(start:end)])

Run the code above in your browser using DataLab