# Barplots, Histograms and Boxplots

**Note:** This entry revolves specifically around Barplots, Histograms and Boxplots. For more general information on quantitative data visualisation, please refer to Introduction to statistical figures. For more info on Data distributions, please refer to the entry on Data distribution.

**In short:** Barplots, histograms and boxplots are commonly used in (descriptive) statistics to visualise distribution of numerical data with two variables (typically the response variable on the X axis and the categorical explanatory variable on the Y axis).

## Barplots

### Overview

A barplot(also known as 'bar chart' or 'column chart') displays quantitative values for different categories. The chart comprises line marks (bars) – not rectangular areas – with the size attribute (length or height) used to represent the quantitative value for each category.

### Plotting in R

We will plot a basic bar chart based on a dataset built-in to R called `mtcars`

. The data set contains data on specifications of different cars. One such specification is the number of gears a given car's transmission has.
We will first create a summary table that contains the number of cars for a given count of gears. Then, we will use that table to create the plot.

The table that contains information about the frequency of cars for a given number of gears looks like this:

gears | freq |
---|---|

3 | 15 |

4 | 12 |

5 | 5 |

... | ... |

Here, the data for `gears`

column are categories, and the data for `freq`

columns are numeric.

# get the data gears <- table(mtcars$gear) # Fig.2 # Plot a basic bar chart with a title and labels barplot(gears, main = "Frequency of Vehicles of each Gear Type", # title of the plot xlab = "Number of Gears", ylab = "Number of Cars") # labels of the plot

For more on Barplots, please refer to the entry on Stacked Barplots.

## Histograms

### Overview

A histogram is a graphical display of data using bars (also called buckets or bins) of different height, where each bar groups numbers into ranges. Histograms reveal a lot of useful information about numerical data with a single explanatory variable. Histograms are used for getting a sense about the distribution of data, its median, and skewness.

### Plotting in R

#Fig.3 par(mfrow = c(2,1)) hist(beaver2$temp[1:38], main = "Body temperature of a beaver (in rest)", xlab = "Body Temperature in Celcius", breaks = 5) hist(beaver2$temp[39:100], main = "Body temperature of a beaver (in move)", xlab = "Body Temperature in Celcius", breaks = 5)

The two histograms are plotted from the “beaver2” dataset and illustrate how a beaver’s body temperature changes when it starts moving. Both histograms resemble the bell-curved shape of normal distribution. We can see a change in the beaver’s body temperature from approximately 37 degrees to 38 degrees.

#### Identifying and interpreting histograms

**Histograms Vs. Bar charts**
Histograms are different than bar charts, and one should not confuse them. A histogram does not have gaps between the bars, but a bar chart does. Histograms have the response variable on the X-axis, and the Y-axis shows the frequency (or the probability density). In contrast, the Y-axis in a bar chart shows the frequency and the X-axis shows the response variable, which however represents nominal data.

**Patterns**
Histograms display well how data is distributed. For instance, a the symmetric, unimodal pattern of a histogram represents a normal distribution. Likewise, skewed right and left patterns in histograms display skewness of data - asymmetry of the distribution of data around the mean. See “Histogram: Study the Shape” to learn more about histogram patterns.

#Fig.4 hist(beaver2$temp, main = "Bimodal Histogram: Body temperature of a beaver (in rest & in move)", xlab = "Body Temperature in Celcius", breaks = 12)

If the beaver2 dataset plotted into one histogram, it takes bimodal pattern and represents binomial distribution as there are two means of sample points - the temperature of a beaver in rest and in the move.

**Number and width of the bars (bins)**
Histograms can become confusing depending on how the bin margin is put. As it is said in The R Book, p231 - “Wide bins produce one picture, narrow bins produce a different picture, unequal bins produce confusion.” Choice of number and width of bins techniques can heavily influence a histogram’s appearance, and choice of bandwidth can heavily influence the appearance of a kernel density estimate. Therefore, it is suggested that the bins stay in the same width and that the number of the bins is selected carefully to best display pattern in data.

## Boxplots

### Overview

The box plot is a diagram suited for showing the distribution of continuous, univariate data, by visualizing the following six characteristics of a dataset: minimum, first quartile, median, third quartile, maximum and outliers. The compact and space-saving design of box plots allows the comparison of more than one dataset, which is why they have an edge over other diagrams for continuous data, like histograms. A good example for comparative visualisation of both, boxplots and histograms, can be found in the R for Data Science book, chapter 7.51:

### Components of a boxplot

Prerequisite: The values of the dataset should be sorted in ascending order.

**Minimum:**
Lowest value of the dataset (outliers excluded)

**First quartile:**
Value which seperates the lower 25% from the upper 75% of the values of the dataset

**Median:**
Middle value of the dataset

**Third quartile:**
Value which seperates the upper 25% from the lower 75% of the values of the dataset

**Interquartile range:**
Range between first and third quartile

**Maximum:**
Highest value of the dataset (outliers excluded)

**Whiskers:**
Lines ranging from minimum to first quartile and from third quartile to maximum

**Outliers:**
Abnormal values represented by circles beyond the whiskers

### Plotting in R

#### Single box plot

#Fig.5 boxplot(airquality$Ozone, main = "Mean of Ozone on Roosevelt Island from May to Sep 1973 ", xlab="Ozone", ylab="Parts per Billion", boxwex = 0.5, # defines width of box las = 1, # flips labels on y-axis into horizontal position col="red", # defines colour of box border = "black" # turns frame and median of box to black )

By default, box plots are plotted vertically. It can be flipped into a horizontal position, by passing the argument **horizontal** and setting it to **TRUE**. Furthermore, the box can be equipped with a **notch**, by passing the argument notch and setting it to **TRUE**.

#### Multiple box plots with interaction

#Fig.6 boxplot(chickwts$weight ~ chickwts$feed, las = 1, main = "Weights of chicks given different diets for 6 weeks", xlab = "Feed", ylab = "Weight in grams", col = "red", border = "black" ) #creates interaction between weight and feed column of dataset

#### Sorting multiple box plots

Multiple box plots can be sorted according to their characteristics. The following box plot shows the plot from above, sorted in ascending order by the median.

#Fig.7 par(mfrow = c(1, 1)) # sets the plot window to a one by one matrix medians <- reorder(chickwts$feed, chickwts$weight, median) # sorts columns feed and weight according to the median boxplot(chickwts$weight ~ medians, las = 1, main = "Weights of chicks given different diets for 6 weeks", xlab = "Feed") # plots interaction of weight and medians

#### Boxplot with notches

Simple boxplots are not very useful at illustrating if the median values between the groups of sets of data are significantly different. Therefore, we use boxplot with nothces to show the distribution of data points around the median and to see whether or not the median values are different between the groups. The notches are drawn as a ‘waist’ on either side of the median and are intended to give a rough impression of significance of the differences between two medians. Boxes in which the notches do not overlap are likely to prove to have significantly different medians under an appropriate statistical test (The R Book, p213).

The size of the notch increases with the magnitude of the interquartile range and declines with the square root of replication:

where ‘IQR’ is the interquartile range, and ‘n’ is the replication per sample. Notches are based on assumptions of asymptotic normality of the median and roughly equal sample sizes for two medians being compared and are said to be rather insensitive to the underlying distribution of the samples. The idea is to give roughly a 95% confidence interval for the difference of two medians. (The R Book, p213)

Here are the beaver2 data that was used earlier for plotting histograms:

#Fig.8 par(mfrow = c(1,2)) boxplot(beaver2$temp[1:38],beaver2$temp[39:100], names = c("in rest", "in move"), ylab = "Body temperature", main = "Boxplot without notches") boxplot(beaver2$temp[1:38],beaver2$temp[39:100], names = c("in rest", "in move"), ylab = "Body temperature", notch = T, main = "Boxplot with notches")

Because the boxes do not overlap, it can be assumed that the difference in the median of the two data samples will be highly significant. However, the same cannot be said for the dataset plotted in the boxplots below.

Below example JohnsonJohnson dataset in R contains quarterly earnings (dollars) per Johnson & Johnson shares share between 1960 and 1980.

#Fig.9 JnJ <- as.data.frame(type.convert(.preformat.ts(JohnsonJohnson))) boxplot(JnJ,notch = T)

Warning message in bxp(list(stats = structure(c(0.61, 1.16, 2.79, 6.93, 14.04, 0.63, : “some notches went outside hinges ('box'): maybe set notch=FALSE”

**The odd-looking boxplots**
The JohnsonJohnson boxplot also illustrates the notches’ curious behaviour in the boxes of the Qtr1 and Qtr2. The notches extended above 75th percentile and/or below the 25th percentile when the sample sizes are small and/or the within-sample variance is high. The notches act this way to warn of the likely invalidity of the test (The R Book, p214).

### Interpretation

**Symmetry:**

If a dataset is symmetric, the median is approximately in the middle of the box.

**Skewness:**

If a dataset is skewed, the median is closer to one end of the box than to the other. If the median is closer to the lower (or left) end of the box, the data is considered to be right-skewed. If the median is closer to the upper (or right) end of the box, the data is considered to be left-skewed. There are several ways to exactly measure the skewness of data, such as the Bowley-Galton skewness or Pearson's coefficient of skewness. Unfortunately, depending on the characteristics of the dataset, different skewness measures might give different or even contradictory results. A hands-on approach for R is included in the further readings below.

**Compactness of data sections:**

If one side of the box is larger than the other, it means that the values in this section are spread over a wider range. If it is smaller than the other one, the values in this section are more densely distributed.

**Distance and number of outliers:**

The number of outliers and their distance from the rest of the data can be easily read from the diagram.

**Significance of difference between two datasets:**

In case of a normal distribution, the significance of the difference between the data of two box plots can be roughly estimated: If the box of one box plot is higher or lower than the median of another box plot, the difference is likely to be significant. Further investigation is recommended.

## Conclusion

We can conclude that both histogram and boxplots are a great tool in displaying how our values are distributed. As stated in Introduction to statistical figures, boxplots compared to histogram show the variance of continuous data across different factor levels and are a solid graphical representation of the Analysis of Variance. One could see box plots less informative than a histogram or kernel density estimate but they take up less space and are better at comparing distributions between several groups or sets of data.

## Further links & reading material

- Box plot variations
- Box plots with point identification
- Approaches to measuring skewness in R
- Good description with many graphics

### Practice more

If you do not have installed R or if you cannot run in on your computer, you can run R Code online with Snippets and folow the examples in below resources.

### Cookbook for R

- Histogram and density plot in base R
- Boxplots in base R
- Plotting distributions with ggplot2

The authors of this entry is Ilkin Bakhtiarov and Henrik von Wehrden.