Chapter 41 Different Ways of Plotting U.S. Map in R

Zhiyi Guo and Fan Wu

41.1 Introduction

When dealing with national data/geographical data, i.e., election results, it is often useful to visualize the data onto a map as it could help draw conclusion geographycally. Hence, in this tutorial, we will explore different packages that could help with mapping geographical data, specifically for the U.S.

41.2 Using usmap package

This is probabily the most convinent package to plot a U.S. map along with some data. usmap provides very helpful functions to select certain regions within the U.S.. Another feature of this package is that it creates a ggplot object and hence we could use all the nice functions that come with ggplot2 package.

Package(s) we need:

Plot all states of the U.S. to create an empty map.

Besides states, we could also plot all counties of the U.S.

usmap also provides many different regions to select, for example, the South region. The “include” and “exclude” functions give us freedom to select/delete regions as we desire, and this works for states and counties.

You could also select certain states using factors(state abbreviations).

The complete list of regions to choose is the following: .east_north_central: East North Central census division .east_south_central: East South Central census division .midwest_region: Midwest census region .mid_atlantic: Mid-Atlantic census division .mountain: Mountain census division .new_england: New England census division .northeast_region: Northeast census region .north_central_region: North-Central census region .pacific: Pacific census division .south_atlantic: South Atlantic census division .south_region: South census region .west_north_central: West North Central census division .west_region: West census region .west_south_central: West South Central census division usmap really provides users with the choice to mix and match areas of interest, hence makes it very desirable to manipulate the map.

The package comes with geographical/national data sets such as population, poverty, and etc. For example, using countypov data set from the package, we could see the poverty estimates for New England counties in 2014:

We could also see population of these counties in 2015:

41.3 Using ggplot2 package

(Note: In this part, we only care about continental U.S.)

First, we get U.S. map data. In R, there are packages named maps and mapdata which save a lot of map information, for instance, continents, countries and states. We can use their data directly by using map_data function in ggplot2 package.

Package(s) we need:

Next, we use geom_ploygon function to plot U.S. map.

In order to get more information about U.S., now we can add state data to the map. Like above, we obtain data from maps and mapdata packages, and use ggplot2 to plot.

What if we only care about one state? Definitely, we can draw one state map using ggplot2. Firstly we need to filter data.

41.4 Using maps package

(Note: In this part, we only care about continental U.S.)

We also can directly plot using maps package. However, with only maps package, we are limited by mapping techniques. Thus, we recommend using other packages instead.

Package(s) we need:

41.5 Using plotly package

We first need to import data from US-State-Boundaries-Census-2014.shp, and then use plotly package. Suppose we care about water level for each state in U.S.

Package(s) we need:

41.6 Using mapview package

We use the same dataset from last method. It gives us an interactive map with all information.

Package(s) we need:

41.7 Using leaflet package

We use the same dataset from last method. We construct a interactive map with no specific information. If you are interested in a specific area, for example, water level, you can add specific choropleth to show more information.

Package(s) we need: