R Bar Plot – Base Graph

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.

The barplot() function

In R, you can create a bar graph using the barplot() function.

It has many options and arguments to control many things, such as labels, titles and colors.

Syntax

The syntax for the barplot() function is:

barplot(x,y,type,main,xlab,ylab,pch,col,las,bty,bg,cex,)

Parameters

R boxplot() function parameters
ParameterDescription
heightA vector or matrix of values describing the bars which make up the plot
widthA vector specifying bar widths
spaceThe amount of space left before each bar
names.argThe names to be plotted below each bar
legend.textThe text used to construct a legend for the plot
besideIf TRUE the columns are portrayed as juxtaposed bars
horizIf TRUE, the bars are drawn horizontally
densityThe density of shading lines
angleThe slope of shading lines
colA vector of colors for the bars
borderThe color to be used for the border of the bars
mainAn overall title for the plot
xlabThe label for the x axis
ylabThe label for the y axis
Other graphical parameters

Create a Simple 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 vector.

> survey <- c(apple=40, kiwi=15, grape=30, banana=50, pear=20, orange=35)
> survey
 apple   kiwi  grape banana   pear orange 
    40     15     30     50     20     35 

To create a bar graph just specify the vector in barplot() function.

Example: Create a bar graph from a vector of data points

> barplot(survey)

Plot:

It is really a good way to show relative sizes: you can see which fruits are most liked, and which are not, at a glance.

Change Group Names

To explicitly add or change names for each bar or group of bars, use names.arg argument.

Example: Change names for each bar

> survey <- c(apple=40, kiwi=15, grape=30, banana=50, pear=20, orange=35)
> barplot(survey,
+         names.arg=c("Fruit1", "Fruit2", "Fruit3", "Fruit4", "Fruit5", "Fruit6"))

Plot:

If this argument is omitted, then the names are taken from the names attribute of a vector, or the column names from a matrix.

Coloring a Bar Graph

Use col argument to change the colors used for the bars.

Example: Change the bar color

> survey <- c(apple=40, kiwi=15, grape=30, banana=50, pear=20, orange=35)
> barplot(survey,
+         col="dodgerblue3")

Plot:

You can change the colors of individual bars by passing a vector of colors to the col argument.

Example: Change the colors of individual bars

> survey <- c(apple=40, kiwi=15, grape=30, banana=50, pear=20, orange=35)
> barplot(survey,
+         col=c("red2", "green3", "slateblue4", "yellow2", "olivedrab2", "orange"))

Plot:

By using the border argument, you can even change the color used for the border of the bars.

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

> survey <- c(apple=40, kiwi=15, grape=30, banana=50, pear=20, orange=35)
> barplot(survey,
+         col="lightblue1",
+         border="dodgerblue3")

Plot:

Create a Hatched Bar Graph

Creating hatched graphs in R is rather easy, just specify the density argument in the barplot() function.

By default, the bars are hatched with 45° slanting lines; however, you can change it with the angle argument.

Example: Create a hatched barplot with 60° slanting lines

> survey <- c(apple=40, kiwi=15, grape=30, banana=50, pear=20, orange=35)
> barplot(survey,
+         col="dodgerblue3",
+         density=c(30,10,20,35,15,25),
+         angle=60)

Plot:

Adjusting Bar Width and Spacing

To make the bars narrower or wider, set the width of each bar with the width argument.

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

Example: Set the width of each bar

> survey <- c(apple=40, kiwi=15, grape=30, banana=50, pear=20, orange=35)
> barplot(survey,
+         col="dodgerblue3",
+         width=c(30,10,20,35,15,25))

Plot:

To add space between bars, specify the space argument. The default value is 0.2.

Example: Add space between bars

> survey <- c(apple=40, kiwi=15, grape=30, banana=50, pear=20, orange=35)
> barplot(survey,
+         col="dodgerblue3",
+         space=1)

Plot:

Adding Titles and Axis Labels

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

ArgumentDescription
mainMain plot title
xlabx‐axis label
ylaby‐axis label

Example: Add the title and axis labels to your plot

> survey <- c(apple=40, kiwi=15, grape=30, banana=50, pear=20, orange=35)
> barplot(survey,
+         col="dodgerblue3",
+         main="Favorite Fruit",
+         ylab="Number of People")

Plot:

Add a Legend

You can include a legend to your plot, a little box that decodes the graphic for the viewer.

To add a legend, specify following arguments:

ArgumentDescription
legend.texta vector of text used to construct a legend for the plot
args.legendlist of additional arguments to pass to legend()

Example: Add a legend to the top right corner and scale it down by 0.75

> survey <- c(apple=40, kiwi=15, grape=30, banana=50, pear=20, orange=35)
> barplot(survey,
+         col=c("red2", "green3", "slateblue4", "yellow2", "olivedrab2", "orange"),
+         legend.text = c("apple", "kiwi", "grape", "banana", "pear", "orange"),
+         args.legend=list(cex=0.75,x="topright"))

Plot:

Horizontal Bar Graph

You can also plot bars horizontally by setting the horiz argument to TRUE.

Example: Create a horizontal bar graph with horizontal axes labels

> survey <- c(apple=40, kiwi=15, grape=30, banana=50, pear=20, orange=35)
> barplot(survey,
+         col="dodgerblue3",
+         horiz=TRUE,
+         las=1)

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 individual involved 100 men and 90 women with the following result:

applekiwigrapebananapearorange
men221015231218
women1851527817

You can put this data in a matrix like this:

> men <- c(apple=22, kiwi=10, grape=15, banana=23, pear=12, orange=18)
> women <- c(apple=18, kiwi=5, grape=15, banana=27, pear=8, orange=17)
> survey <- rbind(men, women)
> survey
      apple kiwi grape banana pear orange
men      22   10    15     23   12     18
women    18    5    15     27    8     17

Now you can pass this matrix to the barplot() function to create a stacked bar graph.

Example: Create a stacked barplot and add a legend

> barplot(survey,
+         col=c("dodgerblue3", "skyblue1"),
+         legend.text = rownames(m),
+         args.legend=list(cex=0.75,x = "topright"))

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.

In R, you just have to set beside to TRUE to go from one to the other.

Example: Create a grouped barplot and add a legend

> men <- c(apple=22, kiwi=10, grape=15, banana=23, pear=12, orange=18)
> women <- c(apple=18, kiwi=5, grape=15, banana=27, pear=8, orange=17)
> survey <- rbind(men, women)
> barplot(survey,
+         beside = TRUE,
+         col = c("dodgerblue3", "skyblue1"),
+         legend.text = rownames(m),
+         args.legend = list(cex=0.75,x = "topright"))

Plot: