Barplot

What is a Barplot?

The barplot is for comparative representation that illustrates the frequency distribution of a discrete (random) variable by means of non-contiguous columns standing vertically on the x-axis. The bar chart is particularly suitable for illustrating up to approx. 15 characteristics. With more categories, the clarity suffers and line charts are to be preferred. The bar chart is also not suitable in the case of metrically continuous data; then a histogram is preferable.

</div>

Create barplots with the barplot(values) function, where values is a vector or matrix.

  • If values is a vector, the values determine the heights of the bars in the plot.
  • If values is a matrix and the option beside=FALSE then each bar of the plot corresponds to a column of height, with the values in the column giving the heights of stacked “sub-bars”.
  • If height is a matrix and beside=TRUE, then the values in each column are juxtaposed rather than stacked.
  • Include option names.arg=(character vector) to label the bars.
  • Use horiz=TRUE to create a horizontal bar plot.

Example with mtcars:

data(mtcars)
counts <- table(mtcars$cyl)
barplot(counts, main="Car Distribution",
        xlab="Number of Cylinders")
Counts of mtcars by cylinder category: 11 four-cylinder cars, 7 six-cylinder cars and 14 eight-cylinder cars.
Simple Barplot without grouping and colouring.

Simple Horizontal Barplot with Added Labels

counts <- table(mtcars$cyl)
barplot(counts, main="Car Distribution", horiz=TRUE,
        names.arg=c("4 Cylinder", "6 Cylinder", "8 Cylinder"))

Horizontal version of the cylinder-count bar chart, with categories 4, 6 and 8 Cylinder labelled on the vertical axis.
Horizontal barplot with labels and without grouping and colouring.

Stacked Barplot with Colors and Legend

# Stacked Bar Plot with Colors and Legend
counts <- table(mtcars$vs, mtcars$gear)
barplot(counts, main="Car Distribution by Gears and VS",
        xlab="Number of Gears", col=c("steelblue","darkgreen"),
        legend = rownames(counts))
Stacked bars count cars with 3, 4 and 5 gears; segments distinguish engine configurations vs = 0 and vs = 1.
Barplot with legend and colouring.

Grouped and colored Barplot

counts <- table(mtcars$cyl, mtcars$gear)
barplot(counts, main="Car Distribution by Gears and Cylinder",
        xlab="Number of Gears", col=c("darkgreen","steelblue", "darkred"),
        legend = rownames(counts), beside=TRUE)
Grouped bars compare cylinder categories within each gear category; colours and legend identify 4, 6 and 8 cylinders.
Barplot with grouping, colouring and legend.

Difference between Barplot and Histogram:

Barplots are good when your data is in categories (such as “Comedy”, “Drama”, etc). But when you have continuous data (such as a person’s height) then use a Histogram. It is best to leave gaps between the bars of a Bar Graph, so it doesn’t look like a Histogram.

Four panels compare inappropriate and appropriate displays: separate bars for cylinder categories and adjoining histogram bins for horsepower ranges.
Difference between Barplot and Histogram: Barplot are used for categories (e.g. number of cylinders); Histograms for continuous data ranges (e.g. horsepower).

Test your knowledge!

Updated: