Learn R Programming

ggplot2 (version 2.0.0)

geom_bar: Bars, rectangles with bases on x-axis

Description

There are two types of bar charts, determined by what is mapped to bar height. By default, geom_bar uses stat="count" which makes the height of the bar proportion to the number of cases in each group (or if the weight aethetic is supplied, the sum of the weights). If you want the heights of the bars to represent values in the data, use stat="identity" and map a variable to the y aesthetic.

stat_count counts the number of cases at each x position. If you want to bin the data in ranges, you should use stat_bin instead.

Usage

geom_bar(mapping = NULL, data = NULL, stat = "count",
  position = "stack", width = NULL, binwidth = NULL, ..., na.rm = FALSE,
  show.legend = NA, inherit.aes = TRUE)

stat_count(mapping = NULL, data = NULL, geom = "bar", position = "stack", width = NULL, ..., na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)

Arguments

mapping
Set of aesthetic mappings created by aes or aes_. If specified and inherit.aes = TRUE (the default), is combined with the default mapping at the top le
data
A data frame. If specified, overrides the default data frame defined at the top level of the plot.
position
Position adjustment, either as a string, or the result of a call to a position adjustment function.
width
Bar width. By default, set to 90% of the resolution of the data.
binwidth
geom_bar no longer has a binwidth argument - if you use it you'll get an warning telling to you use geom_histogram instead.
...
other arguments passed on to layer. There are three types of arguments you can use here:

  • Aesthetics: to set an aesthetic to a fixed value, likecolor = "red"orsize = 3.

na.rm
If FALSE (the default), removes missing values with a warning. If TRUE silently removes missing values.
show.legend
logical. Should this layer be included in the legends? NA, the default, includes if any aesthetics are mapped. FALSE never includes, and TRUE always includes.
inherit.aes
If FALSE, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions that define both data and aesthetics and shouldn't inherit behaviour from the default plot specification, e.g.
geom, stat
Override the default connection between geom_bar and stat_count.

Details

A bar chart maps the height of the bar to a variable, and so the base of the bar must always be shown to produce a valid visual comparison. Naomi Robbins has a nice http://www.b-eye-network.com/view/index.php?cid=2468{article on this topic}. This is why it doesn't make sense to use a log-scaled y axis with a bar chart.

By default, multiple x's occurring in the same place will be stacked atop one another by position_stack. If you want them to be dodged side-to-side, see position_dodge. Finally, position_fill shows relative proportions at each x by stacking the bars and then stretching or squashing to the same height.