qbarplot: Quick barplots

Description Usage Arguments Value Examples

View source: R/qbarplot.R

Description

Convenience function for generating ggplot2 barplots from raw datasets.

Usage

1
2
3
qbarplot(df, x, fill, wrap = NULL, raw = FALSE, text = TRUE,
  cex = 3, percent = TRUE, dodge = FALSE, xlab, ylab,
  wrap100 = FALSE, tibble = TRUE, vjust, display0 = FALSE, ...)

Arguments

df

a data frame with raw data

x

variable to be displayed on the x axis. Must be a column name of df. Can be unquoted.

fill

variable that defines the fill of the bars. Must be a column name of df. Can be unquoted.

wrap

variable along which the data should be split in different panels. See ?ggplot2::facet_wrap and ?ggplot2::facet_grid for more information. Default is NULL, i.e. no wrap.

raw

if TRUE, the function returns the raw data, i.e. a data frame with cross-tabulated absolute and relative frequencies.

text

if TRUE (the default), absolute frequencies are displayed on the bars using geom_text.

cex

size of the text displaying the absolute frequencies.

percent

If TRUE (the default), the y axis displays proportions in percent format. Requires package "scales". If FALSE (or if "scales" is not installed), it is displayed as a fraction between 0 and 1.

dodge

If FALSE (the default), a stacked barplot if produced. If TRUE, qbarplot() returns a dodged barplot, i.e. the bars are displayed side-by-side.

xlab

x axis label. By default, identical to x.

ylab

y axis label. By default, "Relative Frequency".

wrap100

If wrap != NULL and wrap100 = TRUE, percentages are calculated separately for each category. In other words, if wrap100 = TRUE, the bars sum up to 100 percent in each panel. If wrap100 = FALSE (the default), the percentages sum up to 100 across panels. For illustration, compare e.g. qbarplot(ggplot2::diamonds, color, clarity, cut, wrap100 = FALSE) vs. qbarplot(ggplot2::diamonds, color, clarity, cut, wrap100 = TRUE).

tibble

If raw = TRUE and tibble = TRUE (the default), the raw data are returned as a tibble. Only works if the "tibble" package is installed. See ?tibble::tibble for more information.

vjust

vertical adjustment of the text, if text = TRUE. Behaves differently for stacked and dodged barplots. Under the hood, vjust is part of the position_stacked() call in stacked barplots (default: .5, i.e. in the middle of each portion of the bar) and part of the geom_text() call in dodged barplots (-.5, i.e. slightly above each bar).

display0

If text = TRUE, display0 = TRUE (the default) prevents 0 values (if there are any) from being shown on the text display.

...

Further arguments to be passed on to ggplot().

Value

a ggplot2 barplot or, if raw = TRUE, a data frame with the cross-tabulated raw and relative frequencies.

Examples

1
2
qbarplot(ggplot2::diamonds, color, cut)
qbarplot(ggplot2::diamonds, color, clarity, cut)

hartmast/wizard documentation built on Aug. 19, 2018, 3:45 a.m.