Difference between revisions of "Histograms and Boxplots"

From Sustainability Methods
m (Replaced content with "This page was updated and moved to Barplots, Histograms and Boxplots entry.")
Tag: Replaced
 
(15 intermediate revisions by 2 users not shown)
Line 1: Line 1:
'''In short:''' Both histograms and boxplots are commonly used in [https://www.investopedia.com/terms/d/descriptive_statistics.asp (descriptive) statistics] to visualise distribution of [https://365datascience.com/numerical-categorical-data/ numerical data] with two variables (typically the [https://www.statisticshowto.com/explanatory-variable/ response variable] on the X axis and the [https://www.statisticshowto.com/explanatory-variable/ categorical explanatory variable] on the Y axis).
+
This page was updated and moved to [[Barplots,_Histograms_and_Boxplots|Barplots, Histograms and Boxplots]] entry.
 
 
= Histograms and Boxplots =
 
 
 
== How to read this page ==
 
This page is primarily for those who are new to statistics and are looking for tools in data exploration and descriptive infographics. If you already know how to work with histograms and boxplots on a paper, then see “Further links & reading material” part at the end of this page.
 
 
 
== Introduction==
 
Statistical figures play an important role in explorative data analysis. There are many different types of statistical figures and for an overview of the most prominent ones see the [[Statistical_figures_-_on_a_shoestring|Statistical figures - on a shoestring wiki page]]. However, on this page, we will have a rather detailed look into two of the simplest but extremely useful ones - histograms and boxplots. The difference between the two plots depends on what we are intending to see from the data.
 
 
 
[[File:Beaverbodytemperatures.png|350px|thumb|right|Fig.1]]
 
==Histogram==
 
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.
 
 
 
 
 
<syntaxhighlight lang="R" line>
 
#Fig.1
 
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)
 
</syntaxhighlight>
 
 
 
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 X-axis in a bar chart shows the frequency and the Y-axis shows the response variable.
 
[[File:Bimodalhistrogram.png|350px|thumb|right|Fig.2]]
 
 
 
'''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 “[https://www.pqsystems.com/qualityadvisor/DataAnalysisTools/interpretation/histogram_shape.php Histogram: Study the Shape]” to learn more about histogram patterns.
 
 
 
<syntaxhighlight lang="R" line>
 
#Fig.2
 
hist(beaver2$temp, main = "Bimodal Histogram: Body temperature of a beaver (in rest & in move)",
 
      xlab = "Body Temperature in Celcius", breaks = 12)
 
</syntaxhighlight>
 
 
 
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)'''
 
<br>
 
Histograms can become confusing depending on how the bin margin is put. As it is said in The [https://www.wiley.com/en-us/The+R+Book%2C+2nd+Edition-p-9781118448960 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.
 
 
 
==Boxplot==
 
Boxplots are used to illustrate the scatter in the data and good at showing the distribution of data points around the median for several groups of sets of data. A boxplot displays the [https://en.wikipedia.org/wiki/Five-number_summary five number summary] — [https://en.wikipedia.org/wiki/Sample_maximum_and_minimum minimum], [https://mathworld.wolfram.com/Quartile.html first quartile], [https://www.investopedia.com/terms/m/median.asp median], [https://mathworld.wolfram.com/Quartile.html third quartile], and [https://en.wikipedia.org/wiki/Sample_maximum_and_minimum maximum] — of a set of data. Another great thing about boxplots is that they display outliers, value points more than 1.5 times the range above the 75th percentile and more than 1.5 times the range below the 25th percentile. Hence, one would already argue that we a boxplot illustrates more details than a histogram. To understand boxplots better, let’s compare boxplots and histograms visually using an example from [https://r4ds.had.co.nz/index.html R for Data Science] book, [https://r4ds.had.co.nz/exploratory-data-analysis.html#cat-cont chapter 7.51]:
 
[[File:Screenshot 2021-03-29 at 12.42.17.png|500px|frameless|center]]
 
 
 
===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 ([https://www.wiley.com/en-us/The+R+Book%2C+2nd+Edition-p-9781118448960 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:
 
[[File:notchformula1.png|300px|frameless|center]]
 
 
 
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 if two medians. ([https://www.wiley.com/en-us/The+R+Book%2C+2nd+Edition-p-9781118448960 The R Book, p213])
 
 
 
[[File:boxplotswithnwithoutnotches.png|300px|thumb|left|Fig.3]]
 
Here are the beaver2 data that was used earlier for plotting histograms:
 
 
 
<syntaxhighlight lang="R" line>
 
#Fig.3
 
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")
 
</syntaxhighlight>
 
 
 
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.
 
[[File:Boxplotquartiles.png|300px|thumb|left|Fig.4]]
 
Below example JohnsonJohnson dataset in R contains quarterly earnings (dollars) per Johnson & Johnson shares share between 1960 and 1980.
 
 
 
<syntaxhighlight lang="R" line>
 
#Fig.4
 
JnJ <- as.data.frame(type.convert(.preformat.ts(JohnsonJohnson)))
 
boxplot(JnJ,notch = T)
 
</syntaxhighlight>
 
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'''
 
<br>
 
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 ([https://www.wiley.com/en-us/The+R+Book%2C+2nd+Edition-p-9781118448960 The R Book, p214]).
 
 
 
==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==
 
===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 [https://rdrr.io/snippets/ Snippets] and folow the examples in below resources.
 
===[http://www.cookbook-r.com/ Cookbook for R]===
 
#[http://www.cookbook-r.com/Graphs/Histogram_and_density_plot/ Histogram and density plot] in base R
 
#[http://www.cookbook-r.com/Graphs/Box_plot/ Boxplots] in base R
 
#[http://www.cookbook-r.com/Graphs/Plotting_distributions_(ggplot2)/ Plotting distributions] with ggplot2
 
 
 
----
 
The [[Table of Contributors|author]] of this entry is Ilkin Bakhtiarov.
 
[[Category:Statistics]]
 
[[Category:R examples]]
 

Latest revision as of 09:49, 21 May 2021

This page was updated and moved to Barplots, Histograms and Boxplots entry.