A Bar Graph (or a Bar Chart) is a graphical display of data using bars of different heights.
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
Parameter | Description |
height | A vector or matrix of values describing the bars which make up the plot |
width | A vector specifying bar widths |
space | The amount of space left before each bar |
names.arg | The names to be plotted below each bar |
legend.text | The text used to construct a legend for the plot |
beside | If TRUE the columns are portrayed as juxtaposed bars |
horiz | If TRUE, the bars are drawn horizontally |
density | The density of shading lines |
angle | The slope of shading lines |
col | A vector of colors for the bars |
border | The color to be used for the border of the bars |
main | An overall title for the plot |
xlab | The label for the x axis |
ylab | The 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: | Apple | Kiwi | Grapes | Banana | Pears | Orange |
People: | 40 | 15 | 30 | 50 | 20 | 35 |
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.
barplot(survey)
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.
# 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"))
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.
survey <- c(apple=40, kiwi=15, grape=30, banana=50, pear=20, orange=35)
barplot(survey,
col="dodgerblue3")
You can change the colors of individual bars by passing a vector of colors to the col argument.
survey <- c(apple=40, kiwi=15, grape=30, banana=50, pear=20, orange=35)
barplot(survey,
col=c("red2", "green3", "slateblue4", "yellow2", "olivedrab2", "orange"))
By using the border argument, you can even 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")
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.
# 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)
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.
# 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))
To add space between bars, specify the space argument. The default value is 0.2.
# Add space between bars
survey <- c(apple=40, kiwi=15, grape=30, banana=50, pear=20, orange=35)
barplot(survey,
col="dodgerblue3",
space=1)
Adding Titles and Axis Labels
You can add your own title and axis labels easily by specifying following functions.
Argument | Description |
main | Main plot title |
xlab | x‐axis label |
ylab | y‐axis label |
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")
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:
Argument | Description |
legend.text | a vector of text used to construct a legend for the plot |
args.legend | list of additional arguments to pass to legend() |
# 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"))
Horizontal Bar Graph
You can also plot bars horizontally by setting the horiz argument to TRUE.
# 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)
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:
apple | kiwi | grape | banana | pear | orange | |
men | 22 | 10 | 15 | 23 | 12 | 18 |
women | 18 | 5 | 15 | 27 | 8 | 17 |
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.
# 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"))
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.
# 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"))