cranvas (version 0.8.5)

qbar: Draw a bar plot

Description

This function creates a bar plot based on a categorical variable to show the counts of all categories. Another categorical variable can be used to further split each bar into sub-categories, which will make the bar plot a representation of a contingency table. The splitting variable can be specified in qdata using either the color argument or the border argument.

Usage

qbar(x, data, weight = NULL, space = 0.1, main = "", horizontal = FALSE, standardize = FALSE, xlim = NULL, ylim = NULL, xlab = NULL, ylab = NULL)

Arguments

x
a variable name (will be coerced to a factor if it is not; NA will also be a level of the factor if the variable has any NA's)
data
a mutaframe created by qdata
weight
weight the bars by the sums of another variable instead of the counts of x
space
the space between bars proportional to the width of bars
main
the main title
horizontal
TRUE to draw a horizontal plot or FALSE (vertical)
standardize
logical: whether to standardize the height of each bar to 1
xlim
a numeric vector of length 2 (like c(x0, x1)) for x-axis limits; it will be calculated from the data limits if not specified (NULL). Note when x0 > x1, the axis direction will be reversed (i.e. from larger values to small values)
ylim
y-axis limits; similar to xlim
xlab
x-axis title
ylab
y-axis title

Value

A bar plot

Details

All the common interactions like brushing and deleting are documented in common_key_press.

In the identify mode (press the key ? to toggle between brush and identify mode), the variable and its identified values are shown as a text label in the plot, along with the counts and proportion of the identified categories. If the bar plot is split by an additional categorical variable, it will also be shown in the label.

A zero-count category is represented by a one-pixel rectangle, which is a useful visual hint to indicate the presence of this category.

The x-axis (or y-axis when horizontal = TRUE) tickmark locations are from 1 to n shifted to the right by 0.5 (i.e. 1.5, 2.5, ...), where n is the number of levels of the factor variable to be plotted.

See Also

Other plots: qboxplot; qdensity; qhist, qspine; qmval; qparallel; qtime

Examples

Run this code
library(cranvas)
## tennis data

qtennis <- qdata(tennis)
qbar(matches, data = qtennis)
qbar(matches, data = qtennis, weight = serve.speed)

## NRC rankings

qnrc <- qdata(nrcstat, color = Control)  # Control: public or private univ
qbar(RegCode, data = qnrc, main = "Number of public and private colleges in each region")

qparallel(vars = 13:10, data = qnrc, main = "Overview of Rankings", glyph = "tick", 
    horizontal = TRUE, boxplot = TRUE)

qbar(RegCode, data = qnrc, horizontal = TRUE)  # horizontal plot
qbar(RegCode, data = qnrc, standardize = TRUE)  # standardize to 1

## use border color to split the bars
qnrc2 <- qdata(nrcstat, color = "white", border = Control)
qbar(RegCode, data = qnrc2)

## happy data
library(productplots)
qhappy <- qdata(happy, color = happy)
qbar(happy, data = qhappy)

cranvas_off()

Run the code above in your browser using DataCamp Workspace