Install packages and load libraries
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.4.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(sf)
## Linking to GEOS 3.12.1, GDAL 3.8.4, PROJ 9.3.1; sf_use_s2() is TRUE
library(ggmap)
## Warning: package 'ggmap' was built under R version 4.4.3
## ℹ Google's Terms of Service: <https://mapsplatform.google.com>
## Stadia Maps' Terms of Service: <https://stadiamaps.com/terms-of-service>
## OpenStreetMap's Tile Usage Policy: <https://operations.osmfoundation.org/policies/tiles>
## ℹ Please cite ggmap if you use it! Use `citation("ggmap")` for details.
library(crimedata)
## Warning: package 'crimedata' was built under R version 4.4.3
Seattle <- get_crime_data(cities = "Seattle")
Register your Google Maps API key only once per R session
register_stadiamaps(key = "28ab02a4-bce9-4b7e-9287-bd601e6ca000")
Upload map file
bbox <- c(left = -122.45, bottom = 47.48, right = -122.22, top = 47.73)
get_stadiamap(bbox, zoom = 12, maptype = "stamen_toner_lite") %>% ggmap()
## ℹ © Stadia Maps © Stamen Design © OpenMapTiles © OpenStreetMap contributors.
seattle_map <- get_stadiamap(bbox = bbox,
zoom = 12,
maptype = "stamen_toner_lite")
## ℹ © Stadia Maps © Stamen Design © OpenMapTiles © OpenStreetMap contributors.
Seattle_property <- Seattle %>%
filter(offense_against == "property")
Create ggmap plot and overlay Stadia map + Seattle points
ggmap(seattle_map) +
geom_point(data = Seattle_property,
aes(x = longitude, y = latitude),
color = "red",
alpha = 0.7,
size = 2) +
labs(title = "Seattle Property Crime",
x = "Longitude",
y = "Latitude") +
theme_minimal()
## Warning: Removed 7 rows containing missing values or values outside the scale range
## (`geom_point()`).