Bar Chart
Learn how to build a bar chart in ggplot2.
Introduction to bar charts
Bar charts are an important and commonly used data visualization in statistics. They are sometimes also referred to as column charts.
Often used to depict summarized categorical data, bar charts are pleasant and easy to understand for readers as the height of the bar is proportional to the value of the plotted variable. Sometimes, a bar chart can also represent an underlying trend in the dataset. The following are some common presentation styles for a bar chart:
- Vertical bar chart or column chart
- Horizontal bar chart
- Stacked bar chart
- Side-by-side or a grouped bar chart
- Circular bar chart
Basic bar chart in ggplot2
The bar height in a bar chart represents the value of a variable. Hence, it is essential to show the base of the bar to make a valid visual comparison of the values. Here we’ll use the Cars93
dataset from the MASS
package.
In ggplot2
, we can plot the bar chart using either the geom_bar()
function or the geom_col()
function.
There is a subtle difference between them. Using the geom_bar()
function, the bar height is always proportional to the number of elements in each category. On the other hand, we’ll use the geom_col()
function if we want the heights of the bars to correspond to the actual data values.
Get hands-on with 1400+ tech skills courses.