The ggmap package allows plotting geographic data in R over tile maps. In these examples we will use two data sets we have used before - the sample of 311 reports, as a csv with geographic coordinates (lat, lon), and a shapefile of open spaces in Boston. Make sure that whenever you work with multiple spatial data sets, make sure they have the same coordinate format. Tilemaps usually demand a lat lon WGS84 format (crs number is 4326)
#install.packages("ggmap")
library(tidyverse)
# load 311 reports
bos311 <- read_csv("data/vector/point/311.csv")
# calculate the bounding box of the submitted reports
extents <- bos311 %>% summarise(left = min(longitude), bottom = min(latitude), right = max(longitude), top = max(latitude))
# load a background map for the bounding box. note: ggmpap requires a numeric vector, not a data frame, hence as.numeric()
library(ggmap)
bos <- get_stamenmap(bbox = as.numeric(extents), maptype = "toner-lite", zoom = 13)
ggmap(bos) + geom_point(data = bos311, aes(x = longitude, y = latitude, color = subject), alpha = 0.6)
ggmap(bos) + geom_point(data = bos311, aes(x = longitude, y = latitude, color = subject), alpha = 0.6) + facet_wrap(~subject)
We can also use ggmap to plot polygons from shapefiles. The sf (simple feature) package includes tidyverse-friendly functions to work with shapefiles in R and ggplot.
#install.packages("sf")
library(sf)
openspace <- st_read("data/vector/polygon/Open_Space/")
## Reading layer `Open_Space' from data source `/Users/dietmar/Downloads/data - maps/data/vector/polygon/Open_Space' using driver `ESRI Shapefile'
## Simple feature collection with 999 features and 20 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -71.19091 ymin: 42.22789 xmax: -70.87486 ymax: 42.39653
## Geodetic CRS: WGS 84
# size = 0 refers to the thickness of the border
ggplot() + geom_sf(data = openspace, fill = "red", size = 0)
# when combining with ggmap, use inherit.aes = FALSE, because ggmap expects columns called "lat" and "lon" and your coordianates might be in different columns
ggmap(bos) + geom_sf(data = openspace, fill = "red", size = 0, alpha = 0.5, inherit.aes = FALSE)
The tmap package is an alternative to ggmap and ggplot, which offers more GIS-like functionality. It uses a similar logic and grammar than ggplot.
library(tmap)
tm_shape(openspace) + tm_graticules(col = "grey80") + tm_fill(col = "red", alpha = 0.3) + tm_borders(col = "red")
tm_shape() loads a new shape object, which could be a polygon, line, or point file. Before we can use our bos311 data frame, we have to convert it into a GIS-compatible format, here by just casting it into a simple feature object (st_as_sf). We specify the WGS84 coordinate system
# this converts our data frame into a sf data frame (we specify the WGS84 coordinate system, crs = 4326)
bossf <- st_as_sf(bos311, coords = c("longitude", "latitude"), crs = 4326)
tm_shape(openspace) + tm_fill(alpha = 0.7) + tm_shape(bossf) + tm_dots(col = "subject", size = 0.1, alpha = 0.8)
In the last map we notice that not many reports fall into open spaces. Lets find out which reports do. In order to do that, we need to join the points with the polygon.
inter <- st_join(bossf, openspace, join = st_within)
# count number of points in polygons & merge with open space
c <- count(as_tibble(inter), OBJECTID, name = "count")
open.r <- inner_join(openspace,c)
# plot only open spaces that contain points
tm_shape(open.r) + tm_fill(col = "count", palette = "Blues")
We also can create interactive maps by switching tmap to view mode
tmap_mode("view")
tm_shape(openspace) + tm_fill(alpha = 0.7) + tm_shape(inter) + tm_dots(col = "subject", size = 0.2, alpha = 0.8)
# to turn it off
tmap_mode("plot")