Skip to content
Invisible link to canonical for Microformats

Wikipedia maps (I): Organ donor rates

A choropleth map with R



This is a quick post on how to create a map according to the Wikipedia conventions. In this case, I have chosen to plot the international organ donor rates, retrieved on February 10, 2019, although the data refers to 2017 (source: IRODaT).

Historical example: The mapping examples use cartography, which is now in maintenance mode. Its maintainers recommend mapsf for new work. The original code and outputs are preserved and have not been revalidated against current package versions.

Web scraping

The first step is to scrape Wikipedia in order to get the final table. To do so, I will use the rvest package. You can get the xpath you want to scrape as explained here.

library(rvest)
library(dplyr)

Base <-
  read_html("https://en.wikipedia.org/wiki/International_organ_donor_rates") %>%
  html_nodes(xpath = '//*[@id="mw-content-text"]/div/table[3]') %>%
  html_table() %>%
  as.data.frame(
    stringsAsFactors = F,
    fix.empty.names = F
  ) %>%
  select(Country,
    RateDonperMill = Number.of.deceased.donors..per.million.of.population
  )

knitr::kable(head(Base, 10), format = "markdown")
Country RateDonperMill
Argentina 19.60
Armenia 0.00
Australia 21.60
Austria 23.88
Azerbaijan 0.00
Bahrain 4.00
Bangladesh 0.00
Belarus 26.20
Belgium 30.30
Bolivia 0.36

Now we need to choose a good source of maps:

library(sf)
library(giscoR)

# Map import from Eurostat
WorldMap <- gisco_get_countries(resolution = 3, epsg = 3857) %>%
  select(ISO_3166_3 = ISO3_CODE)

Merging everything together

Now let’s join and have a look to see what is going on. We use the countrycode package to retrieve the ISO3 codes of our scraped dataset:

library(countrycode)

Base$ISO_3166_3 <- countrycode(Base$Country, origin = "country.name", destination = "iso3c")


DonorRate <- left_join(
  WorldMap,
  Base
) %>%
  select(
    Country,
    ISO_3166_3,
    RateDonperMill
  )

Make the .svg file

As already explained, I would like to follow the Wikipedia conventions, so there are some things to bear in mind:

  • Obviously, the colors. Wikipedia already provides good guidance for this. I will use the RColorBrewer library, which implements ColorBrewer in R.
  • In terms of projection, Wikipedia recommends the Equirectangular projection but, as in their own sample of a gradient map, I will use the Robinson projection.
  • I should also produce an .svg file following the naming convention.

Some libraries remain to be used: RColorBrewer, rsvg and, especially, one of my favorites, cartography:

library(RColorBrewer)
library(cartography)
library(rsvg)

# Create bbox of the world
bbox <- st_linestring(rbind(
  c(-180, 90),
  c(180, 90),
  c(180, -90),
  c(-180, -90),
  c(-180, 90)
)) %>%
  st_segmentize(5) %>%
  st_cast("POLYGON") %>%
  st_sfc(crs = 4326) %>%
  st_transform(crs = "+proj=robin")

# Create SVG
svg(
  "Organ donor rate per million by country gradient map (2017).svg",
  pointsize = 90,
  width =  1600 / 90,
  height = 728 / 90
)

par(mar = c(0.5, 0, 0, 0))
choroLayer(
  DonorRate %>% st_transform("+proj=robin"),
  var = "RateDonperMill",
  breaks = c(0, 5, 10, 20, 30, 40, 50),
  col = brewer.pal(6, "PuBu"),
  border = "#646464",
  lwd = 0.1,
  colNA = "#E0E0E0",
  legend.pos = "left",
  legend.title.txt = "",
  legend.values.cex = 0.25
)

# Bounding box
plot(bbox,
  add = T,
  border = "#646464",
  lwd = 0.2
)

dev.off()

And that’s all. Our .svg file is ready to be included in Wikipedia.

World choropleth map of organ donors per million people in 2017. Darker blue indicates higher donor rates and gray indicates missing data.

Update: The map is already part of the Wikipedia article.

Mentions and replies

Conversations from across the web.

Shared your thoughts on this post? Drop your link here!

Publish a page that links to this post, then submit its URL. Webmention.io will check the link before the mention appears.


Related posts