Learn R Programming

mosaic (version 0.9-0)

bargraph: Create bar graphs from raw data

Description

barchart from the lattice package makes bar graphs from pre-tabulated data. Raw data can be tabulated using xtabs, but the syntax is unusual compared to the other lattice plotting functions. bargraph provides an interface that is consistent with the other lattice functions.

Usage

bargraph(x, data = parent.frame(), groups, horizontal = FALSE, origin = 0,
  ylab = ifelse(horizontal, "", "Frequency"), xlab = ifelse(horizontal,
  "Frequency", ""), subset, ...)

Arguments

x
a formula describing the plot
data
a data frame in which the formula x is evaluated
groups
a variable or expression used for grouping. See barchart.
horizontal
a logical indicating whether bars should be horizontal
...
additional arguments passed to barchart
origin
beginning point for bars. For the default behavior used by barchart set origin to NULL, but 0 is often a better default. If 0 is not good, perhaps you should use a differ
subset
a vector used to subset data. This may be an expression that will be evaluated within data.
ylab
a character vector of length one used for the y-axis label
xlab
a character vector of length one used for the x-axis label

Value

  • a trellis object describing the plot

Details

bargraph(formula, data=data, ...) works by creating a new data frame from xtabs(formula, data=data) and then calling barchart using modified version of the formula and this new data frame as inputs. This has implications on, for example, conditional plots where one desires to condition on some expression that will be evaluated in data. This typically does not work becuase the required variables do not exist in the output of xtabs. One solution is to first add a new variable to data first and then to condition using this new variable. See the examples.

See Also

barchart

Examples

Run this code
data(HELPrct)
bargraph( ~ substance, data=HELPrct)
bargraph( ~ substance, data=HELPrct, horizontal=TRUE)
bargraph( ~ substance | sex, groups=homeless, auto.key=TRUE, data=HELPrct)
bargraph( ~ substance, groups=homeless, auto.key=TRUE, data=HELPrct, subset=sex=="male")
HELPrct2 <- transform( HELPrct, older = age > 40 )
bargraph( ~ substance | older, data = HELPrct2 )

Run the code above in your browser using DataLab