geom_bar

Bars, rectangles with bases on y-axis

Details

The bar geom is used to produce 1d area plots: bar charts for categorical x, and histograms for continuous y. stat_bin explains the details of these summaries in more detail. In particular, you can use the weight aesthetic to create weighted histograms and barcharts where the height of the bar no longer represent a count of observations, but a sum over some other variable. See the examples for a practical example.

By default, multiple x's occuring in the same place will be stacked a top one another by position_stack. If you want them to be dodged from side-to-side, check out position by 'dodging' overlaps to the side'>position_dodge. Finally, position_fill shows relative propotions at each x by stacking the bars and then stretch or squashing them all to the same height

Bar charts are sometimes used not as a distributional summary, but as instead of a dotplot. Generally, it's preferable to use a dotplot (see geom_point) as it has a better data-ink ratio, and bars are not interpretable as sums. However, if you do want to create this type of plot, you can set y to the value you have calculated. See the example.

A bar chart maps the height of the bar to a variable, and so the base of the bar must always been shown to produce a valid visual comparison. Naomi Robbins has a nice article on this topic. This is the reason it doesn't make sense to use a log-scaled y axis.

See layer and qplot for more information on creating a complete plot from multiple components.

Aesthetics

The following aesthetics can be used with geom_bar. They are listed along with their default value. All geoms and scales can also use the group aesthetic. Read how this important aesthetic works in scale_group. Typically, you will associate an aesthetic with a variable in your data set. To do this, you use the aes function: geom_bar(aes(x = var)). Scales control the details of the mapping between data and aesthetic properties; after each aesthetic are listed scales that can be used with that aesthetic. The scale documentation will also provide references to help you interpret the default values.

Instead of mapping an aesthetic to a variable in your dataset, you can also set it to a fixed value. See the parameters section for details.

Parameters

When an aesthetic is used an a parameter, like geom_bar(colour = 3), it will override mappings from data.

Default statistic

stat_bin. Override with the stat argument: geom_bar(stat="identity")

Default position

position_stack. Override with the position argument: geom_bar(position="jitter").

See also

Examples


> c <- ggplot(mtcars, aes(x=factor(cyl)))
> 
> c + geom_bar()


> c + geom_bar() + coord_flip()


> c + geom_bar(fill="white", colour="darkgreen")


> 
> # Make a coxcomb
> c + geom_bar() + scale_y_sqrt()


> c + geom_bar(width=1, colour="black") + coord_polar()


> 
> # Use qplot
> cyl <- factor(mtcars$cyl)
> qplot(cyl, geom="bar")


> qplot(cyl, geom="bar", fill=cyl)


> qplot(cyl, geom="bar", fill=cyl) + coord_polar()


> qplot(cyl, geom="blank", fill=cyl) + geom_bar(width=1) + coord_polar()


> qplot(cyl, geom="bar", fill=cyl) + coord_polar(theta="y")


> 
> # Make a stacked bar chart
> c <- ggplot(mtcars, aes(x=1,fill=factor(cyl))) + geom_bar()
> c + coord_polar()


> c + coord_polar(theta="y")


> 
> # Dodged bar charts
> ggplot(diamonds, aes(x=price, fill=cut)) + geom_bar(position="dodge")


> ggplot(diamonds, aes(x=clarity, fill=cut)) + geom_bar(position="dodge")


> # compare with
> ggplot(diamonds, aes(x=cut, fill=cut)) + geom_bar() + facet_grid(. ~ clarity)


> 
> # Often we don't want the height of the bar to represent the
> # count of observations, but the sum of some other variable.
> # For example, the following plot shows the number of movies
> # in each rating.
> qplot(rating, data=movies, geom="bar")


> # If, however, we want to see the number of votes cast in each
> # category, we need to weight by the votes variable
> qplot(rating, data=movies, geom="bar", weight=votes)


> # We could also see the total expenditure for each category:
> qplot(rating, data=movies, geom="bar", weight=budget)


> 
> # A bar chart used to display means
> meanprice <- tapply(diamonds$price, diamonds$cut, mean)
> qplot(unique(diamonds$cut), meanprice)


> qplot(unique(diamonds$cut), meanprice, geom="bar", stat="identity")