Skip to content

Instantly share code, notes, and snippets.

@ManuelNeumann
Last active October 25, 2023 09:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ManuelNeumann/728b674d06ca7022773b601b46740d94 to your computer and use it in GitHub Desktop.
Save ManuelNeumann/728b674d06ca7022773b601b46740d94 to your computer and use it in GitHub Desktop.
How to make a map with ggmap, ggplot2, and a shape file
library(pacman)
p_load(tidyverse)
p_load(broom)
p_load(ggmap)
p_load(rgdal)
# For map behind plot ----
# Longitude and latidue
mannheim_longlat <- c(left = 8.38, bottom = 49.4, right = 8.62, top = 49.6)
# Download map from maps.stamen.com
mannmap <- get_stamenmap(mannheim_longlat, zoom = 13, maptype = "terrain")
# Read shape file ----
mannheim_shp <- readOGR("data/mannheim.shp", use_iconv = TRUE, encoding = "UTF-8")
# Tidy shape file
mannheim_tidy <- tidy(mannheim_shp)
# ggplot
ggmap(mannmap) +
geom_polygon(data = mannheim_tidy,
aes(x = long, y = lat, group = group),
color = "darkgreen",
fill = "green",
alpha = 0.3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment