Visualizing data prior to any analysis is a basic and important step. Here we will cover
boxplot
, which is a type of univariate plot. Univariate plots are those that take into account one varible, these may include histograms, density plots, boxplots, etc.
BOXPLOT:
Boxplot is a standardized way of displaying the distribution of data based on five summary numbers from the data.
Using boxplot we can see that the distribution of the data and its main characteristics are clearly observed. And it also allows us to compare different sets of data simultaneously.
It is a powerfull visual tool that can be used to illustrate data, to study symmetry, to study queues, assumptions about distribution, and also can be used to compare different populations.
The five numbers used as default in R are:
25th percentil: bottom of the box (Q1)
75th percentile: top of the box (Q3)
50th percentil: band near the middle of the box (Q2, median),
and, the ends of the whiskers can represent different alternative values, but as default in R it is used:
upper whisker = min(max(x), Q3 + 1.5 * IQR)
lower whisker = max(min(x), Q1 - 1.5 * IQR)
where IQR (inter quartil range) = Q3 - Q1 (box length).
Here there are som examples with the dataset
airquality
:
airquality
dataset it has been modified in order to work with it, all the information about how the initial data analysis of this dataset has been done can be found in the following link: http://dataworldblog.blogspot.com/2017/06/initial-data-analysis-handling-missing.html
data(airquality)
head(airquality)
## Ozone Solar.R Wind Temp Month Day
## 1 41 190 7.4 67 5 1
## 2 36 118 8.0 72 5 2
## 3 12 149 12.6 74 5 3
## 4 18 313 11.5 62 5 4
## 5 NA NA 14.3 56 5 5
## 6 28 NA 14.9 66 5 6
airquality$Month <- month.abb[airquality$Month]
airquality$Date <- paste (airquality$Day, airquality$Month)
row.names(airquality) <- airquality$Date
airquality1 <- airquality[c(1:4)]
#The `airquality` modified dataset has been renamed `airquality1`:
head(airquality1)
## Ozone Solar.R Wind Temp
## 1 May 41 190 7.4 67
## 2 May 36 118 8.0 72
## 3 May 12 149 12.6 74
## 4 May 18 313 11.5 62
## 5 May NA NA 14.3 56
## 6 May 28 NA 14.9 66
boxplot(airquality1)
text (2,205,"Q2", adj = c(-3,0))
text (2,115,"Q1", adj = c(-3,0))
text (2,258,"Q3", adj = c(-3,0))
text (2,334,"min( max(x), Q3 + 1.5 * IQR) ", adj = c(-0.05,1))
text (2,7,"max( min(x), Q1 - 1.5 * IQR)", adj = c(-0.05,-1))
#Variables can be ploted horizontally using the parameter `horizontal = TRUE`:
boxplot(airquality1, col= c("red", "pink", "orange", "gold"), horizontal = TRUE)
#for variable's name horizontal: `las = 1` ; for vertical `las = 2`
#`at` parameter controls were to plot each variable
par(mfrow = c(1,2))
boxplot(airquality1, xlab= "Air Quality Variables", las = 2, col = topo.colors(4, alpha = 0.5), at = c(1,2,4,5),cex.lab = "0.75", cex.axis = "0.75")
boxplot(airquality1, xlab= "Air Quality Variables", las = 1, col = topo.colors(4, alpha = 0.5), at = c(5,4,2,1), cex.lab = "0.75", cex.axis = "0.75")
Range
parameter: determines how far the plot whiskers extend out from the box. If range is positive, the whiskers extend to the most extreme data point which is no more than range times the interquartile range from the box. A value of zero causes the whiskers to extend to the data extremes.
par(mfrow = c(1,3))
boxplot(airquality1, xlab= "Air Quality Variables", las = 2, col = topo.colors(4, alpha = 0.5), range = 0, cex.lab = "0.75", cex.axis = "0.75")
boxplot(airquality1, xlab= "Air Quality Variables", las = 2, col = topo.colors(4, alpha = 0.5), cex.lab = "0.75", cex.axis = "0.75")
boxplot(airquality1, xlab= "Air Quality Variables", las = 2, col = topo.colors(4, alpha = 0.5), range = 1, cex.lab = "0.75", cex.axis = "0.75")
title("Comparing range parameter", outer = TRUE)
We see that the whiskers are different in each of them.
Also, we can use BOXPLOT FOR COMPARING DIFFERENT CONDITIONS:
Here, we will use the
chickwts
dataset.
notch
parameter: a notch is drawn in each side of the boxes. If the notches of two plots do not overlap this is 'strong evidence' that the two medians differ (Chambers et al, 1983, p. 62)
varwith
parameter: the boxes are drawn with widths proportional to the square-roots of the number of observations in the groups.
data("chickwts")
head(chickwts)
## weight feed
## 1 179 horsebean
## 2 160 horsebean
## 3 136 horsebean
## 4 227 horsebean
## 5 217 horsebean
## 6 168 horsebean
par(mfrow = c(1,2))
boxplot(weight ~ feed, data = chickwts, col = topo.colors(6, alpha = 0.5), varwidth = FALSE, notch = FALSE, main = "Chickwt data 1", ylab = "Weight at six weeks (gm)", las = 2)
boxplot(weight ~ feed, data = chickwts, col = topo.colors(6, alpha = 0.5), varwidth = TRUE, notch = TRUE, main = "Chickwt data 2", ylab = "Weight at six weeks (gm)", las = 2)
## Warning in bxp(structure(list(stats = structure(c(216, 271.5, 342, 373.5, :
## some notches went outside hinges ('box'): maybe set notch=FALSE
Using the second graph we can compare the effect of different types of feed, we see that some of them overlap (linseed/meatmeal/soybean or casein/sunflower) while others do not overlap (casein/horsebeanor or soybean/sunflower).
Also, in the second plot we can see that we have used
varwith
= TRUE, so the widths of each box are slightly different, while in the fist graph (varwith
= TRUE) the widths are the same for every box.