How to Use R in DataWrapper: A Practical Example

Carmen Aguilar García
Feb 25 · 5 min read

A few months ago, I was asked to produce an animated spike map to show how the coronavirus-related deaths grew in the UK every fortnight in each local authority.

Sky News

The original idea was actually every week and it was commissioned before 2020 was ended. So, the complete dataset wasn’t yet available.

There were then three elements I needed to consider for this visualization: the type of visualization, the “effect of time” -to show cumulative numbers over time- and the yet uncomplete dataset.

Spike symbol

This is how a UK map by district level (Lower Tier Local Authority) looks like for anyone not familiarised with the UK geography.

DataWrapper base map

I’m not going into details on how the (complex) geographical system in the UK works, but just looking at that map, you can see there are many tiny units concentrated around London, Birmingham, Liverpool/Manchester, Cardiff and Glasgow/Edinburgh. Along with Belfast, these are the main cities in the UK, highly densely populated, and many of them with a high COVID-19 death rate.

If using a bubble map, many of the bubbles would overlap, especially on mobile. The circles in all these tiny divisions would form an almost uniform patch and it would be difficult to distinguish patterns or to see any increase over time.

Instead of area, I considered two other methods of encoding (Cairo, A. 2019, “How charts lie”) that I could use. One is colour, producing a choropleth map, but height/length was clearer to me to compare among areas and weeks.

And it seems it wasn’t only clear to me. DataWrapper explained here that humans are better at comparing different lengths than areas. So, it sounded that this commission was the perfect moment to have a go with the new symbol (spikes) Datawrapper had released a few months before.

One map per week/fortnight

There is only one way of transforming a map in DataWrapper into an animated Gif: creating each map, exporting it as an image and then putting all the pictures together to make the Gif with a video editor or one of the free tools.

But I had around 50 different versions of the same map to produce, and I do hate manual processes

Here is when Benedict Witzenberger, a data journalist at Süddeutsche Zeitung, and his DatawRappr R package came to the rescue.

This package uses DataWrapper’s API to connect data from R directly with your chart/map in DataWrapper.

I am sure there must be other ways of achieving this, but below is my “how I did it.” An f step-by-step guide to avoid manually updating the same visualisation.

1. In your browser, go to Datawrapper and create the map/chart. It might be possible to create the visualisation using the DatawRappr library, but I found it easier and more intuitive using directly Datawrapper.

TIP: if doing a similar visualisation to my spikes map, it was easier to create the template with the latest week of data. Why? Because the highest spike corresponds to the highest value, and that was in the last week as my data showed cumulative rates.

2. Copy the ID of your visualisation. This is normally in the URL between /. Save it somewhere for later.

3. Go to Settings>API Tokens to create your API Access Token. Copy it and save it as well for later.

4. Now, open R. You should structure your data first, as you would normally do before pasting it in Datawrapper. In my case, I had five columns: Latitude, Longitude, AreaName, Week, death_rate.

For each week I added an extra row called “Out”, with a latitude and longitude close to Japan, and with the highest value across the whole dataset.

Why?

Do you remember point 1?

The highest spike corresponds to the highest value in your data. But you don’t upload your complete dataset in Datawrapper at once, but week per week. So, what you upload is a subset of your data. If the highest value in one subset (e.g. week 1) is 5 and the highest value of another subset (e.g. week 11) is 60, both numbers will correspond to the highest length of the spike.

That is why I hid in Japan the reference to the highest value consistent across all weeks. But it could have been placed anywhere outside the boundaries of my map.

This might sound a bit confusing, but when you see the code, you will understand it better.

5. Write the code to loop through your dataset and produce a map per week.

library(DatawRappr)
library(tidyverse)
key=datawrapper_auth(api_key = “ADD YOUR KEY”)for (i in unique(YOUR_OBJECT$WEEK_COLUMN)){
dw_data_to_chart(YOUR_OBJECT %>% filter(WEEK_COLUMN==i),”DW_ID”)
image=dw_export_chart(“DW_ID”, type = “png”, unit = “px”, width = 2560, height = 1440, plain = TRUE, scale = 2, border_color = NULL,transparent=TRUE, api_key = “environment”)
magick::image_write(image, path = paste0(i, “desktop.png”), format = “png”)
}

Notice where you need to paste the ID of your visualisation (DW_ID ) and your API token. The arguments inside the dw_export_chart()function are quite self-explanatory and are similar to the settings in “Publish & Embed,” but read the explanation here and see other functions from this package here.

Uncomplete dataset

The goal of this project was to show the cumulative rate by area throughout the whole year. As we have just seen, the last week of data determines every week on the map. Hence, that of “have all the weeks prepared and ready to just add the latest weeks” wasn’t a realistic option.

The original plan was to publish this map a day or two after the data for the whole year was published, giving me little room for experiment or troubleshooting.

Solution?

Again R.

I wrote a script that:

  • uploaded datasets from the three statistical offices (this is another funny thing of working with local authorities’ data in the UK),
  • cleaned the files and structured them all in the same way (there is no consistency in the format and structure across these files -also funny),
  • combined all of them to have a unique object with all the local authorities and deaths per week,
  • added information about the population, latitude and longitude of each local authority to my combined file,
  • calculated rates and added a row per week with the highest rate across the complete dataset,
  • used this object in the for loop in the `dw_export_chart()` function.

When the final data was out, I “simply” had to run two sets of code -once to produce the object I needed for DatawRappr and the other one to create the images-, zip the pictures and send them to the designer who was producing the gif.

He then replaced all the images, exported the gif and voilà.

The Startup

Get smarter at building your thing. Join The Startup’s +786K followers.

By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Carmen Aguilar García

Written by

Data journalist. I learned coding, statistics, visualisation, and telling stories with data at @BCUMedia. Before, multimedia and TV reporter.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +786K followers.

Carmen Aguilar García

Written by

Data journalist. I learned coding, statistics, visualisation, and telling stories with data at @BCUMedia. Before, multimedia and TV reporter.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +786K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium