-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTexas Immigration Map.Rmd
137 lines (116 loc) · 6.14 KB
/
Texas Immigration Map.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
---
title: "Texas Immigration Map"
author: "Matthew Worthington, M.Ed."
date: "4/25/2019"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidycensus)
library(tidyverse)
library(janitor)
library(censusapi)
library(leaflet)
library(RColorBrewer)
library(scales)
library(leaflet.extras)
library(sf)
library(viridis)
library(widgetframe)
options(tigris_use_cache = TRUE)
readRenviron("~/.Renviron")
censuskey <- Sys.getenv("CENSUS_API_KEY")
census_api_key(censuskey)
# census_api_key("c12e51f54773cfa14b8d94d156edc9dfe8070cec" , install=TRUE)
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r cars}
apis <- listCensusApis()
vars_remove <- "B05006_001|B05006_002|B05006_003|PR_|B05006_004|B05006_013|B05006_021|B05006_028|B05006_047|B05006_048|B05006_049|B05006_056|B05006_067|B05006_078|B05006_091|B05006_092|B05006_098|B05006_101| B05006_106|B05006_109|B05006_117|B05006_123|B05006_124|B05006_125|B05006_138|B05006_148|B05006_160"
foreign_born_vars <- load_variables(2017, "acs5") %>%
filter(str_detect(name, "B05006")) %>%
filter(!str_detect(name, vars_remove))
foreign_born_tx <- get_acs(geography = "county",
table = "B05006",
state = "TX",
cache_table = TRUE,
geometry=TRUE,
summary_var = "B05006_001")
pop_by_birth_raw <- foreign_born_tx %>%
filter(variable != "B05006_139") %>% #Exclude Mexico
filter(!str_detect(variable, vars_remove)) %>% #Exclude Continent & Regional Estimates
select(-moe, -summary_moe) %>%
clean_names() %>%
group_by(geoid) %>%
slice(which.max(estimate)) %>%
left_join(foreign_born_vars, by=c("variable"="name"))
pop_by_birth <- pop_by_birth_raw %>%
mutate(name = gsub(pattern=", Texas", replacement = "",x=name)) %>%
mutate(place = gsub(pattern="Estimate!!Total!!", replacement = "",x=label)) %>%
mutate(place = gsub(pattern="^Africa!!|^Asia!!|^Americas!!|^Europe!!", replacement = "",x=place)) %>%
mutate(place = gsub(pattern="^Latin America!!|^Northern Europe!!|^Eastern Asia!!", replacement = "",x=place)) %>%
separate(place, into = c("place1","place2"), sep="!!") %>%
mutate(country = case_when(
!is.na(place2) ~ place2,
is.na(place2) ~ place1,
)) %>%
select(-label, -variable, -concept,region=place1,-place2) %>%
mutate(region = gsub(pattern="^Northern |^Western |^Middle |^Southern |^South Central ", replacement = "",x=region)) %>%
mutate(region = gsub(pattern="^Eastern |n.e.c.$ |^South Eastern ", replacement = "",x=region)) %>%
mutate(continent = case_when(
str_detect(region, "South America") ~ "South Americas",
str_detect(region, "Central America|America|Caribbean") ~ "North America",
str_detect(region, "United Kingdom|Europe|Europe n.e.c.|Sweden") ~ "Europe",
str_detect(region, "Asia|China|Korea|Japan") ~ "Asia",
region == "Africa" ~ "Africa",
region == "Oceania" ~ "Austrailia"
)) %>%
mutate(continent = gsub(pattern="South Americas", replacement = "South America",x=continent))
labels <- sprintf("<a style = 'font-family: 'Titillium Web', -apple-system, BlinkMacSystemFont, Roboto, Helvetica, Arial, sans-serif; font-size: 28px; font-weight: 700; color:#3d3d3d'><strong>%s</strong></a> <br/> <strong>Country:</strong> %s",
pop_by_birth$name,
pop_by_birth$country) %>%
lapply(htmltools::HTML)
pal <- colorFactor(palette=c("#a3cce9", "#4e79a7", "#40515c", "#c8d0d9", "#f1ce63", "#e46769", "#ffbc79"),
levels = c("Europe", "Asia", "South America", "Austrailia", "Africa", "North America", "Arctic"))
pop_map <- leaflet(pop_by_birth, width = "100%", height = "600px", options = leafletOptions(zoomControl = FALSE, minZoom = 6, maxZoom = 6)) %>%
addTiles(urlTemplate = "https://api.mapbox.com/styles/v1/mrw03b/cjvfr58le99nk1gp3ryxmnvd3/tiles/256/{z}/{x}/{y}@2x?access_token=pk.eyJ1IjoibXJ3MDNiIiwiYSI6IlYwb2FiOWcifQ.RWUm2a87fEC9XrDxzvZKKg",
attribution = 'Tiles by <a href="http://www.mapbox.com/">Mapbox</a> | Map by <a href="http://www.texas2036.org/">Texas 2036</a>') %>%
addPolygons(data = pop_by_birth,
stroke = 0,
smoothFactor = 0,
fill = TRUE,
fillColor = ~pal(continent),
fillOpacity = 1,
group='pop_by_birth',
label = labels,
labelOptions = labelOptions(
style = list("font-weight" = "normal", padding = "3px 8px"),
textsize = "18px",
direction = "auto")) %>%
addLegend("bottomleft", pal = pal, values = ~continent,
title = "Continent", opacity = 1) %>%
addControl("<a style='font-size: 22px; font-weight:900'>Instructions</a></br><hr><strong>Find Your County:</strong> Click the <span class='fa fa-search'></span> icon </br><strong>Reset The Map:</strong> Click the <span class='fa fa-home'></span> icon
</div>",position='topleft') %>%
addSearchFeatures(
targetGroups = 'pop_by_birth',
options = searchFeaturesOptions(
textPlaceholder="Type Your County Name...", moveToLocation = FALSE,
openPopup = TRUE, firstTipSubmit = TRUE,
autoCollapse = TRUE, hideMarkerOnCollapse = FALSE)) %>%
# addResetMapButton()
addEasyButton(easyButton(
icon = 'fa-home fa-lg',
title = 'Reset View',
onClick = JS("function(btn, map){ map.setView([31.3686,-99.9018],6); }"))) %>%
setMapWidgetStyle(list(background='white'))
pop_map
htmlwidgets::saveWidget(frameableWidget(pop_map), file = "tx_immigration_map.html", selfcontained = TRUE, background = "white")
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.