Crafting choropleth maps using ggplot2
Choropleths are thematic maps, usually colored according to a third continuous variable. This recipe demonstrates how to brew these using ggplot2
. This recipe crafts a choropleth displaying the 1985 USA states' gross product (GSP). The way I see it--there are at least four important things to check out from this recipe:
- How to use ggplot2 to get map data
- How to merge map data (coordinates and stuff) and other data in order to plot
- How to use polygons and colors to draw the map and make a choropleth
- How to manipulate the guide color bar
To the job!
Getting ready
Besides ggplot2
, we need some data coming from Ecdat
, and dplyr
is used to manipulate data. Make sure to have both by running the following:
> if( !require(Ecdat)){ install.packages('Ecdat')} > if( !require(dplyr)){ install.packages('dplyr')}
Let's roll.
How to do it...
Let us start with the choropleth:
ggplot2
has a function calledmap_data()
, which is just what we need to a create data frame with...