Learn R Programming

sjPlot (version 1.7)

sjp.xtab: Plot contingency tables

Description

Plot proportional crosstables (contingency tables) of two variables as ggplot diagram.

Usage

sjp.xtab(y, x, title = "", legendTitle = NULL, weightBy = NULL,
  weightByTitleString = NULL, type = "bars", tableIndex = "col",
  reverseOrder = FALSE, axisLimits.y = NULL, axisLabels.x = NULL,
  legendLabels = NULL, labelPos = "outside", stringTotal = "Total",
  breakTitleAt = 50, breakLabelsAt = 15, breakLegendTitleAt = 20,
  breakLegendLabelsAt = 20, gridBreaksAt = 0.2, geom.size = 0.7,
  geom.spacing = 0.1, geom.colors = "Paired", barPosition = "dodge",
  lineDotSize = 3, smoothLines = FALSE, expand.grid = FALSE,
  showValueLabels = TRUE, jitterValueLabels = FALSE,
  showCountValues = TRUE, showPercentageValues = TRUE,
  showCategoryLabels = TRUE, showTableSummary = TRUE,
  tableSummaryPos = "r", showTotalColumn = TRUE, hideLegend = FALSE,
  axisTitle.x = NULL, axisTitle.y = NULL, coord.flip = FALSE,
  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 (mydf).

See Also

  • http://www.strengejacke.de/sjPlot/sjp.xtab{sjPlot manual: sjp.xtab}
  • sjt.xtab

Examples

Run this code
# create 4-category-items
x <- sample(1:4, 100, replace=TRUE)
# create 3-category-items
y <- sample(1:3, 100, replace=TRUE)

# plot "cross tablulation" of x and y
sjp.xtab(y,x)

# plot "cross tablulation" of x and y, including labels
sjp.xtab(y, x,
         axisLabels.x = c("low", "mid", "high"),
         legendLabels = c("Grp 1", "Grp 2", "Grp 3", "Grp 4"))

# plot "cross tablulation" of x and y
# as stacked proportional bars
sjp.xtab(y, x,
         tableIndex = "row",
         barPosition = "stack",
         coord.flip = TRUE)

# example with vertical labels
data(efc)
sjp.setTheme(geom.label.angle = 90)
# hjust-aes needs adjustment for this
library(ggplot2)
update_geom_defaults('text', list(hjust = -0.1))
sjp.xtab(efc$e42dep,
         efc$e16sex,
         showTableSummary = FALSE,
         labelPos = "center")

# grouped bars with EUROFAMCARE sample dataset
# dataset was importet from an SPSS-file, using:
# efc <- read_spss("efc.sav", enc="UTF-8")
data(efc)
efc.val <- get_val_labels(efc)
efc.var <- get_var_labels(efc)

sjp.xtab(efc$e42dep,
         efc$e16sex,
         title=efc.var['e42dep'],
         axisLabels.x=efc.val[['e42dep']],
         legendTitle=efc.var['e16sex'],
         legendLabels=efc.val[['e16sex']])

sjp.xtab(efc$e16sex,
         efc$e42dep,
         title=efc.var['e16sex'],
         axisLabels.x=efc.val[['e16sex']],
         legendTitle=efc.var['e42dep'],
         legendLabels=efc.val[['e42dep']])

sjp.xtab(efc$e16sex,
         efc$e42dep,
         title=efc.var['e16sex'],
         axisLabels.x=efc.val[['e16sex']],
         legendTitle=efc.var['e42dep'],
         legendLabels=efc.val[['e42dep']],
         tableIndex="row",
         barPosition="stack",
         coord.flip=TRUE,
         jitterValueLabels=TRUE)

# -------------------------------
# auto-detection of labels
# -------------------------------
efc <- set_var_labels(efc, efc.var)
sjp.xtab(efc$e16sex, efc$e42dep)

Run the code above in your browser using DataLab