library(leaflet)
home_icon <- makeIcon(
  iconUrl = "https://img.icons8.com/metro/26/000000/home.png",
  iconWidth = 31*215/230, iconHeight = 31,
  iconAnchorX = 31*215/230/2, iconAnchorY = 16)

my_map <- df %>% leaflet() %>%
  setView(lng = -117.98, lat = 33.75, zoom = 10) %>%
  addTiles() %>%
  addMarkers(lat= 33.7508, lng=-117.9897, popup="Westminster, CA", icon = home_icon) 
 
my_map
v15_Profile <- load_variables(year = 2015 , dataset = "acs5/profile",
cache = TRUE) #demographic profile tables
#Open the data for examination
head(v15_Profile)
## # A tibble: 6 x 3
##   name     label                                    concept                     
##   <chr>    <chr>                                    <chr>                       
## 1 DP02_00~ Estimate!!HOUSEHOLDS BY TYPE!!Total hou~ SELECTED SOCIAL CHARACTERIS~
## 2 DP02_00~ Percent!!HOUSEHOLDS BY TYPE!!Total hous~ SELECTED SOCIAL CHARACTERIS~
## 3 DP02_00~ Estimate!!HOUSEHOLDS BY TYPE!!Total hou~ SELECTED SOCIAL CHARACTERIS~
## 4 DP02_00~ Percent!!HOUSEHOLDS BY TYPE!!Total hous~ SELECTED SOCIAL CHARACTERIS~
## 5 DP02_00~ Estimate!!HOUSEHOLDS BY TYPE!!Total hou~ SELECTED SOCIAL CHARACTERIS~
## 6 DP02_00~ Percent!!HOUSEHOLDS BY TYPE!!Total hous~ SELECTED SOCIAL CHARACTERIS~
#Search for variables by keywords in the label
#v15_Profile[grep(x = v15_Profile$label, "Median household"), c("name", "label")]

# Vietnamese as race
viet_variables <- v15_Profile[grep(x = v15_Profile$label, "Vietnamese"), c("name", "label")]
viet_variables
## # A tibble: 2 x 2
##   name       label                                      
##   <chr>      <chr>                                      
## 1 DP05_0045  Estimate!!RACE!!One race!!Asian!!Vietnamese
## 2 DP05_0045P Percent!!RACE!!One race!!Asian!!Vietnamese
# B23010_001    Estimate!!Total PRESENCE OF OWN CHILDREN UNDER 18 YEARS OLD
#   B00001_001 total population
# B14007_002    Estimate!!Total!!Enrolled in school

acs5/subject

Orange county, California Vietnamese population

library(kableExtra)
## 
## Attaching package: 'kableExtra'
## The following object is masked from 'package:dplyr':
## 
##     group_rows
oc_18_viet<-get_acs(geography = "tract", state="CA", county = "059",
        year = 2018, variables=c( "B02015_022") , geometry = T, output = "wide")
## Getting data from the 2014-2018 5-year ACS
oc_17_viet<-get_acs(geography = "tract", state="CA", county = "059",
        year = 2017, variables=c( "B02015_022") , geometry = T, output = "wide")
## Getting data from the 2013-2017 5-year ACS
oc_16_viet<-get_acs(geography = "tract", state="CA", county = "059",
        year = 2016, variables=c( "B02015_022") , geometry = T, output = "wide")
## Getting data from the 2012-2016 5-year ACS
oc_15_viet<-get_acs(geography = "tract", state="CA", county = "059",
        year = 2015, variables=c( "B02015_022") , geometry = T, output = "wide")
## Getting data from the 2011-2015 5-year ACS
oc_18_viet <- arrange(oc_18_viet,GEOID)
oc_17_viet <- arrange(oc_17_viet,GEOID)
oc_16_viet <- arrange(oc_16_viet,GEOID)
oc_15_viet <- arrange(oc_15_viet,GEOID)

oc_years <- oc_18_viet[,2]
oc_years$'2015' <- oc_15_viet$B02015_022E
oc_years$'2016' <- oc_16_viet$B02015_022E
oc_years$'2017' <- oc_17_viet$B02015_022E
oc_years$'2018' <- oc_18_viet$B02015_022E

oc_years$NAME <- gsub("Census Tract", "", oc_years$NAME) 
oc_years$NAME <- gsub(", Orange County, California","",oc_years$NAME) 
top_oc <- top_n(oc_years,10,oc_years$`2018`)
top_oc <- as.data.frame(top_oc)
top_oc <- select(top_oc,-2)

kable(top_oc) %>%
   kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive")) %>%
   add_header_above(c("Census Tract"=2,"" ,"Yearly" = 2)) %>%
  add_footnote(c("Line 3 : decline in 2016", "Line 7: decline in 2016"), notation = "symbol") %>%
   row_spec(1:10, bold = F, color = "black", background = "lightyellow") %>% 
row_spec(c(3,7), bold = T, color = "red", background = "lightblue")
Census Tract
Yearly
NAME 2015 2016 2017 2018
887.02 2643 2912 3283 3153
889.01 4325 4329 4640 4360
889.03 4952 4743 4898 5075
889.04 3263 3454 3609 4065
889.05 3769 4029 3831 3904
890.01 3743 3805 4032 3970
996.01 3323 2561 2524 3274
997.01 3336 3357 3582 3755
997.02 3478 3420 3697 3715
998.03 2590 2757 2726 3220
* Line 3 : decline in 2016
† Line 7: decline in 2016
#------------change

oc_17_viet <- arrange(oc_17_viet,GEOID)
oc_18_viet <- arrange(oc_18_viet,GEOID)

oc_18_viet$change <- oc_18_viet$B02015_022E - oc_17_viet$B02015_022E


#--- change general population
oc_18_change<-get_acs(geography = "tract", state="CA", county = "059",
        year = 2018, variables=c( "B23010_001") , geometry = T, output = "wide")
## Getting data from the 2014-2018 5-year ACS
oc_17_change<-get_acs(geography = "tract", state="CA", county = "059",
        year = 2017, variables=c( "B23010_001") , geometry = T, output = "wide")
## Getting data from the 2013-2017 5-year ACS
oc_17_change <- arrange(oc_17_change,GEOID)
oc_18_change <- arrange(oc_18_change,GEOID)

oc_18_change$change <- oc_18_change$B23010_001E - oc_17_change$B23010_001E

#--------------- General population + Vietnamese population and general family count
# B05003_003    Estimate!!Total!!Male!!Under 18 years
# B05003_014    Estimate!!Total!!Female!!Under 18 years

oc_18 <- get_acs(
      geography = "tract",
      variables = c( Total="B01003_001",Viet="B02015_022",
                     M_under18="B05003_003", F_under18="B05003_014", 
                     Family="B14007_002",Age_median="B01002D_001"),
      state = "CA", county = "059",
      year = 2018, output = "wide",
      geometry = TRUE ) %>%
      arrange(GEOID)
## Getting data from the 2014-2018 5-year ACS
oc_17 <- get_acs(
      geography = "tract",
      variables = c( Total="B01003_001",Viet="B02015_022",
                      M_under18="B05003_003", F_under18="B05003_014", 
                     Family="B14007_002",Age_median="B01002D_001"),
      state = "CA", county = "059",
      year = 2017, output = "wide",
      geometry = TRUE) %>%
      arrange(GEOID)
## Getting data from the 2013-2017 5-year ACS
oc_18$totalchange <- oc_18$TotalE - oc_17$TotalE
oc_18$vchange <- oc_18$VietE - oc_17$VietE
oc_18$familychange <- oc_18$FamilyE - oc_17$FamilyE
oc_18$agechange <- oc_18$Age_medianE - oc_17$Age_medianE
oc_18$studentchange <- oc_18$M_under18E+oc_18$F_under18E -oc_17$M_under18E - oc_17$F_under18E
oc_18$NAME <- gsub("Census Tract", "", oc_18$NAME) 
oc_18$NAME <- gsub(", Orange County, California","",oc_18$NAME)                         

top_viet <- top_n(oc_18,10,vchange)
top_viet %>%
   ggplot(aes(y = vchange, x = reorder(NAME, vchange))) +
   geom_point(color = "red", size = 3) +
   labs(title = "Census tracts with highest increase in Vietnamese population",
       subtitle = "2013-2017 American Community Survey",
       x = "Census Tracts",
       y = "Population count")

top_viet <- as.data.frame(top_viet)
top_viet[,c(2,16,17,18,19,20)] %>% arrange(-vchange) %>%
  kable() %>%
  kable_styling(bootstrap_options = c("striped", "hover")) %>%
  row_spec(1:5, bold = T, color = "black", background = "lightblue")
NAME totalchange vchange familychange agechange studentchange
996.01 -41 750 -22 0.0 -214
998.03 115 494 -57 -2.3 45
889.04 252 456 137 0.9 92
889.02 376 427 169 -0.5 41
881.07 -24 391 72 -0.5 -33
877.01 75 367 23 0.6 -165
524.20 -554 327 159 0.2 -133
992.29 -258 303 4 -0.4 -131
748.03 -150 297 -206 2.0 -292
992.27 42 283 58 0.8 4
#-------------
bottom_change <- top_n(oc_18,10,-vchange)

bottom_change <- as.data.frame(bottom_change)
bottom_change[,c(2,16,17,18,19,20)] %>% arrange(vchange) %>%
  kable() %>%
  kable_styling(bootstrap_options = c("striped", "hover")) %>%
  row_spec(1:5, bold = T, color = "black", background = "#DeF7E9")
NAME totalchange vchange familychange agechange studentchange
863.06 -183 -611 114 17.0 99
878.02 -239 -563 -181 2.3 25
885.02 240 -547 69 5.6 106
999.05 -73 -393 -116 2.5 -8
992.26 -244 -372 -118 0.4 -189
869.01 -153 -370 -97 4.7 -145
884.01 -459 -305 -298 1.3 -325
755.15 1834 -287 988 -2.4 61
889.01 -127 -280 24 -1.2 -10
886.01 234 -266 148 2.2 -4
bottom_change %>%
   ggplot(aes(y = vchange, x = reorder(NAME, vchange))) +
   geom_point(color = "blue", size = 3) +
   labs(title = "Census tracts with largest decline in Vietnamese population",
       subtitle = "Illustration by Kim Phan ",  
       x = "Census Tracts",
       y = "Population count")

#---------------------

oc_18 %>% ggplot() +
geom_sf(aes(fill = -VietE)) +
coord_sf(datum = NA) 

#-------------------
bottom_viet <- top_n(oc_18,10,-vchange)
bottom_viet %>%
  ggplot(aes(y = vchange, x = reorder(NAME, vchange))) +
  geom_point(color = "red", size = 3) +
  labs(title = "Census tracts with highest decline in  Vietnamese population",
       subtitle = "Illustration by Kim Phan ",
       x = "Census Tracts",
       y = "Number ")

oc_18 %>% ggplot() +geom_sf(aes(fill = -vchange)) +
coord_sf(datum = NA) 

library(data.table)
## 
## Attaching package: 'data.table'
## The following objects are masked from 'package:dplyr':
## 
##     between, first, last
library(dplyr)
library(formattable)
## Warning: package 'formattable' was built under R version 3.6.3
library(tidyr)
library(kableExtra)
#Set a few color variables to make our table more visually appealing

customGreen0 = "#DeF7E9"
customGreen = "#71CA97"
customRed = "#ff7f7f"
oc_18 <- as.data.frame(oc_18)
oc_18[1:40,c(2,5,16,17,18,19,20)] %>% arrange(-VietE) %>%
  kable() %>%
  kable_styling(bootstrap_options = c("striped", "hover")) %>%
  row_spec(1:20, bold = T, color = "black", background = "#DeF7E9") %>%
  row_spec(21:40, bold = T, color = "black", background = "lightblue")
NAME VietE totalchange vchange familychange agechange studentchange
115.02 272 62 0 -123 -1.8 15
15.01 216 -43 -43 -13 -0.2 57
16.02 188 -162 135 -70 -0.5 56
111.02 178 -77 54 -83 -0.5 -71
19.01 140 -127 -8 2 0.5 -13
15.05 132 82 -56 -148 -0.3 90
114.03 112 10 66 -67 -0.8 -28
19.03 111 78 21 -68 2.0 53
13.01 76 -13 -6 -55 0.4 -25
18.01 59 142 -121 146 9.6 23
17.04 53 141 44 157 3.5 -124
17.07 51 -233 38 -21 -0.1 -71
114.02 44 145 35 57 -3.1 94
110 39 89 9 0 -1.3 18
11.03 36 -13 5 -55 0.1 -69
111.01 31 -47 13 -89 0.1 -31
112 27 98 27 72 -24.6 57
17.05 20 -236 -1 -93 6.7 -107
115.03 19 30 -3 -35 -7.9 24
17.06 18 23 9 83 -2.3 98
18.02 14 -265 -3 -419 6.8 -225
19.02 13 52 -10 125 -0.7 102
15.06 12 -205 -2 -76 -2.1 -1
14.04 10 -54 4 64 0.4 -76
15.07 10 90 10 141 -1.5 24
11.02 9 19 1 -167 1.4 -14
14.01 9 -147 9 -265 3.1 -35
14.03 9 -63 -4 -27 0.0 -45
113 9 181 7 -168 1.4 -82
13.03 8 43 3 -57 -2.7 -18
12.02 6 -72 -1 -21 1.0 -66
16.01 5 9 0 -46 -0.3 -122
12.01 4 26 -2 187 0.1 72
11.01 0 232 -3 -56 -1.4 -43
13.04 0 155 0 7 -13.2 22
14.02 0 -36 0 95 -1.9 28
15.03 0 -537 -8 -111 8.1 -224
15.04 0 -61 0 -88 -0.5 126
17.08 0 116 0 -37 0.4 -43
114.01 0 227 0 84 -0.5 62
oc_18_viet %>%                      
ggplot( aes(fill = -B02015_022E, color = B02015_022E)) +
geom_sf() +
coord_sf(datum = NA) +
  ggtitle("Vietnamese population",
  subtitle = "Orange County California, 2018 ")+
 
 
  guides(color=FALSE)+
  theme_minimal()    

P0030001 Total population: P0030002 White alone P0030003 Black or African American alone P0030004 American Indian and Alaska Native alone P0030005 Asian alone P0030006 Native Hawaiian and Other Pacific Islander alone P0030007 Some Other Race alone P0030008 Two or More Races —————————— P005003 RACE 18+:one race:White alone P5. Race For The Population 18 Years And Over P005004 RACE 18+:one race:Bl/AfAm alone P5. Race For The Population 18 Years And Over P005005 RACE 18+:one race:AmInd/AK alone P5. Race For The Population 18 Years And Over P005006 RACE 18+:one race:Asian alone P5. Race For The Population 18 Years And Over P005007 RACE 18+:one race:HI alone P5. Race For The Population 18 Years And Over P005008 RACE 18+:one race:Other alone P5. Race For The Population 18 Years And Over P005009 RACE 18+:Total 2+races P5. Race For The Population 18 Years And Over P005010 RACE 18+:Total 2 races P5. Race For The Population 18 Years And Over P005011 RACE 18+:2 races:White;Bl/AfAm

library(viridis)
## Loading required package: viridisLite
library(totalcensus)


# read data of Asian population in each block
# B05006_076    Estimate!!Total!!Asia!!South Eastern Asia!!Vietnam origin
# B03002_001    Estimate!!Total Hispanic origin
# B02015_001    Estimate!!Total Asian origin


oc_18<-get_acs(geography = "tract", state="CA", county = "059", year = 2018, 
               variables=c( Asian="B02015_001",Hispanic="B03002_001",Vietnamese="B05006_076") , 
               geometry = T, output = "wide")
## Getting data from the 2014-2018 5-year ACS
ggplot(oc_18, aes(fill = -VietnameseE)) +
  geom_sf(color = "red") +
   labs(title = "Vietnamese population",
       subtitle = "Illustration by Kim Phan ")+
    theme_void()

ggplot(oc_18, aes(fill = -HispanicE)) +
  labs(title = "Hispanic population",
       subtitle = "Illustration by Kim Phan ")+
  geom_sf(color = "black") +
  theme_void()

ggplot(oc_18, aes(fill = -AsianE)) +
  labs(title = "Asian population",
       subtitle = "Illustration by Kim Phan ")+
  geom_sf(color = "yellow") +
  theme_void()

San Diego county, California Vietnamese population

sd_18_viet<-get_acs(geography = "tract", state="CA", county = "073",
        year = 2018, variables=c( "B02015_022") , geometry = T, output = "wide")
## Getting data from the 2014-2018 5-year ACS
sd_17_viet<-get_acs(geography = "tract", state="CA", county = "073",
        year = 2017, variables=c( "B02015_022") , geometry = T, output = "wide")
## Getting data from the 2013-2017 5-year ACS
#------------change
sd_17_viet$value = sd_17_viet$B02015_022E
sd_18_viet$value = sd_18_viet$B02015_022E
sd_17_viet <- arrange(sd_17_viet,GEOID)
sd_18_viet <- arrange(sd_18_viet,GEOID)
sd_18_viet$change <- sd_18_viet$value - sd_17_viet$value

gone <- filter(oc_18_viet,GEOID=="06059990100")
top_sd <- top_n(sd_18_viet,20,value)
top_sd <-top_sd %>%
  mutate(NAME = gsub("Census Tract", "", NAME)) %>%
  mutate(NAME = gsub(", San Diego County, California", "", NAME)) 
  
top_sd %>%
  ggplot(aes(y = value, x = reorder(NAME, value))) +
  geom_point(color = "red", size = 3) +
  labs(title = "Top 20 Census tracts with highest Vietnamese population in San Diego County",
       subtitle = "2013-2017 American Community Survey",
       x = "Census Tracts",
       y = "Population count")

sd_18_viet %>% ggplot() +
geom_sf(aes(fill = -B02015_022E)) +
coord_sf(datum = NA) 

change_sd <- top_n(sd_18_viet,20,change)
bottom_sd <- top_n(sd_18_viet,20,-change)

change_sd <-change_sd %>%
  mutate(NAME = gsub("Census Tract", "", NAME)) %>%
  mutate(NAME = gsub(", San Diego County, California", "", NAME)) 
  
change_sd %>%
  ggplot(aes(y = value, x = reorder(NAME, value))) +
  geom_point(color = "red", size = 3) +
  labs(title = "Top 20 Census tracts with highest CHANGE in \nVietnamese population in San Diego County",
       subtitle = "2013-2017 American Community Survey",
       x = "Census Tracts",
       y = "Population count")