Tutorial: Drawing a boxplot
What’s a boxplot?
A boxplot is a graphical display of a sample’s five-number summary: the minimum, the maximum, the median (i.e., the middle value if you sort the data from high to low), and the 25th and 75th percentiles. The 25th percentile is the value below which 25% of the data lie; the 75th percentile is the value below which 75% of the data lie. The range between the 25th and 75th percentiles contains 50% of the sample and is known as the inter-quartile range.
A boxplot might look like the one below–the median is highlighted by a thick line, the 25th and 75th are displayed by a box, and the minimum and maximum are plotted as ‘whiskers’:
Often, though, you’ll also see some points that lie beyond the whiskers. These are values that lie too far from the bulk of data, and they’re commonly referred to as outliers. Here’s an example:
It’s important to note, however, that these outliers may be perfectly valid observations, so you shouldn’t remove them from the data set just because they show up in a boxplot.
When to use boxplots, and when there are better choices
Boxplots are often a good choice when you want to compare two or more groups. In particular, I’ll take a boxplot over the ubiquitous ‘average-only’ barplot any day. The plot belows illustrates the problem with barplots: the ‘average-only’ barplots on the left make a crisp impression, but we have no way of knowing how large the 0.6-point difference between the means is in practical terms. (Error bars only slightly alleviate this for me.) Additionally, we don’t know how the data are really distributed—knowing this is usually important when interpreting the results. The boxplots on the right, by contrast, show that—relative to the spread of the data—a 0.6-point difference is tiny. Moreover, they show that both samples have a skewed distribution: the lower 50% of the data is restricted to a small range, whereas the upper 50% is spread out much more.
That said, there are situations where boxplots aren’t optimal. One such situation is when the median is actually fairly atypical of the data. From the left panel below, we would correctly gather that the sample median is slightly higher than 0.4 and that 50% of the data lies between 0.1 and 0.9. A moment’s thought shows that this means that a quarter of the data lies squished between 0 and 0.1 and another quarter between 0.9 and 1, but this may not be something you consciously think of when leafing through a paper—what you focus on is the big white box and the thick line. In this case, the histogram on the right does a much better job of highlighting the interesting patterns in the data—viz., that the distribution is strongly bimodal.
My point is this: Boxplots are much better than your run-of-the-mill barplots showing the group means, but be willing to look for alternatives when you need to—or ought to—highlight particular aspects of the data.
Tutorial: Drawing boxplots in ggplot2
What you’ll need
- The free program R,
the graphical user interface RStudio, and the add-on package
ggplot2. See my previous post when you need help with this.
- A dataset. For this tutorial we’re going to work with another dataset on receptive multilingualism that you can download to your hard disk. The dataset contains three variables; what interests us is whether the proportion of responses to Dutch words with the digraph ij (
PropCorrect) differs according the
LearningConditionto which the participants were assigned.
In RStudio, read in the data.
If the summary looks like this, you’re good to go.
Now load the
ggplot2 package we’ll be using.
A first attempt
The first three lines of code specify
the data frame that the data are to be read from
and the variables that go on the x- and y-axis.
LearningCondition as the x-variable,
we make it clear that we want to compare the
accuracy data between these groups of participants.
The fourth line specifies that these data
should be rendered as boxplots.
This first attempt already produces a very presentable result. We would eventually like to label the axes more appropriately and get rid of the grey background, but all in all, this is pretty decent.
Adding the individual data points
One more substantial thing we can add to the graph is the individual data points that the boxplots are based on. You don’t have to do this, but particularly when the number of observations is fairly small and the data aren’t too coarse, it may be interesting to see how the data are distributed within the boxes and whiskers.
To add the individual data points to the boxplots, simply add a
geom_point() layer to the previous code.
I’ve specified that the points should be grey circles, but you can simply use
geom_point() instead of the fifth line.
Now, some participants had the same number of correct responses, but from the graph you can’t tell which ones: the points are just plotted on top of each other.
To remedy this, we can ‘jitter’ the position of the data points using the
Note that I set the
height parameter to 0 as I don’t want to render proportions of, say, 0.24 as 0.28; I just want to spread apart the data points horizontally:
The final product
You’ll notice that in the plot above, it appears as though three ‘oe’ participants have scores of around 62%, whereas in actual fact only two do:
we plotted a symbol to represent the outliers when drawing the boxplots and we drew the data points individually.
When you’re plotting the individual data points anyway,
there’s no need to also draw the outliers for the boxplots;
you can turn this off by specifying
outlier.shape = NA in the
Second, the y-axis should be properly labelled,
whereas the label for the x-axis seems to be superfluous
(the information is already supplied in the tick labels). Change this using the
Third, personally I prefer white backgrounds. Simply adding
theme_bw() to the call overrides the grey default.
theme_bw(8) means that the font size will be slightly smaller, which is okay.
Lastly, we can flip the x- and y-axes using
coord_flip(). The main advantage here is that doing so saves some vertical space on the page, which means there’s more room for other graphs!
The cowplot package
Lastly, I want to draw your attention to the
cowplot package, which among other things is useful when you want to draw a plot containing several panels.