The new Leaflet package for R is an excellent way to make interactive maps quickly; I’ve written about the package elsewhere. I’ve been experimenting with the package a little more, and I’d like to share a function I’ve written to create an interactive Leaflet choropleth map with data from the World Bank and boundary files from Natural Earth in RStudio - in one line of R code!

The script requires three packages to be installed: rgdal, WDI, and leaflet. The first two can be installed from CRAN with install.packages, and the third can be installed from GitHub with devtools::install_github("rstudio/leaflet").

To get started making your map, first source my script from GitHub with the following command:


Alternatively, download the script yourself from GitHub.

This will give you access to the wdi_leaflet function that you can use to make your map. The function has five parameters:

  1. indicator (string, required): The World Bank code for the indicator you want to map; you can get this from
  2. indicator_alias (string, optional): Enter the text you want to display for the indicator in the popup. It defaults to “Value”.
  3. year (numeric, optional): Enter the year for which you want to make your map. The default is 2012.
  4. classes (numeric, optional): Enter the number of quantiles you want to use to bin your data. The function default is 5.
  5. colors (string, optional): This parameter takes the ColorBrewer code for the color scheme you’d like to use. The default is “Blues”.

As the function only has one required argument, we can run it as such. This command will create a map of countries shaded by the percent of their population that lives in urban areas; it will show in your RStudio viewer.