Creating maps

One can use a map to visualize the geographical relationship of spatial data. Here, we introduce how to create a map from a shapefile with ggplot2. Moreover, we introduce how to use ggmap to download map data from an online mapping service.

Getting ready

Ensure you have installed and loaded ggplot2 into your R session. Please download all files from the following GitHub link folder:

https://github.com/ywchiu/rcookbook/tree/master/chapter7

How to do it…

Perform the following steps to create a map with ggmap:

  1. First, load the ggmap and maptools libraries into an R session:
    > install.packages("ggmap")
    > install.packages("maptools")
    > library(ggmap)
    > library(maptools)
    
  2. We can now read the .shp file with the readShapeSpatial function:
    > nyc.shp ...

Get R for Data Science Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.