bcat_plt_bar: Bar plot utility

View source: R/bcat_plt_bar.R

bcat_plt_barR Documentation

Bar plot utility

Description

Create a bar plot using ggplot2 graphics. This function is a wrapper to create commonly used styles of bar plots. Additional layers can be added to this plot as needed. More complicated plots can be creating using individual ggplot2 layers.

Usage

bcat_plt_bar(
  df,
  x = NULL,
  y = NULL,
  fill = NULL,
  facet = NULL,
  stat = c("freq", "identity", "sum", "mean", "median"),
  position = c("stack", "dodge", "fill"),
  x_lab = ggplot2::waiver(),
  y_lab = ggplot2::waiver(),
  title = ggplot2::waiver(),
  subtitle = ggplot2::waiver(),
  caption = ggplot2::waiver(),
  legend_lab = ggplot2::waiver(),
  legend_position = "bottom",
  legend_hide = FALSE,
  x_scale = NULL,
  y_scale = NULL,
  fill_scale = Rbearcat::scale_fill_UC(),
  facet_scale = c("fixed", "free_y", "free_x", "free"),
  nrow = NULL,
  ncol = NULL,
  x_refline = NULL,
  y_refline = NULL,
  coord_flip = FALSE,
  order = FALSE
)

Arguments

df

The data to be displayed

x

Categorical variable to map to the x-axis

y

Variable to map to the y-axis. Only applicable if stat = "identity"

fill

Variable to map to the fill aesthetic

facet

Facetting variable(s). Note: must wrap in vars(), e.g, facet = vars(var1, var2)

stat

Statistic to map to y-axis. Default ("freq") calculates frequencies, "identity" plots the data as-is. "sum", "mean", and "median" calculate and display the respective summary stats Must specify y if using "identity".

position

For grouped data, either stack bars (position = "stack"), place bars side-by-side (position = "dodge"), or stack such that relative proportions within each group sum to 1 (position = "fill").

x_lab

Label for x-axis

y_lab

Label for y-axis

title

Plot title

subtitle

Plot subtitle

caption

Plot caption

legend_lab

Legend title

legend_position

legend position. "bottom" or "right"

legend_hide

Set to TRUE to hide the legend

x_scale

scale_x_ function to apply to x-axis.

y_scale

scale_y_ function to apply to y-axis.

fill_scale

scale_fill_ function to apply to colors.

facet_scale

Shoud facet scales be fixed ("fixed", the default), free ("free"), or free in one dimension ("free_x", "free_y")?

nrow

Number of facet rows

ncol

Number of facet columns

x_refline

Vector of x-values at which to draw vertical reference lines

y_refline

Vector of y-values at which to draw horizontal reference lines

coord_flip

Set to TRUE to flip x and y-axis. Useful if levels of x have long names.

order

Set to TRUE to arrange bars by frequency (if stat = "freq"), values of y (if stat = "identity"), or by other provided stat. Use if levels of x do not have a natural ordering.

Value

A ggplot2 plot object.

Author(s)

Saannidhya Rawat

See Also

Other plots: bcat_plt_area(), bcat_plt_box(), bcat_plt_coef(), bcat_plt_diag(), bcat_plt_hist(), bcat_plt_line(), bcat_plt_point(), bcat_plt_ts()

Examples

library(ggplot2)
library(dplyr)
library(scales)

# basic plot of frequencies
bcat_plt_bar(df = mpg,
             x = toupper(class),
             order = TRUE,
             x_lab = NULL,
             y_lab = NULL,
             title = "Number of Vehicles by Class and Drive Type",
             legend_lab = "Drive Type")

# can plot relative frequencies for each `x` by `fill` using `position = "fill"`
bcat_plt_bar(df = mpg,
             x = toupper(class),
             fill = drv,
             position = "fill",
             x_lab = NULL,
             y_lab = NULL,
             y_scale = scale_y_continuous(labels = percent_format()),
             title = "Percent of Vehicle Class for Each Drive Type",
             legend_lab = "Drive Type")

# use `stat` to compute and plot other statistics of interest
bcat_plt_bar(mpg,
             x = toupper(class),
             y = hwy,
             fill = factor(year),
             stat = "mean",
             order = TRUE,
             position = "dodge",
             y_refline = round(mean(mpg$hwy), 2),
             coord_flip = TRUE,
             x_lab = NULL,
             y_lab = "Highway MPG",
             title = "Average Highway MPG by Vehicle Class",
             legend_lab = NULL)

# use `stat = "identity"` to plot data directly from data set
mpg %>%
  group_by(year, class) %>%
  summarise(hwy = mean(hwy)) %>%
  bcat_plt_bar(x = toupper(class),
               y = hwy,
               fill = factor(year),
               stat = "identity",
               order = TRUE,
               position = "dodge",
               y_refline = round(mean(mpg$hwy), 2),
               coord_flip = TRUE,
               x_lab = NULL,
               y_lab = "Highway MPG",
               title = "Average Highway MPG by Vehicle Class",
               legend_lab = NULL)


Rbearcat documentation built on March 21, 2026, 5:07 p.m.