Boxplots +
Tags: boxplot, datascience, datavisualization, flatironschool
Published: 2019-08-08 17:05:55

Having spent the last 6 years fielding the question, "When are we ever going to use this in real life?" I'm excited to approach boxplots with a different audience in mind. 

__The Basics:__

The boxplot is a standardized method used to display the distribution of data based on a 5 number summary: minimum, quartile 1 median, quartile 2, and maximum. 

__IQR__: interquartile range, calculated as Q3 - Q1
__Minimum__: Q1 - 1.5 * IQR
__Quartile 1__: 25th percentile 
__Median__: 50th percentile center value of the data's full range
__Quartile 3__: 75th percentile
__Maximum__: Q3 + 1.5 * IQR

![](https://thepracticaldev.s3.amazonaws.com/i/gf48k6btwkayt5ylpxf2.png)
<center>[Image Source ](https://support.sas.com/documentation/cdl/en/statug/63347/HTML/default/viewer.htm#statug_boxplot_sect012.htm)</center>

Beyond the summary lines, boxplots allow you to view outliers, symmetry, tightness of grouping, and any skews your data may have. Boxplots are best used when you want more information than just the central measures of tendency as they make dispersion and variability visible. 

__Group Comparisons:__

Boxplots work their magic when you are comparing many groups of data sets. The visible 5 summary statistics make similarities and differences between groups evident. For example, the following plot was created while exploring trends in movie data. The release dates of movies ranging from 2006 - 2018 were grouped by release week and plotted against %ROI. The aim was to determine if there were weeks that were favorable to others in terms of profitability. 
![](https://thepracticaldev.s3.amazonaws.com/i/aefgrf0asu0jyql5x7tr.jpg)
Here the visual effect was enhanced by utilizing a colormap. Based on the percentile of data above or below the break-even point, the boxplots are shaded in a gradient or red or blue. The visual allows for quick insight, directing further exploration. 

__Extensions:__
Despite the depth of information, it could be useful to have a more direct means of comparison between plots. Focusing on a subset of the release week data I wanted to implement notched boxplots and a swarm overlay.

__Notched Boxplots:__
The notched boxplot displays a confidence interval around the median, generally a 95% confidence interval.  If the notches in the boxplots being compared do not overlap then there is evidence that the medians are significantly different.  I chose a few weeks where I expected some medians to be significantly different and some not to be.
![](https://thepracticaldev.s3.amazonaws.com/i/1vkde93n7vnvd69kbzxz.jpg)
The notches help to display that the medians for the weeks of May 17th and May 31st  are significantly different than those for the weeks of April 19th and April 26th. 

__Swarm Overlay Boxplots:__
One downside to boxplots is being unable to see all observations that are underlying the distribution of the plot. A swarm plot overlay not only allows us to see all observations but ensures data points are a minimum distance away from one another to over overlapping. The swarm overlay gives a better representation of the distribution of values.
![](https://thepracticaldev.s3.amazonaws.com/i/tiyvko3bbbntj4zisl0i.jpg)
To get the full effect of the swarm overlay, be sure to change the transparency level of your box plots or your points will be hidden behind the box itself.