Learn R Programming

vcd (version 1.4-4)

cotabplot: Coplot for Contingency Tables

Description

cotabplot is a generic function for creating trellis-like coplots (conditional plots) for contingency tables.

Usage

cotabplot(x, …)
# S3 method for default
cotabplot(x, cond = NULL,
  panel = cotab_mosaic, panel_args = list(),
  margins = rep(1, 4), layout = NULL,
  text_gp = gpar(fontsize = 12), rect_gp = gpar(fill = grey(0.9)),
  pop = TRUE, newpage = TRUE, return_grob = FALSE,
  …)
# S3 method for formula
cotabplot(formula, data = NULL, …)

Arguments

x

an object. The default method can deal with contingency tables in array form.

cond

margin index(es) or corresponding name(s) of the conditioning variables.

panel

panel function applied for each conditioned plot, see details.

panel_args

list of arguments passed to panel if this is a panel-generating function inheriting from class "grapcon_generator".

margins

either an object of class "unit" of length 4, or a numeric vector of length 4. The elements are recycled as needed. giving the margins around the whole plot.

layout

integer vector (of length two), giving the number of rows and columns for the panel.

text_gp

object of class "gpar" used for the text in the panel titles.

rect_gp

object of class "gpar" used for the rectangles with the panel titles.

pop

logical indicating whether the generated viewport tree should be removed at the end of the drawing or not.

newpage

logical controlling whether a new grid page should be created.

return_grob

logical. Should a snapshot of the display be returned as a grid grob?

further arguments passed to the panel-generating function.

formula

a formula specifying the variables used to create a contingency table from data. It has to be of type ~ x + y | z where z is/are the conditioning variable(s) used.

data

either a data frame, or an object of class "table" or "ftable".

Details

cotabplot is a generic function designed to create coplots or conditional plots (see Cleveland, 1993, and Becker, Cleveland, Shyu, 1996) similar to coplot but for contingency tables.

cotabplot takes on computing the conditioning information and setting up the trellis display, and then relies on a panel function to create plots from the full table and the conditioning information. A simple example would be a contingency table tab with margin names "x", "y" and "z". To produce this plot either the default interface can be used or the formula interface via

cotabplot(tab, "z") cotabplot(~ x + y | z, data = tab)

The panel function needs to be of the form

panel(x, condlevels)

where x is the full table (tab in the example above) and condlevels is a named vector with the levels (e.g., c(z = "z1") in the example above).

Alternatively, panel can also be a panel-generating function of class "grapcon_generator" which creates a function with the interface described above. The panel-generating function is called with the interface

panel(x, condvars, …)

where again x is the full table, condvars is now only a vector with the names of the conditioning variables (and not their levels, e.g., "z" in the example above). Further arguments can be passed to the panel-generating function via which also includes the arguments set in panel_args.

Suitable panel-generating functions for mosaic, association and sieve plots can be found at cotab_mosaic.

A description of the underlying ideas is given in Zeileis, Meyer, Hornik (2005).

References

Becker, R.A., Cleveland, W.S., Shyu, M.-J. (1996), The visual design and control of trellis display. Journal of Computational and Graphical Statistics, 5, 123--155.

Cleveland, W.S. (1993), Visualizing Data, Summit, New Jersey: Hobart Press.

Meyer, D., Zeileis, A., and Hornik, K. (2006), The strucplot framework: Visualizing multi-way contingency tables with vcd. Journal of Statistical Software, 17(3), 1-48. URL http://www.jstatsoft.org/v17/i03/ and available as vignette("strucplot").

Zeileis, A., Meyer, D., Hornik K. (2007), Residual-based shadings for visualizing (conditional) independence, Journal of Computational and Graphical Statistics, 16, 507--525.

See Also

cotab_mosaic, cotab_coindep, co_table, coindep_test

Examples

Run this code
# NOT RUN {
data("UCBAdmissions")

cotabplot(~ Admit + Gender | Dept, data = UCBAdmissions)
cotabplot(~ Admit + Gender | Dept, data = UCBAdmissions, panel = cotab_assoc)

ucb <- cotab_coindep(UCBAdmissions, condvars = "Dept", type = "assoc",
                     n = 5000, margins = c(3, 1, 1, 3))
cotabplot(~ Admit + Gender | Dept, data = UCBAdmissions, panel = ucb)
# }

Run the code above in your browser using DataLab