Bar Charts in R

Bar charts in R

A bar chart uses rectangular bars to compare the values of different categories. The x-axis of the chart shows the category names and the y-axis shows the values of those categories.

This tutorial teaches you how to create a bar chart using base R as well as using the data visualization package ggplot2.

Bar Charts using base R

The basic syntax to create a bar chart in base R is the following:

barplot(H,xlab,ylab,main,names.arg,col)

where 

  • H is a vector or matrix containing numeric values used in bar chart
  • xlab is the label for the x-axis
  • ylab is the label for the y-axis
  • main is the title of the bar chart
  • names.arg is a vector of names appearing under each bar
  • col is the color of the bars in the graph

Vertical Bar Chart

To create  the most basic bar version of a vertical bar chart in R, all you need is a vector of values:

#Create a vector of values to use in the bar chart
H <- c(1, 4, 5, 6, 12, 14, 23)

#Create the bar chart
barplot(H)

Bar chart in base R

To create  a more advanced bar chart in R, you can add a vector of names to appear under each bar, a title for the chart, labels for both axes, a color to fill in the bars, and a border color for the bars.

#Create a vector of values to use in the bar chart
H <- c(1, 4, 5, 6, 12, 14, 23)

#Create a vector of names to appear under the bars
M <- c("Jan", "Feb", "Mar", "Apr", "May", "June", "July")

#Create the bar chart
barplot(H,names.arg=M,xlab="Month",ylab="Sales",col="steelblue", main="Sales by Month",border="black")

Bar chart in R with titles and axis labels

Horizontal Bar Chart

To create a horizontal bar chart, add the argument horiz=TRUE, switch the axis labels, and add the argument par(las=2) to make the y-axis labels perpendicular to the y-axis:

#Make the y-axis labels perpendicular to the y-axis:
par(las=2)

#Create a horizontal bar chart
barplot(H,names.arg=M,xlab="Sales",ylab="Month",col="steelblue",
        main="Sales by Month",border="black", horiz=TRUE)

Horizontal bar chart in R

Stacked Bar Chart

To create a stacked bar chart for the dataset above, use the following syntax:

#Create dummy dataset
category <- c(1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3)
amount <- c(0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1)
data <- table(amount, category)
barplot(data, main="Distribution by Category", xlab="Category", col=c("steelblue", "coral2"),
        legend=rownames(data))

#Here is what that dataset looks like
       category
amount 1 2 3
     0 2 3 1
     1 2 2 5

#Create stacked bar chart
barplot(counts, main="Distribution by Category", xlab="Category", ylab="Amount", col=c("steelblue", "coral2"))

Stacked bar chart in R

To add a legend to the chart, use the following code:

#Define margins of chart
par(mfrow=c(1, 1), mar=c(5, 5, 4, 8))

#Create bar chart with legend in top right corner with no box around legend (bty="n")
barplot(counts, main="Distribution by Category", xlab="Category", ylab="Amount", col=c("steelblue", "coral2"),
        legend.text=TRUE, args.legend = list(x="topright", bty="n", inset=c(-0.15, 0)))

Bar chart in R with legend

Grouped Bar Chart

To create a grouped bar chart, simply add the argument “beside=TRUE” to the previous code:

#Define margins of chart
par(mfrow=c(1, 1), mar=c(5, 5, 4, 8))

#Create bar chart with legend in top right corner with no box around legend (bty="n")
barplot(counts, main="Distribution by Category", xlab="Category", ylab="Amount", col=c("steelblue", "coral2"),
        legend.text=TRUE, args.legend = list(x="topright", bty="n", inset=c(-0.15, 0)), beside=TRUE)

Grouped bar chart in R

Bar Charts using ggplot2

ggplot2 is a data visualization package for R. To install ggplot2, use the following code:

#Install ggplot2 (if not already installed)
install.packages("ggplot2")

#Load ggplot2
library(ggplot2)

Vertical Bar Chart using ggplot2

To create a vertical bar chart in ggplot2, the basic syntax is the following:

ggplot(data=dataset, aes(x=x_variable, y=y_variable, fill=fill_variable)) +
  geom_bar(stat="identity") + #or you could use stat="count" if data is not already summarized
  xlab("X label") + ylab("Y label") +
  ggtitle("Chart title")

To create a simple bar chart, we’ll use the built-in mpg dataset:

#View first six lines of mpg dataset
head(mpg)

  manufacturer model displ  year   cyl      trans   drv   cty   hwy    fl   class
         <chr> <chr> <dbl> <int> <int>      <chr> <chr> <int> <int> <chr>   <chr>
1         audi    a4   1.8  1999     4   auto(l5)     f    18    29     p compact
2         audi    a4   1.8  1999     4 manual(m5)     f    21    29     p compact
3         audi    a4   2.0  2008     4 manual(m6)     f    20    31     p compact
4         audi    a4   2.0  2008     4   auto(av)     f    21    30     p compact
5         audi    a4   2.8  1999     6   auto(l5)     f    16    26     p compact
6         audi    a4   2.8  1999     6 manual(m5)     f    18    26     p compact

#create bar chart with class on x-axis and count of cars on y-axis
ggplot(data=mpg, aes(x=class)) +
  geom_bar(fill="steelblue")

Bar chart in ggplot2

Vertical Bar Chart using ggplot2

To create a horizontal bar chart in ggplot2, simply add the argument coord_flip()

#create bar chart with class on x-axis and count of cars on y-axis
ggplot(data=mpg, aes(x=class)) +
  geom_bar(fill="steelblue") +
  coord_flip()

Horizontal bar chart in ggplot2

Stacked Bar Chart using ggplot2

To create a stacked bar chart in ggplot2, simply add the variable you would like to stack  to the fill argument:

#create stacked bar chart with class on x-axis and count of cars on y-axis, stacked by type of drive
ggplot(data=mpg, aes(x=class, fill=drv)) +
  geom_bar()

Stacked bar chart in ggplot2

Grouped Bar Chart using ggplot2

To create a grouped bar chart, simply add the argument position=“dodge” to the geom_bar():

#create grouped bar chart with class on x-axis and count of cars on y-axis, grouped by type of drive
ggplot(data=mpg, aes(x=class, fill=drv)) +
  geom_bar(position="dodge")

Grouped bar chart in ggplot2

Leave a Reply

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