Skip to content

Instantly share code, notes, and snippets.

@rudeboybert
Last active November 8, 2018 17:20
Show Gist options
  • Save rudeboybert/4db453b0af6e4c7d60f00506250011e5 to your computer and use it in GitHub Desktop.
Save rudeboybert/4db453b0af6e4c7d60f00506250011e5 to your computer and use it in GitHub Desktop.
Clinton vs Trump 2016 Campaign Stops
library(tidyverse)
library(fivethirtyeight)
library(maps)
# View Clinton vs Trump 2016 campaign stops data. Based on
# https://fivethirtyeight.com/features/the-last-10-weeks-of-2016-campaign-stops-in-one-handy-gif/
View(pres_2016_trail)
# Load US state data. See ?map_data() for other options
us_state_map <- map_data("state") %>%
as_data_frame()
View(us_state_map)
# 1. Plot map of campaign stops
ggplot(pres_2016_trail, aes(x = lng, y = lat)) +
geom_path(data = us_state_map, aes(x = long, y = lat, group = group), size = 0.1) +
geom_point(aes(col = candidate), size = 3, alpha = 0.3) +
scale_color_manual(values = c("blue", "red")) +
coord_map() +
facet_wrap(~candidate)
# 2. Interactive map from leaflet page on markers described here:
# https://rstudio.github.io/leaflet/markers.html
library(leaflet)
data(quakes)
quakes <- quakes %>%
as_data_frame() %>%
slice(1:20)
leaflet(data = quakes) %>%
addTiles() %>%
addCircleMarkers(
~long, ~lat, popup = ~as.character(mag), label = ~as.character(mag)
)
# 3. Make an interactive map of Clinton vs Trump campaign stops with
# blue marking Clinton and red marking Trump.
# Define a new color palette:
pal <- colorFactor(c("blue", "red"), domain = c("Clinton", "Trump"))
# Plot interactive map
leaflet(data = pres_2016_trail) %>%
addTiles() %>%
addCircleMarkers(
~lng, ~lat, popup = ~location, label = ~location,
# Map our color palette to the variable candidate
color = ~pal(candidate),
stroke = FALSE, fillOpacity = 0.5
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment