Skip to contents

Create a simple bar plot

Usage

plot_bar(
  data,
  formula,
  type = c("percent", "count"),
  fill = "#0032A0",
  orient = c("vertical", "horizontal"),
  dodge = 1,
  title = NULL,
  na_rm = FALSE,
  ...
)

Arguments

data

A data frame.

formula

The variable to tabulate. Should be given in formula notation, ~var.

type

The type of plot to create. Valid options are "percent" (the default) or "count".

fill

The fill of the plot. Valid options are a character color (for one variable plots) or a variable given in formula notation (~var), used to create a grouped bar plot.

orient

The orientation for the plot (either "vertical", the default, or "horizontal"). As a shortcut, "v" and "h" may be used.

dodge

The number of rows to dodge the axis labels to should they be overlapping.

title

An override for the title of the plot. A sensible default is provided.

na_rm

Should missing values be removed? Defaults to FALSE.

...

Extra title arguments passed on to ggformula::gf_labs() (which feeds to ggplot2::ggplot()).

Value

A ggplot object. In an interactive session, results are viewable immediately.

Examples

plot_bar(mtcars, ~cyl)
#> When no value for `type` is provided, a 'percent' plot is created.
#> This message is displayed once every 8 hours.

plot_bar(mtcars, ~cyl, type = "count")

plot_bar(mtcars, ~cyl, type = "percent", fill = "yellowgreen")

plot_bar(mtcars, ~cyl, orient = "horizontal")

plot_bar(dplyr::starwars, ~hair_color, dodge = 2)


plot_bar(mtcars, ~cyl, fill = ~gear)

plot_bar(mtcars, ~cyl, type = "count", fill = ~gear)