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.
Ensure you have installed and loaded ggplot2
into your R session. Please download all files from the following GitHub link folder:
Perform the following steps to create a map with ggmap
:
First, load the
ggmap
andmaptools
libraries into an R session:> install.packages("ggmap") > install.packages("maptools") > library(ggmap) > library(maptools)
We can now read the
.shp
file with thereadShapeSpatial
function:> nyc.shp <- readShapeSpatial("nycc.shp") > class(nyc.shp) [1] "SpatialPolygonsDataFrame" attr(,"package") [1] "sp"
At this point, we can plot the map with the
geom_polygon
function:> ggplot() + geom_polygon(data = nyc.shp, aes(x...