library(tidyverse)
library(ggplot2)
library(ggthemes)
library(socviz)
library(maps)
library(mapproj)
library(questionr)
library(viridis)
library(leaflet)
library(tidycensus)lab 7
Movietavern and Population Overlay - With Bonus
ACQUIRE THE DATA
The following code will read the dataset in directly from data.world.
https://data.world/data-hut/movietavern-location-dataset
movieUS <- read.csv("https://query.data.world/s/5xsdaezg32gho5hy4xklnwjsbuyuis?dws=00000", header=TRUE, stringsAsFactors=FALSE)CREATE MAP FOR TEXAS LOCATIONS
Map Movietavern store locations JUST FOR in TX using leaflet package.
movieUS <- movieUS %>% filter(state == "TX")
movieUS %>% leaflet(width = "100%") %>%
addTiles() %>%
setView(-97.7431, 30.2672, zoom = 6) %>%
addMarkers(lat = ~latitude,
lng = ~longitude,
popup = movieUS$name)ACQUIRE ARKANSAS COUNTY-LEVEL POPULATION DATA FROM THE US CENSUS
Obtain your own census api key at: https://api.census.gov/data/key_signup.html
We will use the api key to directly download population data from the census.
OVERLAY MOVIETAVERN TO POPULATION
Let’s map the AR Population Census using the leaflet package.
This time we are using a different provider for the map: OpenStreetMap
Try swapping other maps – see a few below in the commented code.
MapPalette <- colorQuantile(palette = "viridis", domain = tx_pop$estimate, n = 20)
tx_pop %>%
st_transform(crs = "+proj=longlat +datum=WGS84") %>%
leaflet(width = "100%", height = 500) %>%
addProviderTiles(provider = "Esri.WorldStreetMap") %>%
addPolygons(popup = ~NAME,
stroke = FALSE,
smoothFactor = 0,
fillOpacity = 0.6,
color = ~ MapPalette(estimate)) %>%
addLegend("bottomright",
pal = MapPalette,
values = ~ estimate,
title = "Population Percentiles",
opacity = 1) %>%
addCircleMarkers(data = movieUS,
lat = movieUS$latitude,
lng = movieUS$longitude,
popup = movieUS$name,
weight = 1,
radius=4,
color = "blue",
opacity = 1)## Alternative maps (just swap out the above)
#addProviderTiles(provider = "Esri.WorldStreetMap") %>%
#addProviderTiles(provider = "OpenStreetMap") %>%
#addProviderTiles(provider = "Esri.WorldPhysical") %>%
#addProviderTiles(provider = "Esri.WorldImagery") %>%
#addProviderTiles(provider = "Esri.WorldTopoMap") %>% ADD ON STARBUCKS LOCATIONS
Note – modified code slightly in the addPolygons() function to outline each county border in gray
whataburgerUS <- read.csv("https://query.data.world/s/an4baowuctocogcc5l2dlwa3rl7pae?dws=00000", header=TRUE, stringsAsFactors=FALSE)
sbar <- whataburgerUS%>% filter(state=="TX")
rm(whataburgerUS)
tx_pop %>%
st_transform(crs = "+proj=longlat +datum=WGS84") %>%
leaflet(width = "100%", height = 500) %>%
addProviderTiles(provider = "Esri.WorldStreetMap") %>%
addPolygons(popup = ~NAME,
stroke = TRUE, # Enable stroke for the border
color = "gray", # Set border color to gray
weight = 1, # Set the thickness of the border
smoothFactor = 0,
fillOpacity = 0.6,
fillColor = ~ MapPalette(estimate)) %>%
addLegend("bottomright",
pal = MapPalette,
values = ~ estimate,
title = "Population Percentiles",
opacity = 1) %>%
addCircleMarkers(data = movieUS,
lat = movieUS$latitude,
lng = movieUS$longitude,
popup = movieUS$name,
weight = 1,
radius=4,
color = "blue",
opacity = 1) %>%
addCircleMarkers(data = sbar,
lat = sbar$latitude,
lng = sbar$longitude,
popup = sbar$name,
weight = 1,
radius=4,
color = "red",
opacity = 1)