## c(Median household Income, Median Housing Value)
ca_rent <- get_acs(geography = "congressional district",
                   variables = "B25058_001",
                   year = 2018,
                   survey = "acs5",
                   state = "06")
## Getting data from the 2014-2018 5-year ACS
ca_cd <- us_congressional(resolution = "high",states = "California")
ca_cd <- arrange(ca_cd,geoid)
ca_cd$rent <- ca_rent$estimate                   


ggplot(ca_cd) + geom_sf(aes(fill = rent)) +
  scale_fill_gradientn(colors = rev(magma(5)))+
  labs(title = "Median Contract Rent by California Congressional District",
       subtitle = "2018 Census data : B25028_001 ")+
  theme_void()

library(stringr)
numextract <- function(string){ 
  str_extract(string, "\\-*\\d+\\.*\\d*")
} 

 ca_rent[,c(2,4)] %>%
   mutate(NAME = gsub("116th", "", NAME)) %>%
  mutate(NAME = numextract(NAME)) %>% 
  rename(Congressional_District = NAME, median_Rent = estimate)  %>%
  kable() %>%
  kable_styling(bootstrap_options = c("hover", "condensed",full_width = F))
Congressional_District median_Rent
1 822
2 1270
3 1087
4 1131
5 1392
6 991
7 1121
8 897
9 958
10 970
11 1516
12 1729
13 1382
14 2009
15 1797
16 750
17 2246
18 1980
19 1737
20 1365
21 715
22 878
23 851
24 1388
25 1369
26 1583
27 1390
28 1375
29 1192
30 1514
31 1138
32 1311
33 1938
34 1069
35 1206
36 923
37 1328
38 1327
39 1569
40 1068
41 1180
42 1449
43 1217
44 1076
45 1985
46 1408
47 1284
48 1802
49 1729
50 1345
51 1072
52 1785
53 1430
ca_rent[,c(2,4)] %>%
  mutate(NAME = gsub("2018", "",NAME)) %>% 
   mutate(NAME = numextract(NAME)) %>% 
   ggplot(aes(x = estimate, y = reorder(NAME, estimate))) +
   geom_point(color = "brown", size = 1) +
  labs(title = "2018 Median Contract Rent by Congressional Districts in California",
       subtitle = "B25058_001   Median Contract Rent (Dollars)",
       y = "Congressional District ",
       x = "Median Rent")+
  theme_economist(base_size = 6.5)  

ca_rent <- get_acs(geography = "county",
                   variables = "B25058_001",
                   year = 2018,
                   survey = "acs5", geometry = T,
                   state = "06")
## 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%
  |                                                                            
  |=====                                                                 |   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%
ggplot(ca_rent) + geom_sf(aes(fill = estimate)) +
  scale_fill_gradientn(colors = rev(magma(5)))+
  labs(title = "Median Contract Rent by County",
       subtitle = "2018 Census data : B25028_001 ")+
  theme_void()

ca_senate <- get_acs(geography = "state legislative district (upper chamber)", 
                     variables = "B25058_001",   survey = "acs5",
                       state = "CA",year = 2018)
## Getting data from the 2014-2018 5-year ACS
ca_senate %>%
   mutate(NAME = gsub("2018", "",NAME)) %>% 
   mutate(NAME = numextract(NAME)) %>% 
  
   ggplot(aes(x = estimate, y = reorder(NAME,estimate ))) +
 
  geom_point(color = "brown", size = 1) +
  labs(title = "Median Contract Rent by State Senate District in California",
       subtitle = "2018 Census Variable = B25058_001",  
       y = "",
       x = "Rent", caption = "Tidycensus extraction by Joe Long")+
  theme_economist(base_size = 7)

ca_senate[,c(2,4)] %>%
   mutate(NAME = gsub("2018", "",NAME)) %>% 
   mutate(NAME = numextract(NAME)) %>% 
  rename(Senate_District = NAME, Rent = estimate) %>%
  kable() %>%
  kable_styling(bootstrap_options = c("hover", "condensed",full_width = F))
Senate_District Rent
1 1001
2 1249
3 1382
4 946
5 950
6 1014
7 1719
8 908
9 1379
10 1930
11 1758
12 889
13 2149
14 699
15 1839
16 824
17 1451
18 1296
19 1470
20 1159
21 1088
22 1289
23 1059
24 1090
25 1417
26 1790
27 1644
28 1123
29 1479
30 1168
31 1246
32 1336
33 1116
34 1493
35 1159
36 1786
37 1879
38 1350
39 1651
40 1147

Using USAboundaries package to get geographic information

library(USAboundaries)
ca_rent <- get_acs(geography = "congressional district", state = "CA",  
                    year = 2018, 
                    variable = "B25058_001")
## Getting data from the 2014-2018 5-year ACS
ca_cd <- us_congressional(resolution = "high",states = "California")
ca_cd <- arrange(ca_cd,geoid)
ca_cd$rent <- ca_rent$estimate

ggplot(ca_cd) + geom_sf(aes(fill = rent)) +
  scale_fill_gradientn(colors = rev(viridis(10)))+
  labs(title = "Median contract Rent by Congressional District",
       subtitle = "2018 Census data : B25058_001 ",
       caption = "Using USAboundaries Package for geographic information")+
  theme_void()

ca_rent[,c(2,4)] %>%
   mutate(NAME = gsub("116th Congress", "", NAME)) %>%
  mutate(NAME = numextract(NAME)) %>% 
  rename(Congressional_District = NAME, median_Rent = estimate)  %>% 
  kable() %>%
  kable_styling(bootstrap_options = c("hover", "condensed",full_width = F))
Congressional_District median_Rent
1 822
2 1270
3 1087
4 1131
5 1392
6 991
7 1121
8 897
9 958
10 970
11 1516
12 1729
13 1382
14 2009
15 1797
16 750
17 2246
18 1980
19 1737
20 1365
21 715
22 878
23 851
24 1388
25 1369
26 1583
27 1390
28 1375
29 1192
30 1514
31 1138
32 1311
33 1938
34 1069
35 1206
36 923
37 1328
38 1327
39 1569
40 1068
41 1180
42 1449
43 1217
44 1076
45 1985
46 1408
47 1284
48 1802
49 1729
50 1345
51 1072
52 1785
53 1430