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

View source: R/mk_barplot_resp.R

mk_barplot_respR Documentation

Create a function for making publishable ggplot2 barplots, showing single or cumulative values of a continuous variable by 1 or 2 categorical variables.

Description

mk_barplot_resp 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 the value (or cumulative values) of any continuous variable on the y-axis. The output function can also produce dodged bar charts when supplied a second categorical variable, a fillby variable. The resulting bar chart will have bars ordered ordered by the alphanumerical order of the x levels by default, and labeled with the cumulative y values. If the y values are decimals, user can specify the number of decimals to display on the bar labels. If the y values are between 0 and 1, user can choose to format the y-axis and the bar labels as percent ( the resulting plot will have a clean theme and clear fonts. It'll also use color-blind friendly palettes.

Usage

mk_barplot_resp(df)

Arguments

df

A data frame.

Value

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

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

  • yvar. String, name of a continuous variable for y-axis.

  • fillby. String, name of a different categorical variable for subdividing and coloring the bars. Default = "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.

  • is_y_pct. Logical, if TRUE, format y-axis as Set it to TRUE when the input yvar has values between 0 and 1 that can also be viewed as percents.

  • show_pct. Logical, if TRUE, calculate the relative frequencies of the aggregated y values between each x category, and then display them as percentages on y-axis and also format bar labels as format them as comma. Default is FALSE.

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

  • raw_label_decimals. Integer, number of decimal points shown for the raw number labels. Default = 0.

  • label_size. Integer, size of bar label text. Default = 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_resp.R

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