This function builds a bar plot given user specifications using ggplot2.

buildBarPlot(df, x, y, fill = "none", facet = "none", title = NULL,
  facetScales = "fixed", scaleLabels = waiver())

Arguments

df

A data frame with plot data

x

Name of x-variable (string)

y

Name of y-variable (string)

fill

Name of fill variable (string)

facet

Name of facet variable (string)

title

Title of plot (string)

facetScales

Scale specification for subplots (fixed, free_y, free_x, free). This value is passed to the facet_rep_wrap function.

scaleLabels

Scale labels for y-variable (e.g., waiver(), scales::percent). This value is passed to the scale_y_continuous function.

Value

A ggplot2 plot object

Details

The plot labels are based on variable names. The to_any_case function is used to convert variable names from the data frame in camel case to sentence case.

The y-variable values are divided by one thousand or one million when appropriate to simplify axis labels. Scaling is performed using the scaleVariable function.

The y-axis labels are repeated for each subplot using the facet_rep_wrap function.

The barAndLinePal function is used to map the fill variable to a fill color.

This function looks for a variable named paste0(y, "Avg") in the data frame. If this variable is available, it is used to create a horizontal line indicating the average value. A variable avgLab should also be present in the data frame with the legend text for the horizontal line.

If the maximum length of x-variable labels are greater than or equal to four, then the labels are rotated 45 degrees.

See also

Other plot functions: buildLinePlot