When the president of the parliament states that there are some MPs (Members of Parliament) “doing nothing,” you know what to do as a data journalist: you turn to the numbers. This is how I did that and how I got a scatter plot in a printed paper and an interactive one online.
I knew that the Flemish parliament has a strong open data policy and publishes all parliamentary activities of the members of parliament, so I decided to check out their API. But the API proved to be a bit difficult for me:
- To get the info I was interested in, I had to make a lot of API calls, store the results and make a lot of other API calls.
- The response file formats are json and xml. I don’t have a lot of experience getting data out of these formats and this proved to be challenging.
After a while I gave up on the API, the xml’s and the json’s, and I decided to just scrape the website instead. Luckily, the website is very well structured and contains all the information I wanted in a very structured way.
I used the rvest R package for scraping. I took some time in the summer to learn R and some of its useful packages. I’m very glad I did that: it is paying off already.
What the scraper does (you can find all the code at the bottom of this page):
- It visits the page where all the MPs are listed and stores their names, the party they belong to and the urls of their personal profile pages.
- It then goes to all the profile pages and collects the urls to the pages where the activity of the MPs are listed (questions they asked, things they said in parliament and proposals they made).
- It then changes a parameter in these url’s to filter out the activity of only the current term.
- It visits the urls with the filter and gets the number of activities listed on these pages.
Fairly simple, all in all. I wasted much more time trying to collect the data with the API then writing the html scraper.
I decided to analyze two measures: how much an MP said something in parliament and how many official documents (proposals, amendments, …) they filed. An obvious choice then was to make a scatter plot. I used ggplot2, another great R package I learned to work with, to do that.
Key was to add lines for both medians. This divides the plot into 4 quadrants and I used these quadrants to classify the MPs as Busy Bees (a lot of interventions in parliament, a lot of documents filed), Silent Workers (few interventions, lot of docs), Chatterers (lot of interventions, few docs) and the Passive MPs (few interventions and few docs).
This added layer of classification, both in the story and in the graphic, proved to be the sugar to let the dry graphic that a scatter plot is to a lot of people (not to me!) go down. Without it, I don’t think I could have convinced the editors to run the graphic and I think a lot of people would have a harder time getting the chart.
For print, I generated the scatter plot with ggplot2 and exported it as a pdf. Further processing for print (which involved the manual placement of the overlapping labels) was done by my colleague Filip Ysenbaert, while reporting was done by 2 colleagues of the politics desk.
For the online version, I used D3 to make a scatter plot with buttons for highlighting and for zooming in on the ‘passive’ zone of the plot. Details of every MP are shown on hover/tap.
As I wrote already: learning R payed off. And not only for getting the data and visualizing it: I now have an R script (see below) that I can run by clicking a button and it will get all the data, put it in the right format, visualize it and prepare the data for the interactive scatter plot. No tedious manual editing anymore!
I actually edited the script and ran it on Friday morning (the graphic was published on Saturday). Getting new data while I still had a lot of work to do for publishing was something I would have never done if there were some manual steps involved in the data gathering and processing.
Bonus: Explaining the Median
I always struggle to explain in words what the median means exactly. But graphically this is surprisingly easy: on the scatter plot half of the points are always above, below left and right of the black lines. Can’t be easier, I think.
An image preview of the code is included below. Click here to get access to the code on the original post and scroll to the bottom.