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
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
qbarplot(
  df,
  x,
  fill,
  wrap = NULL,
  raw = FALSE,
  text = TRUE,
  vjust_strip,
  cex = 3,
  cex_axes = 18,
  percent = TRUE,
  dodge = FALSE,
  xlab,
  ylab,
  wrap100 = FALSE,
  tibble = TRUE,
  display0 = FALSE,
  angle = 0,
  hjust = 0.5,
  vjust = 0.5,
  color,
  rev.palette = 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.

vjust_strip

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).

cex

size of the text displaying the absolute frequencies.

cex_axes

size of the axis labels, 18pt by default. To get the ggplot2 default values and customize with your own ggplot layers, just enter a non-numeric value.

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.

display0

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

angle

Use this to rotate the x tick labels.

hjust

horizontal justification of x tick labels.

vjust

vertical justification of x tick labels.

color

color scheme to be used for the plot. Can be an ordinary palette (see ?palettes) or an RColorBrewer palette, see ?scale_fill_brewer for available palettes. Set to FALSE to get ggplot's default color scheme.

rev.palette

if a color palette is used, setting rev.palette to TRUE will reverse it.

...

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 Oct. 7, 2020, 4:16 p.m.