mk_barplot_freq: Create a function for making publishable ggplot2 barplots,...

View source: R/mk_barplot_freq.R

mk_barplot_freqR Documentation

Create a function for making publishable ggplot2 barplots, showing frequency counts (or percentages) of a categorical variable or 2 categorical variables.

Description

mk_barplot_freq takes a data frame as input and returns a function for making bar charts with any categorical variable from the data frame on the x-axis and frequency counts (or percents) of its levels on the y-axis. The output function can also produce dodged (for counts) or stacked (for percents) bar charts when supplied a second categorical variable, a fillby variable. The resulting bar chart will have bars ordered by the alphanumerical order of the x levels and labeled with both counts and percents by default. Plus, it'll have a clean theme and clean fonts. It'll also use color-blind friendly palettes.

Usage

mk_barplot_freq(df)

Arguments

df

A data frame.

Value

function(xvar, fillby = "1", xorder = NULL, show_pct = FALSE, pct_label_decimals = 1, label_size = 3, legend_title = fillby, legend_pos = "right", font_size = 14)

  • xvar. String, name of a categorical variable for x-axis.

  • fillby. String, name of a different categorical variable for subdividing and coloring the bars. Default is "1", meaning no such variable is supplied.

  • xorder. String, Possible values: NULL (default), "alphanumeric", "ascend" or "descend". It specifies how categories are ordered on the x-axis. When NULL, the categories are shown in their order in the data.

  • show_pct. logical, if TRUE, show percent on y-axis; otherwise, show count. Default is FALSE.

  • pct_label_decimals. Integer, number of decimal points shown for the percent labels. Default = 1.

  • label_size. Integer, size of bar label text. Default is 3. Hide bar labels when its value is 0.

  • legend_title. String, legend title. Default is the name of the fillby variable.

  • legend_pos. String, legend position. Default = "right".

  • font_size. Overall font size. Default = 14. The font size of the axes and legend text is a fraction of this value.

See Also

scale_axis for adding different scales to the y-axis.

Examples

inst/examples/ex-mk_barplot_freq.R

gmlang/ezplot documentation built on Sept. 18, 2022, 6:33 a.m.