knitr::opts_chunk$set(echo = T)
library(tidyverse); library(tidycensus); library(sf); library(raster)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.3     v purrr   0.3.4
## v tibble  3.1.1     v dplyr   1.0.6
## v tidyr   1.1.3     v stringr 1.4.0
## v readr   1.4.0     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
## Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1
## Loading required package: sp
## 
## Attaching package: 'raster'
## The following object is masked from 'package:dplyr':
## 
##     select
## The following object is masked from 'package:tidyr':
## 
##     extract
library(reshape);library(tmap)
## 
## Attaching package: 'reshape'
## The following object is masked from 'package:dplyr':
## 
##     rename
## The following objects are masked from 'package:tidyr':
## 
##     expand, smiths
library(kableExtra)
## 
## Attaching package: 'kableExtra'
## The following object is masked from 'package:dplyr':
## 
##     group_rows
library(choroplethr)
## Loading required package: acs
## Loading required package: XML
## 
## Attaching package: 'acs'
## The following object is masked from 'package:dplyr':
## 
##     combine
## The following object is masked from 'package:base':
## 
##     apply
library(choroplethrMaps)
library(mapview)
## GDAL version >= 3.1.0 | setting mapviewOptions(fgb = TRUE)
library(stringr)
library(leaflet)
library(viridis)
## Loading required package: viridisLite
library(RColorBrewer)
library(censusapi)
## 
## Attaching package: 'censusapi'
## The following object is masked from 'package:methods':
## 
##     getFunction
census_api_key("dfbe50fd7620ed3bf0e87e29c6bc18ae92e8e9b0")
## To install your API key for use in future sessions, run this function with `install = TRUE`.
options(scipen = 999)
options(tigris_use_cache = TRUE)

California Counties

2010 variables : B01003_001 General population

B02006_010 Estimate!!Total!!Korean

2019 variables : B01003_001 General population

B02015_012 Estimate!!Total:!!Korean

library(dplyr)
oc10 <- get_acs(state = '06', geography =   "county", 
                  variables = c(General="B01003_001",Korean="B02006_010"), output = "wide",year = 2010) %>%
        arrange(GEOID)
## Getting data from the 2006-2010 5-year ACS
oc19 <- get_acs(state = '06', geography =   "county", 
                  variables = c(General="B01003_001",Korean="B02015_012"), output = "wide",year = 2019) %>%
        arrange(GEOID)
## Getting data from the 2015-2019 5-year ACS
oc19$general2010 <- oc10$GeneralE
oc19$k2010 <- oc10$KoreanE
oc19$change <- oc19$GeneralE - oc10$GeneralE
oc19$Kchange <- oc19$KoreanE - oc10$KoreanE

oc19$NAME <- gsub(" County, California","",oc19$NAME)

oc19a <-  oc19[,c("NAME","KoreanE","Kchange")]
colnames(oc19a) <- c("County","Korean","Change")
oc19a %>% arrange(desc(Korean)) %>%
  kbl(caption = "2019 Korean Population") %>%
  kable_classic(full_width = F, html_font = "Cambria")    %>%
  column_spec(2, bold = T, color = "blue") %>%
  column_spec(3, color = "blue") 
2019 Korean Population
County Korean Change
Los Angeles 209336 -1530
Orange 93676 12021
Santa Clara 30137 1471
San Diego 22779 2872
Alameda 19561 -192
San Bernardino 14758 3356
Riverside 11390 1231
San Francisco 10759 839
Contra Costa 9439 1212
San Mateo 6851 909
Sacramento 6705 -129
Ventura 4307 707
Monterey 2617 -325
Yolo 1996 4
Fresno 1841 132
Santa Barbara 1789 -329
Kern 1641 -618
Solano 1544 162
San Joaquin 1510 -115
Placer 1320 202
Sonoma 1289 -88
Marin 1099 -98
Tulare 865 648
San Luis Obispo 815 -18
Imperial 777 139
Stanislaus 738 15
Santa Cruz 700 4
El Dorado 650 18
Butte 580 259
Merced 518 144
Napa 486 -53
Kings 331 146
Mendocino 276 243
Shasta 182 -104
San Benito 162 123
Sutter 116 -311
Lake 102 52
Humboldt 101 -61
Tuolumne 88 -18
Madera 87 35
Del Norte 53 -22
Yuba 50 -15
Calaveras 40 40
Amador 37 37
Nevada 37 -75
Plumas 32 32
Lassen 24 13
Tehama 21 -46
Trinity 11 -14
Colusa 8 5
Mariposa 6 6
Siskiyou 3 -42
Inyo 2 -53
Alpine 0 0
Glenn 0 -17
Modoc 0 0
Mono 0 0
Sierra 0 0

Assembly Districts

oc10 <- get_acs(state = '06',  geography =   "state legislative district (lower chamber)", 
                  variables = c(General="B01003_001",Korean="B02006_010"), output = "wide",year = 2010) %>%
        arrange(GEOID)
## Getting data from the 2006-2010 5-year ACS
oc19 <- get_acs(state = '06',  geography =   "state legislative district (lower chamber)", 
                  variables = c(General="B01003_001",Korean="B02015_012"), output = "wide",year = 2019) %>%
        arrange(GEOID)
## Getting data from the 2015-2019 5-year ACS
oc19$Korean10 <- oc10$KoreanE
oc19$change <- oc19$KoreanE - oc10$KoreanE


oc19$NAME <- gsub(", California","",oc19$NAME) 
oc19$NAME <- gsub("2018","",oc19$NAME)
oc19$NAME <- gsub("[[:punct:]]","",oc19$NAME)

oc19[,c(2,5,8)] %>% arrange(desc(KoreanE)) %>%
  kbl(caption = "2019 Korean Population") %>%
  kable_classic(full_width = F, html_font = "Cambria")    %>%
  column_spec(2, bold = T, color = "blue") %>%
  column_spec(3, color = "blue") 
2019 Korean Population
NAME KoreanE change
Assembly District 53 52573 35144
Assembly District 65 38703 37476
Assembly District 66 24716 21756
Assembly District 55 22650 19087
Assembly District 68 17682 8747
Assembly District 43 17470 4555
Assembly District 38 12155 -3271
Assembly District 74 12044 9665
Assembly District 28 10993 9566
Assembly District 58 10987 3740
Assembly District 54 10793 4418
Assembly District 50 10774 8876
Assembly District 77 10394 8831
Assembly District 16 10098 6349
Assembly District 57 9448 7123
Assembly District 24 9011 2713
Assembly District 25 8018 7431
Assembly District 72 7625 -11926
Assembly District 41 7534 4016
Assembly District 15 6667 1455
Assembly District 51 6494 906
Assembly District 45 6472 -1716
Assembly District 49 6307 3251
Assembly District 17 6230 5503
Assembly District 19 5226 1144
Assembly District 22 5195 -7228
Assembly District 20 4909 -912
Assembly District 73 4791 1717
Assembly District 46 4596 -6302
Assembly District 29 4573 3185
Assembly District 60 4519 -14879
Assembly District 78 4092 1610
Assembly District 52 3893 3628
Assembly District 39 3725 1927
Assembly District 40 3694 -3107
Assembly District 70 3647 -13585
Assembly District 69 3340 1555
Assembly District 18 3258 -1783
Assembly District 62 3209 2131
Assembly District 79 2965 1943
Assembly District 8 2744 -271
Assembly District 6 2721 1063
Assembly District 44 2658 -7975
Assembly District 4 2614 1123
Assembly District 36 2563 324
Assembly District 14 2514 -5042
Assembly District 27 2406 -539
Assembly District 59 2236 -3050
Assembly District 75 2182 -7026
Assembly District 67 2137 -13129
Assembly District 76 2114 -29
Assembly District 37 2062 -1461
Assembly District 61 1886 -8
Assembly District 64 1878 -1501
Assembly District 9 1751 518
Assembly District 63 1751 -3205
Assembly District 23 1746 -915
Assembly District 10 1674 -1394
Assembly District 48 1563 -34756
Assembly District 80 1505 309
Assembly District 42 1500 -9298
Assembly District 33 1457 -233
Assembly District 34 1407 852
Assembly District 7 1405 -256
Assembly District 56 1402 -20731
Assembly District 47 1386 -10472
Assembly District 35 1373 -503
Assembly District 11 1314 -912
Assembly District 30 1301 895
Assembly District 13 1184 -4039
Assembly District 2 1052 10
Assembly District 26 916 1
Assembly District 12 772 -4653
Assembly District 3 767 402
Assembly District 71 732 -5850
Assembly District 21 679 -6330
Assembly District 5 540 -1927
Assembly District 32 460 -1529
Assembly District 1 319 -153
Assembly District 31 101 -245

Senate Districts

oc10 <- get_acs(state = '06',  geography =   "state legislative district (upper chamber)", 
                  variables = c(General="B01003_001",Viet="B02006_017"), output = "wide",year = 2010) %>%
        arrange(GEOID)
## Getting data from the 2006-2010 5-year ACS
oc19 <- get_acs(state = '06',  geography =   "state legislative district (upper chamber)", 
                  variables = c(General="B01003_001",Viet="B02015_022"), output = "wide",year = 2019) %>%
        arrange(GEOID)
## Getting data from the 2015-2019 5-year ACS
oc19$change <- oc19$GeneralE - oc10$GeneralE
oc19$Vchange <- oc19$VietE - oc10$VietE

oc19$NAME <- gsub(", California","",oc19$NAME) 
oc19$NAME <- gsub("2018","",oc19$NAME)
oc19$NAME <- gsub("[[:punct:]]","",oc19$NAME)

oc19[,c(2,5,8)] %>% arrange(desc(VietE)) %>%
  kbl(caption = "2019 Vietnamese American Population") %>%
  kable_classic(full_width = F, html_font = "Cambria")    %>%
   column_spec(2, bold = T, color = "blue") %>%
  column_spec(3, color = "blue") 
2019 Vietnamese American Population
NAME VietE Vchange
State Senate District 34 144963 53766
State Senate District 15 81899 74714
State Senate District 10 49935 8555
State Senate District 22 38613 32282
State Senate District 29 33322 24498
State Senate District 37 28892 20028
State Senate District 39 27689 6122
State Senate District 6 24188 12339
State Senate District 9 18424 5491
State Senate District 17 17263 14163
State Senate District 11 16620 -170
State Senate District 27 11209 4893
State Senate District 38 10503 31
State Senate District 5 10066 -3104
State Senate District 40 9899 5032
State Senate District 31 9437 722
State Senate District 7 9316 3055
State Senate District 35 9188 -45878
State Senate District 36 8226 -1916
State Senate District 32 8201 310
State Senate District 13 7824 -66584
State Senate District 28 7756 2494
State Senate District 20 7603 157
State Senate District 3 6604 -889
State Senate District 26 5402 2555
State Senate District 23 5081 -333
State Senate District 4 5068 2584
State Senate District 24 4924 -23266
State Senate District 25 4806 -2447
State Senate District 8 4290 -7324
State Senate District 18 3980 1618
State Senate District 33 3611 -16540
State Senate District 19 3439 -1633
State Senate District 2 3083 -1107
State Senate District 1 3000 -6164
State Senate District 16 2322 665
State Senate District 21 2269 -8091
State Senate District 30 2016 -1957
State Senate District 14 1719 -1858
State Senate District 12 1694 -302

Congressional Districts

oc10 <- get_acs(state = '06',  geography =   "congressional district", 
                  variables = c(General="B01003_001",Viet="B02006_017"), output = "wide",year = 2010) %>%
        arrange(GEOID)
## Getting data from the 2006-2010 5-year ACS
oc19 <- get_acs(state = '06',  geography =   "congressional district", 
                  variables = c(General="B01003_001",Viet="B02015_022"), output = "wide",year = 2019) %>%
        arrange(GEOID)
## Getting data from the 2015-2019 5-year ACS
oc19$change <- oc19$GeneralE - oc10$GeneralE
oc19$Vchange <- oc19$VietE - oc10$VietE

oc19$NAME <- gsub("116th Congress","",oc19$NAME) 
oc19$NAME <- gsub(", California","",oc19$NAME) 
oc19$NAME <- gsub("[[:punct:]]","",oc19$NAME)


oc19[,c(2,5,8)] %>% arrange(desc(VietE)) %>%
  kbl(caption = "2019 Vietnamese American Population") %>%
  kable_classic(full_width = F, html_font = "Cambria")    %>%
   column_spec(2, bold = T, color = "blue") %>%
  column_spec(3, color = "blue") 
2019 Vietnamese American Population
NAME VietE Vchange
Congressional District 19 82836 80683
Congressional District 48 75925 59549
Congressional District 47 64334 1815
Congressional District 17 46607 44379
Congressional District 46 43295 -12878
Congressional District 27 21887 15302
Congressional District 52 21124 14212
Congressional District 45 19893 14936
Congressional District 7 18409 12975
Congressional District 32 18295 -5455
Congressional District 15 15179 -25965
Congressional District 13 14780 -3669
Congressional District 12 13073 6800
Congressional District 53 11517 -2320
Congressional District 39 10616 6676
Congressional District 18 10326 8051
Congressional District 42 10039 2084
Congressional District 51 9845 4131
Congressional District 6 9829 5962
Congressional District 30 9285 5363
Congressional District 43 8470 4257
Congressional District 9 8382 -733
Congressional District 38 7548 1121
Congressional District 11 7024 -1878
Congressional District 35 6362 1414
Congressional District 14 6230 -593
Congressional District 49 5557 721
Congressional District 41 5479 2296
Congressional District 50 5308 -9058
Congressional District 31 4956 1026
Congressional District 3 4922 -6219
Congressional District 5 4807 -6220
Congressional District 34 4397 2136
Congressional District 33 3986 2149
Congressional District 22 3394 1956
Congressional District 10 3128 -1281
Congressional District 26 3118 -4015
Congressional District 28 2856 155
Congressional District 25 2824 -327
Congressional District 20 2806 1689
Congressional District 4 2706 1021
Congressional District 29 2619 -10946
Congressional District 37 2579 -2750
Congressional District 24 2443 -529
Congressional District 44 2235 -6834
Congressional District 36 2150 -1438
Congressional District 23 2107 -1862
Congressional District 2 2059 307
Congressional District 8 1695 -9053
Congressional District 16 1645 -69366
Congressional District 1 1275 -2474
Congressional District 40 1121 -28909
Congressional District 21 1062 -1883