| barchart2 {heR.Misc} | R Documentation |
A replacement for the original bar chart function in the lattice package, dealing with grouped data by plotting stacked or side-by-side bars just like the bar plotting function in base R graphics
barchart2(formula, data = parent.frame(), panel = "panel.superbar",
prepanel = "prepanel.superbar", strip = TRUE, box.ratio = 10,
groups = NULL, beside = FALSE, horizontal = NULL, subset = TRUE,
subscripts = !is.null(groups), ...)
formula |
a formula specification containing the y, x, and conditioning variables; the x and y variables can be interpreted either as a factor (independent variable) or the dependent variable depending on the value of horizontal as described below |
data |
the dataframe containing variables in the formula |
panel |
a function for plotting the conditioned data in each panel, defatuls to panel.superbar |
prepanel |
a function for determining the horizontal and vertical limits of each panel, defaulting to prepanel.superplot |
strip |
logical, whether to draw strips or not |
box.ratio |
the ratio of bar width to the space between bars, defaulting to 10 |
group.ratio |
the ratio of group width (i.e., the width of all bars making up each group) to the space between groups, defaulting to 1; only used for side-by-side bar groups |
groups |
specification of the grouping variables |
beside |
logical, whether to draw bars side-by-side or stacked |
horizontal |
logical, whether to draw bars horizontally or vertically; make sure to specify an appropriate factor as the independent variable, which will be the 'y' variable for horizontal bars and the 'x' variable for vertical bars |
subset |
logical vector containing the index of rows in data that will be plotted |
subscripts |
subscripts for groups |
... |
additional arguments |
See the xyplot documentation for more complete information on
how to use the lattice graphics functions, including descriptions of most
of the arguments listed above.
The only visible difference in usage from the original lattice barchart function is the `beside' argument and the `group.ratio' argument. Internally, new panel and prepanel functions (panel.superbar and prepanel.superbar) are used to plot the bars in each panel and specify the default x and y limits for each panel.
A trellis lattice graphics object
Neil Klepeis, adapted from the barchart function included in the lattics package and barplot in the base R package.
barchart is the original lattice function for plotting (univariate) barcharts in each panel.
panel.superbar is the panel function used to plot superimposed (group) data as stacked or side-by-side bars.
prepanel.superbar is the prepanel function used to specify default horizontal and vertical limits for each panel for either stacked or side-by-side bars.
barplot is the original non-lattice function in base R used to plot univariate or grouped data as a bar chart.
test <- data.frame(x=rep(1:4,2),y=1:8, f=gl(2,4)) # vertical side-by-side bar chart barchart2(y ~ x, groups=f, data=test, beside=TRUE, horizontal=FALSE, col=1:2) # horizontal stacked bar chart barchart2(x ~ y, groups=f, data=test, beside=FALSE, horizontal=TRUE, col=1:2)