R Bar Plot – ggplot2

A Bar Graph (or a Bar Chart) is a graphical display of data using bars of different heights.

typical bar graph

They are good if you to want to visualize the data of different categories that are being compared with each other.

Create a Basic Bar Graph

To get started, you need a set of data to work with.

Let’s consider a survey was conducted of a group of 190 individuals, who were asked “What’s your favorite fruit?”

The result might appear as follows:

Fruit:AppleKiwiGrapesBananaPearsOrange
People:401530502035

Let’s put this data in a data frame.

> survey <- data.frame(fruit=c("Apple", "Banana", "Grapes", "Kiwi", "Orange", "Pears"),
+                      people=c(40, 50, 30, 15, 35, 20))
> survey
   fruit people
1  Apple     40
2 Banana     50
3 Grapes     30
4   Kiwi     15
5 Orange     35
6  Pears     20

To create a bar graph, use ggplot() with geom_bar(stat="identity") and specify what variables you want on the X and Y axes.

Example: Create a basic bar graph with ggplot

> library(ggplot2)
> ggplot(survey, aes(x=fruit, y=people)) +
         geom_bar(stat="identity")

Plot:

Coloring a Bar Graph

Often you want to apply different colors to the bars in your graph.

By default, bar graphs use a very dark grey for the bars. You can change this by using the fill argument.

Example: Change the colors of individual bars (default fill colors)

> ggplot(survey, aes(x=fruit, y=people, fill=fruit)) + 
+        geom_bar(stat="identity")

Plot:

If the default colors aren’t to your liking, you can set the colors manually using scale_fill_manual()

Example: Manually set fill colors for the bars

> ggplot(survey, aes(x=fruit, y=people, fill=fruit)) + 
+        geom_bar(stat="identity") +
+        scale_fill_manual(values=c("red2", "yellow2", "slateblue4", "green3", "orange", "olivedrab2"))

Plot:

It is also possible to use preset color schemes using scale_fill_brewer()

The following palettes are available for use with these scales:

color brewer color schemes

Example: Use preset color schemes for the bars

> ggplot(survey, aes(x=fruit, y=people, fill=fruit)) + 
+        geom_bar(stat="identity") +
+        scale_fill_brewer(palette="Oranges")

Plot:

Shades of gray come out well in print as well as photocopying.

To display graphs only in gray scale, use scale_fill_grey().

Example: Use grayscale pattern for the bars

> ggplot(survey, aes(x=fruit, y=people, fill=fruit)) + 
+        geom_bar(stat="identity") +
+        scale_fill_grey()

Plot:

By default, there is no outline around the fill. To add an outline, use color argument.

Example: Change the color used for the border of the bars

> ggplot(survey, aes(x=fruit, y=people, fill=fruit)) +
+        geom_bar(stat="identity", colour="dodgerblue", fill="white")

Plot:

Change Theme

The ggplot2 package provides some premade themes to change the overall plot appearance.

With themes you can easily customize some commonly used properties, like background color, panel background color and grid lines.

Example: Change the ggplot theme to ‘Minimal’

> ggplot(survey, aes(x=fruit, y=people, fill=fruit)) + 
+        geom_bar(stat="identity") +
+        theme_minimal()

Plot:

Other than theme_minimal, following themes are available for use:

ggplot theme

Adjust Bar Width

Although the default bar width is highly appealing, you can adjust it by setting the width in geom_bar()

Larger values make the bars wider, while smaller values make the bars narrower.

The default bar width is 0.9.

Example: Set the width of each bar

> ggplot(survey, aes(x=fruit, y=people)) + 
+   geom_bar(stat="identity", width=0.5)

Plot:

Adding Titles and Axis Labels

You can add your own title and axis labels easily by specifying following functions.

ArgumentDescription
ggtitle()Main plot title
xlab()x‐axis label
ylab()y‐axis label

Example: Add the title and axis labels to your plot

> ggplot(survey, aes(x=fruit, y=people, fill=fruit)) + 
+   geom_bar(stat="identity") +
+   ggtitle("Favorite fruit survey") +
+   xlab("Fruits") +
+   ylab("Number of People")

Plot:

Adding Labels

Adding labels/markers to a bar graph will help you interpret the graph correctly.

You can label each bar with the actual value by incorporating geom_text().

Example: Add labels above bars

> ggplot(survey, aes(x=fruit, y=people, fill=fruit)) + 
+   geom_bar(stat="identity") +
+   geom_text(aes(label=people), vjust=-0.3, size=3.5)

Plot:

Example: Add labels under the tops of bars

> ggplot(survey, aes(x=fruit, y=people, fill=fruit)) + 
+   geom_bar(stat="identity") +
+   geom_text(aes(label=people), vjust=1.5, colour="white", size=3.5)

Plot:

Horizontal Bar Graph

It is also possible to plot bars horizontally by applying the coord_flip() function, which flips the x and y coordinates.

Example: Create a horizontal bar graph

> ggplot(survey, aes(x=fruit, y=people, fill=fruit)) + 
+        geom_bar(stat="identity") +
+        coord_flip()

Plot:

Stacked Bar Graph

If your data contains several groups of categories, you can display the data in a bar graph in one of two ways. You can decide to show the bars in groups (grouped bars) or you can choose to have them stacked (stacked bars).

Suppose, our earlier survey of 190 individuals involved 100 men and 90 women with the following result:

applekiwigrapebananapearorange
men221015231218
women1851527817

You can put this data in a data frame like this:

Example:

> survey <- data.frame(group=rep(c("Men", "Women"),each=6),
+                      fruit=rep(c("Apple", "Kiwi", "Grapes", "Banana", "Pears", "Orange"),2),
+                      people=c(22, 10, 15, 23, 12, 18, 18, 5, 15, 27, 8, 17))
> survey
   group  fruit people
1    Men  Apple     22
2    Men   Kiwi     10
3    Men Grapes     15
4    Men Banana     23
5    Men  Pears     12
6    Men Orange     18
7  Women  Apple     18
8  Women   Kiwi      5
9  Women Grapes     15
10 Women Banana     27
11 Women  Pears      8
12 Women Orange     17

Now you can pass this data frame to the ggplot() function to create a stacked bar graph.

Remember to map the categorical variable to fill.

Example: Create a stacked bar graph

> ggplot(survey, aes(x=fruit, y=people, fill=group)) + 
+   geom_bar(stat="identity")

Plot:

Grouped Bar Graph

Grouped bar graphs are similar to stacked bar graphs; the only difference is that the grouped bar graph shows the bars in groups instead of stacking them.

You just have to set position="dodge" in the geom_bar() function to go from one to the other.

Example: Create a grouped bar graph

> ggplot(survey, aes(x=fruit, y=people, fill=group)) + 
+   geom_bar(stat="identity", position=position_dodge())

Plot:

For grouped bars, there is no space between bars within each group by default.

However, you can add some space between bars within a group, by making the width smaller and setting the value for position_dodge to be larger than width.

Example: Add space between bars within each group

> ggplot(survey, aes(x=fruit, y=people, fill=group)) + 
+   geom_bar(stat="identity", width=0.7, position=position_dodge(width=0.8))

Plot: