Skip to content

Instantly share code, notes, and snippets.

@ryanrosenberg
Created August 22, 2018 03:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanrosenberg/2b1a6b96c057f79b6d4b3135453ec41c to your computer and use it in GitHub Desktop.
Save ryanrosenberg/2b1a6b96c057f79b6d4b3135453ec41c to your computer and use it in GitHub Desktop.
code for making a map from the Tribune's trivia guide
library(tidyverse)
library(rvest)
library(leaflet)
library(mapsapi)
trivia <- "http://www.chicagotribune.com/redeye/culture/ct-redeye-do-trivia-chicago-bars-20180320-story.html"
bars <- read_html(trivia) %>%
html_nodes("a strong") %>%
html_text()
addresses <- read_html(trivia) %>%
html_nodes("p em") %>%
html_text()
slugs <- read_html(trivia) %>%
html_nodes("div p") %>%
html_text() %>%
as.tibble() %>%
filter(grepl("p\\.m\\.", value))
addresses <- addresses[1:109] %>%
str_split(",") %>%
pluck(1) %>%
unlist()
geocodes <- mp_geocode(paste0(addresses, ", Chicago, IL")) %>%
mp_get_points()
geocodes <- mp_get_points(geocodes)
maps <- tibble(bars = bars,
address = geocodes$address_google,
long = geocodes$pnt %>%
pluck(1) %>%
unlist(),
lat = geocodes$pnt %>%
pluck(2) %>%
unlist(),
day = c(rep("Monday", 11),
rep("Tuesday", 45),
rep("Wednesday", 34),
rep("Thursday", 15),
rep("Friday", 1),
rep("Sunday", 3))) %>%
mutate(label = paste0(bars, " -- ", day)) %>%
filter(day %in% c("Monday", "Tuesday", "Wednesday", "Thursday"))
days_ordered <- factor(c("Monday", "Tuesday", "Wednesday", "Thursday"), levels = c("Monday", "Tuesday", "Wednesday", "Thursday"))
pal <- colorFactor("viridis", domain = days_ordered)
leaflet(data = maps) %>%
setView(lng = -87.7, lat = 41.9, zoom = 10.7) %>%
addProviderTiles(providers$Stamen.Toner) %>%
addCircleMarkers(~long, ~lat, popup = ~bars, label = ~label, color = ~pal(day), fillOpacity = .8,
labelOptions = labelOptions(direction = "bottom",
style = list(
"font-family" = "Lato",
"box-shadow" = "3px 3px rgba(0,0,0,0.25)",
"font-size" = "12px",
"border-color" = "rgba(0,0,0,0.5)"
)))%>%
addLegend("bottomright",
pal = pal,
values = days_ordered,
title = "Day",
opacity = 1
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment