2.3 Creating a Bar Graph

2.3.1 Problem

You want to make a bar graph.

2.3.2 Solution

To make a bar graph of values (Figure 2.5, left), use barplot() and pass it a vector of values for the height of each bar and (optionally) a vector of labels for each bar. If the vector has names for the elements, the names will automatically be used as labels:

Bar graph of values with base graphics (left); Bar graph of counts (right)Bar graph of values with base graphics (left); Bar graph of counts (right)

Figure 2.5: Bar graph of values with base graphics (left); Bar graph of counts (right)

Sometimes “bar graph” refers to a graph where the bars represent the count of cases in each category. This is similar to a histogram, but with a discrete instead of continuous x-axis. To generate the count of each unique value in a vector, use the table() function:

Then pass the table to barplot() to generate the graph of counts:

With ggplot2, you can get a similar result using geom_col() (Figure 2.6). To plot a bar graph of values, use geom_col(). Notice the difference in the output when the x variable is continuous and when it is discrete:

Bar graph of values using geom_col() with a continuous x variable (left); With x variable converted to a factor (notice that there is no entry for 6; right)Bar graph of values using geom_col() with a continuous x variable (left); With x variable converted to a factor (notice that there is no entry for 6; right)

Figure 2.6: Bar graph of values using geom_col() with a continuous x variable (left); With x variable converted to a factor (notice that there is no entry for 6; right)

ggplot2 can also be used to plot the count of the number of data rows in each category (Figure 2.7, by using geom_bar() instead of geom_col(). Once again, notice the difference between a continuous x-axis and a discrete one. For some kinds of data, it may make more sense to convert the continuous x variable to a discrete one, with the factor() function.

Bar graph of counts using geom_bar() with a continuous x variable (left); With x variable converted to a factor (right)Bar graph of counts using geom_bar() with a continuous x variable (left); With x variable converted to a factor (right)

Figure 2.7: Bar graph of counts using geom_bar() with a continuous x variable (left); With x variable converted to a factor (right)

Note

In previous versions of ggplot2, the recommended way to create a bar graph of values was to use geom_bar(stat = "identity"). As of ggplot2 2.2.0, there is a geom_col() function which does the same thing.

2.3.3 See Also

See Chapter 3 for more in-depth information about creating bar graphs.