Unemployment rates using different congressional district boundaries

 v18 <- load_variables(2018, "acs5", cache = TRUE)
# B25064_001    Estimate!!Median gross rent
View(v18)

Var<-c("B23025_003E","B23025_004E","B23025_005E")
## c(Median household Income, Median Housing Value)
ca_df <- get_acs(geography = "county",
                   variables = Var,
                   year = 2018,
                   survey = "acs5",
                   state = "06",
                   output = "wide")
## Getting data from the 2014-2018 5-year ACS
## Warning: `funs()` is deprecated as of dplyr 0.8.0.
## Please use a list of either functions or lambdas: 
## 
##   # Simple named list: 
##   list(mean = mean, median = median)
## 
##   # Auto named with `tibble::lst()`: 
##   tibble::lst(mean, median)
## 
##   # Using lambdas
##   list(~ mean(., trim = .2), ~ median(., na.rm = TRUE))
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
                   #geometry = TRUE) 

ca_df <- ca_df %>% 
  rename("Total Labor Force"=B23025_003E,
          "Employed"=B23025_004E,
          "Unemployed"=B23025_005E)


ca_df$rate <- 100*ca_df$Unemployed/(ca_df$`Total Labor Force`)
colnames(ca_df)
## [1] "GEOID"             "NAME"              "Total Labor Force"
## [4] "B23025_003M"       "Employed"          "B23025_004M"      
## [7] "Unemployed"        "B23025_005M"       "rate"
 library(DT)

datatable(cbind('County Name' = ca_df[[2]],
                "Unemployment rate" = ca_df[[9]])  )
ca_df <- get_acs(geography = "county",
                   variables = Var,
                   year = 2018,
                   survey = "acs5",
                   state = "06",
                   output = "wide", geometry = T)
## Getting data from the 2014-2018 5-year ACS
## Downloading feature geometry from the Census website.  To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |                                                                      |   1%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |==                                                                    |   4%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |=====                                                                 |   8%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |=======                                                               |  11%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |=========                                                             |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |=========                                                             |  14%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |==========                                                            |  15%
  |                                                                            
  |===========                                                           |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |============                                                          |  18%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |==============                                                        |  19%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |==============                                                        |  21%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  22%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |================                                                      |  24%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |=================                                                     |  25%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |===================                                                   |  28%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=====================                                                 |  29%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |=====================                                                 |  31%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |=======================                                               |  32%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |=========================                                             |  35%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |==========================                                            |  38%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |============================                                          |  39%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |============================                                          |  41%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |==============================                                        |  42%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |===============================                                       |  45%
  |                                                                            
  |================================                                      |  45%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |=================================                                     |  46%
  |                                                                            
  |=================================                                     |  47%
  |                                                                            
  |=================================                                     |  48%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |===================================                                   |  49%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |===================================                                   |  51%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |=====================================                                 |  52%
  |                                                                            
  |=====================================                                 |  53%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |======================================                                |  55%
  |                                                                            
  |=======================================                               |  55%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |========================================                              |  56%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |========================================                              |  58%
  |                                                                            
  |=========================================                             |  58%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |==========================================                            |  59%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |==========================================                            |  61%
  |                                                                            
  |===========================================                           |  61%
  |                                                                            
  |===========================================                           |  62%
  |                                                                            
  |============================================                          |  62%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |=============================================                         |  65%
  |                                                                            
  |==============================================                        |  65%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |===============================================                       |  66%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |===============================================                       |  68%
  |                                                                            
  |================================================                      |  68%
  |                                                                            
  |================================================                      |  69%
  |                                                                            
  |=================================================                     |  69%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |=================================================                     |  71%
  |                                                                            
  |==================================================                    |  71%
  |                                                                            
  |==================================================                    |  72%
  |                                                                            
  |===================================================                   |  72%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |====================================================                  |  75%
  |                                                                            
  |=====================================================                 |  75%
  |                                                                            
  |=====================================================                 |  76%
  |                                                                            
  |======================================================                |  76%
  |                                                                            
  |======================================================                |  77%
  |                                                                            
  |======================================================                |  78%
  |                                                                            
  |=======================================================               |  78%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |========================================================              |  79%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |========================================================              |  81%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |=========================================================             |  82%
  |                                                                            
  |==========================================================            |  82%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |===========================================================           |  85%
  |                                                                            
  |============================================================          |  85%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |=============================================================         |  86%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |=============================================================         |  88%
  |                                                                            
  |==============================================================        |  88%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |===============================================================       |  89%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |===============================================================       |  91%
  |                                                                            
  |================================================================      |  91%
  |                                                                            
  |================================================================      |  92%
  |                                                                            
  |=================================================================     |  92%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |=================================================================     |  94%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |==================================================================    |  95%
  |                                                                            
  |===================================================================   |  95%
  |                                                                            
  |===================================================================   |  96%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |====================================================================  |  98%
  |                                                                            
  |===================================================================== |  98%
  |                                                                            
  |===================================================================== |  99%
  |                                                                            
  |======================================================================|  99%
  |                                                                            
  |======================================================================| 100%
ca_df <- ca_df %>% 
  rename("Total Labor Force"=B23025_003E,
          "Employed"=B23025_004E,
          "Unemployed"=B23025_005E)


ca_df$rate <- 100*ca_df$Unemployed/(ca_df$`Total Labor Force`)
colnames(ca_df)
##  [1] "GEOID"             "NAME"              "Total Labor Force"
##  [4] "B23025_003M"       "Employed"          "B23025_004M"      
##  [7] "Unemployed"        "B23025_005M"       "geometry"         
## [10] "rate"
  ggplot(ca_df) +
    geom_sf(aes(fill = rate), color=NA) +
    #coord_sf(datum=NA) +
    labs(title = "2018 Unemployment rate",
         caption = "Source: Census ACS 5-year, 2014-2018",
         fill = "rate") +
    scale_fill_viridis(direction=-1)

B23025_005E Unemployed Estimate !!Total!!In labor force!!Civilian labor force!!

B23025_003E Total Estimate!!Total!!In labor force!!Civilian labor force

B23025_004E Employed Estimate!!Total!!In labor force!!Civilian labor force!!Employed

ca_cd <- get_acs(geography = "congressional district", variables = Var, state = "CA",
                 year = 2018, output = "wide")
## Getting data from the 2014-2018 5-year ACS
ca_cd <- ca_cd %>% 
  rename("Total Labor Force"=B23025_003E,
          "Employed"=B23025_004E,
          "Unemployed"=B23025_005E)

ca_assembly <- get_acs(geography = "state legislative district (lower chamber)", 
                       variables = Var, output = "wide", 
                       state = "CA",year = 2018)
## Getting data from the 2014-2018 5-year ACS
ca_assembly <- ca_assembly %>% 
  rename("Total Labor Force"=B23025_003E,
          "Employed"=B23025_004E,
          "Unemployed"=B23025_005E)
ca_senate <- get_acs(geography = "state legislative district (upper chamber)", 
                     variables = Var, output = "wide", 
                       state = "CA",year = 2018)
## Getting data from the 2014-2018 5-year ACS
ca_senate <- ca_senate %>% 
  rename("Total Labor Force"=B23025_003E,
          "Employed"=B23025_004E,
          "Unemployed"=B23025_005E)

cleaning up

numextract <- function(string){ 
  str_extract(string, "\\-*\\d+\\.*\\d*")}
ca_cd$NAME <- gsub("115th Congress", "", ca_cd$NAME)
ca_cd$NAME <- numextract(ca_cd$NAME) %>% as.numeric(ca_cd$NAME)
str(ca_cd)
## Classes 'tbl_df', 'tbl' and 'data.frame':    53 obs. of  8 variables:
##  $ GEOID            : chr  "0601" "0603" "0605" "0606" ...
##  $ NAME             : num  1 3 5 6 8 10 11 13 15 16 ...
##  $ Total Labor Force: num  307057 341148 383690 366051 298355 ...
##  $ B23025_003M      : num  2500 2409 2832 2860 3013 ...
##  $ Employed         : num  284276 317261 361052 336654 267817 ...
##  $ B23025_004M      : num  2359 2614 2995 2870 3046 ...
##  $ Unemployed       : num  22781 23887 22638 29397 30538 ...
##  $ B23025_005M      : num  1008 1040 1258 1260 1535 ...
ca_cd$rate <- 100*ca_cd$Unemployed/(ca_cd$Employed+ca_cd$Unemployed)

ca_cd %>%
   ggplot(aes(x = rate, y = reorder(NAME,rate ))) +
 
  geom_point(color = "brown", size = 1) +
  labs(title = "2018 Unemployment rate by Congressional District in California",
       subtitle = "Census Variable = B23025_005E",  
       y = "",
       x = "2018 Median Unemployment rate", caption = "Tidycensus extraction by Joe Long")+
  theme_economist(base_size = 7)

ca_cd[,c(2,9)] %>%
  rename(Congressional_District = NAME, Unemployment_rate = rate) %>%
  kable() %>%
  kable_styling(bootstrap_options = c("hover", "condensed",full_width = F))
Congressional_District Unemployment_rate
1 7.4
3 7.0
5 5.9
6 8.0
8 10.2
10 9.5
11 6.2
13 5.8
15 4.3
16 11.6
18 3.9
20 6.0
21 11.3
23 8.9
24 5.5
26 6.1
28 6.8
29 7.2
31 8.5
33 5.4
34 7.4
36 9.5
38 5.8
39 5.7
41 9.1
42 7.5
44 9.2
46 5.5
47 5.8
49 4.8
51 11.3
52 5.1
2 5.7
4 5.9
7 6.8
9 8.6
12 4.4
14 4.6
25 6.8
17 4.4
19 5.5
22 7.7
27 5.0
30 6.4
32 6.8
35 8.2
37 6.9
40 8.1
43 7.3
45 4.8
48 4.7
50 6.0
53 6.6
ca_assembly$rate <- 100*ca_assembly$Unemployed/(ca_assembly$Employed+ca_assembly$Unemployed)
#--------------------------------
ca_assembly %>%
  mutate(NAME = gsub("2018", "",NAME)) %>% 
 
  mutate(NAME = numextract(NAME)) %>% 
   ggplot(aes(x = rate, y = reorder(NAME, rate))) +
   geom_point(color = "brown", size = 1) +
  labs(title = "2018 Unemployment rate by Assembly District in California",
       subtitle = "American Community Survey",
       y = "Assembly District ",
       x = "Rate")+
  theme_economist(base_size = 6.5)

ca_assembly[,c(2,9)] %>%
   mutate(NAME = gsub("2018", "",NAME)) %>% 
 
  mutate(NAME = numextract(NAME)) %>% 
  rename(Assembly_District = NAME, Unemployment_rate = rate) %>%
  kable() %>%
  kable_styling(bootstrap_options = c("hover", "condensed"))
Assembly_District Unemployment_rate
1 6.4
2 7.0
3 8.7
4 5.8
5 7.2
6 4.8
7 7.4
8 7.6
9 8.0
10 4.4
11 7.0
12 8.6
13 9.3
14 6.9
15 5.8
16 3.7
17 4.6
18 6.3
19 4.9
20 4.8
21 12.6
22 3.8
23 7.9
24 4.1
25 4.4
29 5.5
26 9.3
27 6.2
28 4.1
30 5.8
31 11.1
32 11.7
33 11.3
34 8.3
35 5.6
36 8.8
37 5.6
38 5.6
39 6.3
40 8.4
41 5.1
42 8.6
43 6.9
44 6.2
45 6.0
46 8.0
47 9.3
48 7.0
49 5.2
50 6.0
51 8.7
52 7.8
53 6.9
54 6.4
55 5.7
56 11.9
57 6.0
58 6.3
59 9.1
60 7.1
61 9.4
62 7.1
63 8.5
64 8.8
65 5.8
66 5.0
67 9.0
68 4.8
69 5.6
70 6.5
71 7.6
72 5.2
73 4.3
74 4.7
75 5.5
76 5.2
77 5.1
78 4.9
79 7.5
80 9.8
ca_senate$rate <- 100*ca_senate$Unemployed/(ca_senate$Employed+ca_senate$Unemployed)
ca_senate %>%
  mutate(NAME = gsub("2018", "",NAME)) %>% 
  mutate(NAME = numextract(NAME)) %>% 
  ggplot(aes(x = rate, y = reorder(NAME, rate))) +
  geom_point(color = "darkgreen", size = 2) +
  labs(title = "2018 Unemployment rate by State Senate District in California",
       subtitle = "American Community Survey",
       y = "",
       x = "")+
  theme_economist()

ca_senate[,c(2,9)] %>%
   mutate(NAME = gsub("2018", "",NAME)) %>% 
   mutate(NAME = numextract(NAME)) %>% 
  rename(State_Senate_District = NAME, '2018 Unemployment rate' = rate) %>%
  kable() %>%
  kable_styling(bootstrap_options = c("hover", "condensed"))
State_Senate_District 2018 Unemployment rate
1 5.7
2 6.0
3 6.0
4 7.5
5 8.9
6 8.1
7 5.4
8 8.0
9 6.0
10 4.6
11 4.8
12 9.7
13 3.9
14 12.0
15 5.1
16 8.6
17 5.4
18 7.2
19 6.2
20 8.6
21 8.9
22 6.2
23 8.5
24 7.6
25 5.9
26 5.6
27 5.6
28 8.6
29 5.8
33 7.8
30 7.8
31 8.2
32 5.8
34 5.3
35 8.0
36 4.7
37 4.7
38 6.1
39 5.3
40 10.2