Rebecca Ferrell
May 25, 2016
ggmap
for mashing up maps with ggplot2
ggrepel
ggmap
is a package that goes with ggplot2
so that you can plot spatial data directly onto map images downloaded from Google Maps, OpenStreetMap, and Stamen Maps.
What this package does for you:
get_map
) at the location and scale you wantggplot
objectggplot
layers like points, 2D density plots, text annotationsIn Week 5, we looked at types of incidents the Seattle Police Department responded to in a single day. Now, we'll look at where those were.
library(ggplot2); library(ggmap); library(readr); library(dplyr)
spd_raw <- read_csv("https://raw.githubusercontent.com/rebeccaferrell/CSSS508/master/Seattle_Police_Department_911_Incident_Response.csv")
qmplot
will automatically set the map region based on your data:
qmplot(data = spd_raw, x = Longitude, y = Latitude, color = I("firebrick"), alpha = I(0.5))
qmap(location = "mary gates hall university of washington", zoom = 15, maptype = "watercolor", source = "stamen")
Both qmplot
and qmap
are wrappers for a function called get_map
that retrieves a base map layer. Some options:
location =
search query or numeric vector of longitude and latitudezoom =
a zoom level (3 = continent, 10 = city, 21 = building)source =
"google"
, "osm"
, "stamen"
maptype =
"terrain"
, "terrain-background"
, "satellite"
, "roadmap"
, "hybrid"
"watercolor"
, "toner"
, "toner-background"
, "toner-lite"
color =
"color"
or "bw"
qmap(location = "pike place market", zoom = 14, maptype = "toner-background", source = "stamen")
qmap(location = "seattle", zoom = 8, maptype = "terrain", source = "google")
Let's look at locations of incidents near downtown.
# query the map server
downtown_map <- get_map(location = "pike place market", zoom = 14)
# grab the bounding box coordinate data frame
downtown_bb <- attributes(downtown_map)[["bb"]]
# subset the data based on bounding box
downtown_seattle_incidents <- spd_raw %>%
filter(downtown_bb[["ll.lat"]] <= Latitude &
Latitude <= downtown_bb[["ur.lat"]] &
downtown_bb[["ll.lon"]] <= Longitude &
Longitude <= downtown_bb[["ur.lon"]])
Call qmplot
with no geom
, and then add density layers:
qmplot(data = downtown_seattle_incidents, geom = "blank", x = Longitude, y = Latitude, maptype = "toner-lite", darken = 0.5) + stat_density_2d(aes(fill = ..level..), geom = "polygon", alpha = .2, color = NA) + scale_fill_gradient2("Incident concentration", low = "white", mid = "yellow", high = "red")
Let's label the assaults and robberies specifically in downtown:
assaults <- downtown_seattle_incidents %>% mutate(assault_label = ifelse(`Event Clearance Group` %in% c("ASSAULTS", "ROBBERY"), `Event Clearance Description`, "")) %>% filter(assault_label != "")
Now let's plot the events and label these specifically using geom_label
(geom_text
also works without the background/border):
qmplot(data = downtown_seattle_incidents, x = Longitude, y = Latitude, maptype = "toner-lite", color = I("firebrick"), alpha = I(0.5)) + geom_label(data = assaults, aes(label = assault_label))
You can also try geom_label_repel
or geom_text_repel
if you install and load in the ggrepel
package to fix overlaps:
library(ggrepel)
qmplot(data = downtown_seattle_incidents, x = Longitude, y = Latitude, maptype = "toner-lite", color = I("firebrick"), alpha = I(0.5)) + geom_label_repel(data = assaults, aes(label = assault_label), fill = "black", color = "white", segment.color = "black")
Use the Lab/HW 7 template to practice making maps of the restaurant inspection data. Save your work when you're done by emailing it to yourself, and complete and submit it next week as the final homework for this class.