Easily create a bar chart

bar_chart(
  data,
  x,
  y,
  facet = NULL,
  ...,
  bar_color = "auto",
  highlight = NULL,
  sort = TRUE,
  horizontal = TRUE,
  top_n = NULL,
  threshold = NULL,
  other = FALSE,
  limit = NULL
)

column_chart(
  data,
  x,
  y,
  facet = NULL,
  ...,
  bar_color = "auto",
  highlight = NULL,
  sort = NULL,
  horizontal = FALSE,
  top_n = NULL,
  threshold = NULL,
  limit = NULL
)

Arguments

data

Dataset to use for the bar chart

x

character or factor column of data

y

numeric column of data representing the bar length. If missing, the bar length will be proportional to the count of each value in x.

facet

character or factor column of data defining the faceting groups

...

Additional arguments passed to aes()

bar_color

character. The color of the bars

highlight

character. One or more value(s) of x that should be highlighted in the plot

sort

logical. Should the data be sorted before plotting?

horizontal

logical. Should the plot be oriented horizontally?

top_n

numeric. If a value for top_n is provided only the top top_n records will be displayed

threshold

numeric. If a value for threshold is provided only records with y > threshold will be displayed

other

logical. Should all x with y < threshold be summarized in a group called 'other' and be displayed at the bottom of the chart?

limit

Deprecated. use top_n instead.

Value

An object of class ggplot

Details

Both top_n and threshold only work when sort = TRUE. Attempting to use them when sort = FALSE will result in an error. Furthermore, only top_n or threshold can be used at a time. Providing a value for both top_n and threshold will result in an error as well.

column_chart() is a shortcut for bar_chart() with horizontal = FALSE and sort = FALSE if x is numeric.

See also

Examples

data(biomedicalrevenue) revenue2018 <- biomedicalrevenue[biomedicalrevenue$year == 2018, ] revenue_roche <- biomedicalrevenue[biomedicalrevenue$company == "Roche", ] ## By default bar_chart() creates a horizontal and sorted plot bar_chart(revenue2018, company, revenue)
## If the `y` argument is missing the count of each value in `x` is displayed bar_chart(mtcars, cyl)
## Create a vertical, non-sorted bar chart bar_chart(revenue_roche, year, revenue, horizontal = FALSE, sort = FALSE)
## column_chart() is a shortcut for the above column_chart(revenue_roche, year, revenue)
## Limit the number of bars to the top 10 bar_chart(revenue2018, company, revenue, top_n = 10)
## Display only companies with revenue > 40B. bar_chart(revenue2018, company, revenue, threshold = 40)
## Change the bar color bar_chart(revenue2018, company, revenue, bar_color = "purple")
## Highlight a single bar bar_chart(revenue2018, company, revenue, top_n = 10, highlight = "Roche")
## Use facets to show the top 5 companies over the years bar_chart(biomedicalrevenue, company, revenue, facet = year, top_n = 5)