Ordering bars within their clumps in a bar chart by @ellis2013nz
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
A pleasant diversion
An unexpectedly pleasant diversion from the news during the COVID-19 pandemic has been the contemplation of a considerable range of data visualisations, from the truly excellent to the amusingly bad. After all, at the excellent end, the crisis has given us the Siouxsie Wiles and Toby Morris “flattening the curve” animated cartoon, the Matt Cowgill “days since 100 cases” chart, and some fantastic animations of infections through networks. Anything that leads to so much discussion of logarithmic scales, the correct usage of statistical adjustments, and even when to use a connected scatter plot has its upside. And at the other end, who could forget the crazy Fox 31 vertical axis and that infamous cubic fit with extrapolation beyond the data from the White House itself.
A not-so-good data visualisation
One chart doing the rounds is sourced from the US state Georgia’s Department of Public Health. It is a bar chart of daily cases over time in Georgia’s five counties with most states. This blog post by Andrew Gelman (and follow links from there to the original and to other discussions) does not claim to be the first place it was critiqued but has some of the liveliest discussion. There’s some some genuine debate in the comments about how it came about – whether there is deliberate deception involved or just a bad judgement.
Here’s my re-creation of the chart in R, complete with colours and styling to match the original created in SAS VA (note that my data – sourced from the New York Times – is quite different to that in the original chart for reasons unknown to me, but that doesn’t seem to be important for illustrative purposes):
As a visualisation at least three things are wrong:
- dodged bar charts are rarely effective for making comparisons over time – it’s difficult for the eye to follow;
- within each day’s clump of bars, the counties are in a different order (highest to lowest, within the clump), reducing the meaning in the pattern in each clump;
- the daily clumps of bars are not in chronological order.
Side issue – what is the correct word for what I am calling a “clump” of bars?
In my judgement, I’ve listed the worst mistake first; the other two seem to me secondary. Once you’ve decided not to use a line chart for this dataset, you’ve immediately lost the most powerful leverage a visulaisation can give you. This bar chart is never going to be much better than a simple table of numbers, no matter what sequence the bars are in.
Most critiques have focused on the third of the faults listed above, and assumed malicious intent to make it look like cases are going down faster than they are. However, I can easily see this error being made without thinking.
More often than not, the best thing to do visually with the categories in a bar or column chart is to order them by the values, so the reader can scan down or across the axis labels and get some information without even looking across at the bars. It’s certainly much better than leaving the categories in (for example) alphabetical order. The convention that time series data goes from left to right should trump this rule about reordering your bars, but it’s easy to imagine making a misjudgement there.
The second of the errors I found the most surprising – the counties appearing in a different order in each daily clump of bars. Like a few others I thought “how could you even do that?” But in my chart above I found it was actually quite easy, thanks to tools developed by the tidytext
community to better present words in facets (for example, words associated with different topics as a result of topic modelling).
Reordering your bars within each clump in a bar chart is easy with tidytext::reorder_within()
Here’s the code to download county-level data from the New York Times and draw my re-creation of this bad chart. The trick is to have one variable county
ordered by number of new cases overall, and use this for the fill and stroke colours; and a second variable county2
that is reordered by cases within date
. Of course, date
itself has to be converted into a character string and then a factor for its own reordering (“mistake 3” in my original enumeration).
Note the trick here is to force geom_bar()
to use the county2
variable for its grouping, while still leaving county
to determine the stroke and fill colours. So, I’d say it is easy to do this with R. But I don’t think you’d be likely to do it by mistake.
However, I’m still on the side of the original being a mistake rather than malign in intent. I think someone clicked the wrong button in SAS VA. Going up a level, I’m certain that the original sin in this case – the decision to use a bar chart rather than a line chart – was just a poor choice, not a conspiracy against the public.
A better visualisation
I couldn’t leave this dataset without trying to visualise it properly. It turns out a single line chart is pretty messy and difficult to read. However, small multiples work nicely. With judicious use of layers, we can even have both the original data (as points) and a meaningful smoothing line (but not a projection forwards!) to help the eye:
With the appropriate chart for this sort of data, we can also show a meaningfully longer period of time too – a real problem with the bar chart.
Here’s the R code to produce that chart, drawing on the same dataset (but without filtering it to just a few weeks of data) prepared earlier.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.