Skip to content

Instantly share code, notes, and snippets.

@aaroncharlton
Last active January 26, 2023 23:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaroncharlton/04150c927ffa6b1f7f2bda0de4d2b8df to your computer and use it in GitHub Desktop.
Save aaroncharlton/04150c927ffa6b1f7f2bda0de4d2b8df to your computer and use it in GitHub Desktop.
library(tidyverse)
library(tidygeocoder)
library(ggmap)
library(viridis)
# import a dataset that has addresses in this format: "123 Royal Drive, Mesa, AZ 85206."
# If the address values are in separate columns (address, city, state, zip) you can use
# dplyr::mutate and paste to build the combined address:
# mydata <- mydata %>% mutate(address = paste0(add1,", ",city,", ",state," ",zip))
# first geocode your addresses (get latitude and longitude)
lat_longs <- mydata %>%
geocode(address, method = 'osm', lat = latitude , long = longitude)
# get the map--you'll want to adjust the coordinates (bbox) and zoom level
bbox <- c(left = -112.4, bottom = 33.15, right = -111.48, top = 33.8)
map_phx <- ggmap(get_stamenmap(bbox, zoom = 10))
# Overlay heatmap on top of the map
map_phx +
stat_density2d(data = lat_longs, aes(x = longitude, y = latitude, fill = after_stat(density)), geom = 'tile', contour = F, alpha = .5)+
scale_fill_viridis(option = 'inferno')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment