Learn R Programming

descr (version 1.0.0)

crosstab: Cross tabulation with mosaic plot

Description

This function is a wrapper for CrossTable, adding a mosaic plot and making it easier to do a weighted cross-tabulation.

Usage

crosstab(x, y, weight = NULL, digits = 3, max.width = NA,
         expected = FALSE, prop.r = FALSE, prop.c = FALSE,
         prop.t = FALSE, prop.chisq = FALSE, chisq = FALSE,
         fisher = FALSE, mcnemar = FALSE, resid = FALSE,
         sresid = FALSE, asresid = FALSE,
         missing.include = FALSE, format = "SPSS",
         dnn = NULL, user.missing.x, user.missing.y,
         cell.layout = TRUE, plot = getOption("descr.plot"),
         xlab = deparse(substitute(x)),
         ylab = deparse(substitute(y)), main = "", ...)

Arguments

x, y
Vectors in a matrix or a dataframe.
weight
An optional vector for a weighted cross tabulation.
digits
max.width
expected
prop.r
prop.c
prop.t
prop.chisq
chisq
fisher
mcnemar
resid
sresid
asresid
missing.include
format
dnn
cell.layout
user.missing.x
An optional character vector with the levels of x that should be treated as missing values.
user.missing.y
An optional character vector with the levels of y that should be treated as missing values.
plot
Logical: if TRUE (default), a mosaic plot is produced. You may put options(descr.plot = FALSE) in your .Rprofile to change the default function behavior.
main
An overall title for the plot (see title).
xlab
A title for the x axis (see title).
ylab
A title for the y axis (see title).
...
Further arguments to be passed to mosaicplot.

Details

crosstab invokes the CrossTable with all boolean options set to FALSE and "SPSS" as the default format option. The returned CrossTable object can be plotted as a mosaicplot.

See Also

CrossTable, plot.CrossTable, xtable.CrossTable.

Examples

Run this code
educ <- sample(c(1, 2), 200, replace = TRUE, prob = c(0.3, 0.7))
educ <- factor(educ, levels = c(1, 2), labels = c("Low", "High"))
income <- sample(c(1, 2, 3), 200, replace = TRUE,
                 prob = c(0.3, 0.4, 0.3))
income <- factor(income, levels = c(1, 2, 3),
                 labels = c("Low", "Middle", "High"))
attr(educ, "label") <- "Education level"
attr(income, "label") <- "Income level"
w <- sample(c(10, 15, 19), 200, replace = TRUE)

crosstab(income, educ, ylab = "Education", xlab = "Income")
ct <- crosstab(income, educ, w, expected = TRUE, plot = FALSE)
ct
plot(ct, inv.y = TRUE)
print(xtable(ct))
print(xtable(ct, decimal.mark = ",", digits = 1,
             multirow = TRUE, hline = TRUE, align = "llrrr"),
      sanitize.text.function = function(x) x,
      include.rownames = FALSE)

Run the code above in your browser using DataLab