Today’s RSS feeds picked up this article by Marianne Sullivan, Chris Sellers, Leif Fredrickson, and Sarah Lamdanon on the woeful state of enforcement actions by the U.S. Environmental Protection Agency (EPA). While there has definitely been overreach by the EPA in the past the vast majority of its regulatory corpus is quite sane and has made Americans safer and healthier as a result. What’s happened to an EPA left in the hands of evil (yep, “evil”) in the past two years is beyond lamentable and we likely have two more years of lamenting ahead of us (unless you actually like your water with a coal ash chaser).
The authors of the article made this chart to show the stark contrast between 2017 and 2018 when it comes to regulatory actions for eight acts:
-
Clean Air Act (CAA)
-
Clean Water Act (CWA)
-
Emergency Planning and Community Right to Know Act (EPCRA)
-
Federal Insecticide, Fungicide, and Rodenticide Act (FIFRA)
-
Resource Conservation and Recovery Act (RCRA)
-
Safe Drinking Water Act (SDWA)
-
Toxic Substances Control Act (TSCA)
-
Comprehensive Environmental Response, Compensation, and Liability Act (CERCLA)
They made this arrow chart (via Datawrapper):
For some reason, that chart sparked a “I really need to make that in R” moment, and thus begat this post.
I’ve got a geom for dumbbell charts but that’s not going to work for this arrow chart since I really wanted to (mostly) reproduce it the way it was. Here’s my go at it.
Data First
Datawrapper embeds have a handy “Get the data” link in them but it’s not a link to a file. It’s a javascript-generated data:
href
so you either need to click on the link and download it or be hard-headed like I am go the way of pain and scrape it (reproducibility FTW). Let’s get packages and data gathering code out of the way. I’ll exposit a bit more about said data gathering after the code block:
1 |
|
Inside the main article URL content there’s an iframe
load:
1 |
|
We grab the contents of that iframe link (https://datawrapper.dwcdn.net/psm7n/2/
) which has a data:
line way down towards the bottom of one of the last javascript blocks:
That ugly line gets transformed into a link that will download as a normal CSV file, but we have to do the above wrangling on it before we can get it into a format we can work with.
Now, we can make the chart.
Chart Time!
Let’s get the Y axis in the right order:
1 |
|
Next, we setup X axis breaks and also get the max value for some positioning calculations (so we don’t hardcode values):
1 |
|
I have two minor nitpicks about the original chart (and changes to them as a result). First, I really don’t like the Y axis gridlines but I do believe we need something to help the eye move horizontally and associate each label to its respective geom. Instead of gridlines I opt for a diminutive dotted line from 0 to the first (min) value.
The second nitpick is that — while the chart has the act information in the caption area — the caption is in alpha order vs the order the act acronyms appear in the data. If it was an alpha bullet list I might not complain, but I chose to modify the order to fit the chart, which we build dynamically with the help of this vector:
1 |
|
Now, we can generate the geoms. It looks like alot of code, but I like to use newlines to help structure ggplot2 calls. I still miss my old gg <- gg +
idiom but RStudio makes it way too easy to execute the whole expression with just the use of +
so I’ve succumbed to their behaviour modification. To break it down w/o code, we essentially need:
-
the arrows for each act
-
the 2017 and 2018 direct label values for each act
-
the 2017 and 2018 top “titles”
-
segments for ^^
-
title, subtitle and caption(s)
We use percent-maths to position labels and other objects so the code can be re-used for other arrow plots (hardcoding to the data values is likely fine, but you’ll end up tweaking the numbers more and wasting ~2-5m per new chart).
1 |
|
Here’s the result:
(it even looks ok in “batman” mode):
FIN
With Microsoft owning GitHub I’m not using gists anymore and the GitLab “snippets” equivalent is just too dog-slow to use, so starting in 2019 I’m self-hosing contiguous R example code used in the blog posts. For the moment, that means links to plain R files but I may just setup gitea for them sometime before the end of Q1. You can find a contiguous, commented version of the above code in here.
If you do your own makeover don’t forget to drop a link to your creation(s) in the comments!
Related