Today I will demonstrate how to easily create Administrative Level 1 Choropleth Maps in R. If you are new to R, you might want to first take a free introductory R course (such as this) before continuing.
To start, we need to install and load a few packages:
install.packages(c("choroplethr", "choroplethrAdmin1")) library(choroplethr) library(choroplethrAdmin1)
Choroplethr is a package for creating choropleth maps from shapefiles in R. The choroplethrAdmin1 package contains the Administrative Level 1 map from Natural Earth Data in a form that R can work with.
We can render this map in its entirety by using the ggplot2 library. ggplot2 is currently the most popular graphics library in R.
library(ggplot2) data(admin1.map) ggplot(admin1.map, aes(long, lat, group=group)) + geom_polygon()
Note that by default, ggplot2 renders the map using cartesian coordinates. That is, there is no projection applied.
When mixing programming with geography spelling becomes important. In many cases there is more than one way to romanize a foreign country’s name and administrative regions. But strings have to exactly match when programming.
To see how the map spells each country and its administrative levels, look at the dataset admin1.regions:
|5 afghanistan||faryab province|
You can get a list of all the countries as follows:
 "afghanistan"  "aland"  "albania"  "algeria" …
You can create an Admin Level 1 map of an individual country by passing its name to the function
A choropleth map, of course, requires data. This is where R excels. As an example, the choroplethrAdmin1 package comes with some demographic data from the Japanese Census Bureau:
data(df_japan_census) head(df_japan_census) region pop_2010 percent_pop_change_2005_2010 pop_density_km2_2010 23 aichi 7411000 2.2 1434.8 5 akita 1086000 -5.2 93.3 2 aomori 1373000 -4.4 142.4 12 chiba 6216000 2.6 1205.5 38 ehime 1431000 -2.5 252.1
choroplethr will merge the regions in this data frame with the map, and use the values in the value column when coloring the regions. Note that there is no value column here yet – let’s create it by setting it equal to the population density column:
df_japan_census$value = df_japan_census$pop_density_
km2_2010 admin1_choropleth("japan", df_japan_census)
Note that the admin1_choropleth function also applies a map projection to the country.
Exploratory Data Analysis
Choroplethr also allows us to perform exploratory data analysis using maps. As one example, let’s try to see if there are any outliers in the data, and if so, where they are. We can do this by changing the scale to be continuous. To do this, set the parameter num_colors to 1.
admin1_choropleth("japan", df_japan_census, num_colors=1)
Here two regions stand out as being particularly dense. But which regions are they? One way to find out is to overlay our choropleth map over a reference map from google maps. We can do this by setting the parameter reference_map=TRUE:
admin1_choropleth("japan", df_japan_census, num_colors=1, reference_map=TRUE)
Here we can see that the densest regions are Tokyo and Osaka.
To see the full list of options that the admin1_choropleth function supports, type ?admin1_choropleth. That will bring up help for the function.
If you would like to learn more about choroplethr, consider taking my new course: Mapmaking in R with Choroplethr. I also have a free course, Learn to Map Census Data in R, that talks about US Maps in R, and data from the US Census Bureaus