How to Create a Horizontal Bar Chart in R (With Example)


horizontal bar chart is a type of chart that displays numerical values along the x-axis of the chart, categorical values along the y-axis of the chart, and bars inside the chart to represent quantities.

This type of chart is particularly useful when you have a lot of values for the categorical variable and/or the names of the values for the categorical variable are long and are better displayed on the y-axis of the chart as opposed to the x-axis.

The easiest way to create a horizontal bar chart in the R programming language is by using the geom_col() function from the ggplot2 package, which was designed for this exact task.

This function uses the following basic syntax:

library(ggplot2)

ggplot(df) +
  geom_col(aes(points, team))

This particular example creates a horizontal bar chart, using the points variable as the x-axis variable and the team variable as the y-axis variable.

Note that you may need to use the following syntax to first install the ggplot2 package if it is not already installed on your computer:

install.packages('ggplot2')

Once this package is installed, you can use the functions from ggplot2 to create a horizontal bar chart.

The following example shows how to create a horizontal bar chart in practice.

Example: How to Create a Horizontal Bar Chart in R

For this example, we will first create the following data frame that contains information about the total points scored by basketball players on various teams:

#create data frame
df <- data.frame(team=c('Mavs', 'Nets', 'Heat', 'Kings', 'Lakers', 'Hawks'),
                 points=c(34, 22, 18, 19, 40, 28))

#view data frame
df

    team points
1   Mavs     34
2   Nets     22
3   Heat     18
4  Kings     19
5 Lakers     40
6  Hawks     28

Suppose that we would like to create a horizontal bar chart to visualize the points scored by the players on each team.

We can use the following syntax with the geom_col() function from the ggplot2 package to create a basic horizontal bar chart:

library(ggplot2)

#create horizontal bar chart
ggplot(df) +
  geom_col(aes(points, team))

This produces the following chart:

The x-axis displays the points scored by each player and the y-axis displays the team name for each player.

This chart makes it easy to compare the total points scored by the various players.

For example, we can see that the player on the Heat scored the fewest points while the player on the Lakers scored the most points.

Note that ggplot2 also automatically sets the limits on the x-axis to make the bars the easiest to view.

Also note that we can use the fill and width arguments within the geom_col() function to specify the fill color to use and the width of the bars in the plot, respectively:

library(ggplot2)

#create horizontal bar chart with red fill and narrower bars
ggplot(df) +
  geom_col(aes(points, team), fill='red', width=0.7)

This produces the following plot:

horizontal bar chart in R

Feel free to use whatever fill color and width you would like depending on how you would like the bars to appear in your own chart.

Additional Resources

The following tutorials explain how to perform other common tasks in ggplot2:

How to Rotate Axis Labels in ggplot2
How to Set Axis Breaks in ggplot2
How to Set Axis Limits in ggplot2
How to Change Legend Labels in ggplot2

Featured Posts

Leave a Reply

Your email address will not be published. Required fields are marked *