R Bar Plot - ggplot2 - Learn By Example (2024)

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

R Bar Plot - ggplot2 - Learn By Example (1)

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 people1 Apple 402 Banana 503 Grapes 304 Kiwi 155 Orange 356 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.

# Create a basic bar graph with ggplotlibrary(ggplot2)ggplot(survey, aes(x=fruit, y=people)) + geom_bar(stat="identity")
R Bar Plot - ggplot2 - Learn By Example (2)

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.

# Change the colors of individual bars (default fill colors)ggplot(survey, aes(x=fruit, y=people, fill=fruit)) +  geom_bar(stat="identity")
R Bar Plot - ggplot2 - Learn By Example (3)

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

# Manually set fill colors for the barsggplot(survey, aes(x=fruit, y=people, fill=fruit)) +  geom_bar(stat="identity") + scale_fill_manual(values=c("red2", "yellow2", "slateblue4", "green3", "orange", "olivedrab2"))
R Bar Plot - ggplot2 - Learn By Example (4)

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

The following palettes are available for use with these scales:

R Bar Plot - ggplot2 - Learn By Example (5)
# Use preset color schemes for the barsggplot(survey, aes(x=fruit, y=people, fill=fruit)) +  geom_bar(stat="identity") + scale_fill_brewer(palette="Oranges")
R Bar Plot - ggplot2 - Learn By Example (6)

Shades of gray come out well in print as well as photocopying. To display graphs only in gray scale, use scale_fill_grey().

# Use grayscale pattern for the barsggplot(survey, aes(x=fruit, y=people, fill=fruit)) +  geom_bar(stat="identity") + scale_fill_grey()
R Bar Plot - ggplot2 - Learn By Example (7)

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

# Change the color used for the border of the barsggplot(survey, aes(x=fruit, y=people, fill=fruit)) + geom_bar(stat="identity", colour="dodgerblue", fill="white")
R Bar Plot - ggplot2 - Learn By Example (8)

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.

# Change the ggplot theme to 'Minimal'ggplot(survey, aes(x=fruit, y=people, fill=fruit)) +  geom_bar(stat="identity") + theme_minimal()
R Bar Plot - ggplot2 - Learn By Example (9)

Other than theme_minimal, following themes are available for use:

R Bar Plot - ggplot2 - Learn By Example (10)

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. he default bar width is 0.9.

# Set the width of each barggplot(survey, aes(x=fruit, y=people)) +  geom_bar(stat="identity", width=0.5)
R Bar Plot - ggplot2 - Learn By Example (11)

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
ggplot(survey, aes(x=fruit, y=people, fill=fruit)) +  geom_bar(stat="identity") + ggtitle("Favorite fruit survey") + xlab("Fruits") + ylab("Number of People")
R Bar Plot - ggplot2 - Learn By Example (12)

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().

# Add labels above barsggplot(survey, aes(x=fruit, y=people, fill=fruit)) +  geom_bar(stat="identity") + geom_text(aes(label=people), vjust=-0.3, size=3.5)
R Bar Plot - ggplot2 - Learn By Example (13)
# Add labels under the tops of barsggplot(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)
R Bar Plot - ggplot2 - Learn By Example (14)

Horizontal Bar Graph

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

ggplot(survey, aes(x=fruit, y=people, fill=fruit)) +  geom_bar(stat="identity") + coord_flip()
R Bar Plot - ggplot2 - Learn By Example (15)

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:

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 people1 Men Apple 222 Men Kiwi 103 Men Grapes 154 Men Banana 235 Men Pears 126 Men Orange 187 Women Apple 188 Women Kiwi 59 Women Grapes 1510 Women Banana 2711 Women Pears 812 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.

ggplot(survey, aes(x=fruit, y=people, fill=group)) +  geom_bar(stat="identity")
R Bar Plot - ggplot2 - Learn By Example (16)

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.

# Create a grouped bar graphggplot(survey, aes(x=fruit, y=people, fill=group)) +  geom_bar(stat="identity", position=position_dodge())
R Bar Plot - ggplot2 - Learn By Example (17)

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.

# Add space between bars within each groupggplot(survey, aes(x=fruit, y=people, fill=group)) +  geom_bar(stat="identity", width=0.7, position=position_dodge(width=0.8))
R Bar Plot - ggplot2 - Learn By Example (18)
R Bar Plot - ggplot2 - Learn By Example (2024)

References

Top Articles
Latest Posts
Article information

Author: Kelle Weber

Last Updated:

Views: 6116

Rating: 4.2 / 5 (53 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Kelle Weber

Birthday: 2000-08-05

Address: 6796 Juan Square, Markfort, MN 58988

Phone: +8215934114615

Job: Hospitality Director

Hobby: tabletop games, Foreign language learning, Leather crafting, Horseback riding, Swimming, Knapping, Handball

Introduction: My name is Kelle Weber, I am a magnificent, enchanting, fair, joyous, light, determined, joyous person who loves writing and wants to share my knowledge and understanding with you.