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")
Upload map file
kml_data <- st_read("C:/Users/ku26/Downloads/Untitled layer.kml")
## Reading layer `Untitled layer' from data source
## `C:\Users\ku26\Downloads\Untitled layer.kml' using driver `KML'
## Simple feature collection with 4 features and 2 fields
## Geometry type: POINT
## Dimension: XYZ
## Bounding box: xmin: -122.4263 ymin: 47.4968 xmax: -122.256 ymax: 47.78961
## z_range: zmin: 0 zmax: 0
## Geodetic CRS: WGS 84
print(kml_data)
## Simple feature collection with 4 features and 2 fields
## Geometry type: POINT
## Dimension: XYZ
## Bounding box: xmin: -122.4263 ymin: 47.4968 xmax: -122.256 ymax: 47.78961
## z_range: zmin: 0 zmax: 0
## Geodetic CRS: WGS 84
## Name Description geometry
## 1 Point 1 POINT Z (-122.4256 47.77719 0)
## 2 Point 2 POINT Z (-122.4263 47.4968 0)
## 3 Point 3 POINT Z (-122.256 47.51257 0)
## 4 Point 6 POINT Z (-122.2811 47.78961 0)
Register your Google Maps API key only once per R session
register_google(key = "AIzaSyBEmxGepih-PKBOyvrkw1MkbbkcZzP5Q8I")
Define map center from your KML file
bbox <- st_bbox(kml_data)
center_lon <- mean(c(bbox["xmin"], bbox["xmax"]))
center_lat <- mean(c(bbox["ymin"], bbox["ymax"]))
Get your basemap using Google
seattle_map <- get_map(location = c(lon = center_lon, lat = center_lat),
zoom = 12, source = "google", maptype = "roadmap")
## ℹ <https://maps.googleapis.com/maps/api/staticmap?center=47.643203,-122.341136&zoom=12&size=640x640&scale=2&maptype=roadmap&language=en-EN&key=xxx-PKBOyvrkw1MkbbkcZzP5Q8I>
Seattle_property <- Seattle %>%
filter(offense_against == "property")
Create ggmap plot and overlay KML + Seattle points
ggmap(seattle_map) +
geom_sf(data = kml_data, inherit.aes = FALSE, color = "red", size = 2) +
geom_point(data = Seattle, aes(x = longitude, y = latitude, color = offense_against),
alpha = 0.7, size = 2) +
theme_minimal() +
labs(title = "Seattle Crime Data on Google Map",
x = "Longitude", y = "Latitude")
## Coordinate system already present. Adding new coordinate system, which will
## replace the existing one.
## Warning: Removed 162 rows containing missing values or values outside the scale range
## (`geom_point()`).
Create a different map that only shows property crimes
ggmap(seattle_map) +
geom_sf(data = kml_data, inherit.aes = FALSE, color = "red", size = 2) +
geom_point(data = Seattle_property, aes(x = longitude, y = latitude, color = offense_against),
alpha = 0.7, size = 2) +
theme_minimal() +
labs(title = "Seattle Crime Data on Google Map",
x = "Longitude", y = "Latitude")
## Coordinate system already present. Adding new coordinate system, which will
## replace the existing one.
## Warning: Removed 131 rows containing missing values or values outside the scale range
## (`geom_point()`).