In this project, I attempt to map poverty status among Black rural Texans by county. In order to find out which counties have the highest percent of Black inhabitants impoverished, I pull data from ACS 2019:

First, I pull Census api data.

#https://cran.r-project.org/web/packages/censusapi/vignettes/getting-started.html
#https://cran.r-project.org/web/packages/censusapi/censusapi.pdf
#install.packages("censusapi")
library(censusapi)
## 
## Attaching package: 'censusapi'
## The following object is masked from 'package:methods':
## 
##     getFunction
apis <- listCensusApis()

I look for my data variables from ACS 5yr 2019.

sahie_vars <- listCensusMetadata(
    name = "acs/acs5", 
    vintage = 2019)

#sahie_vars[grep(x = sahie_vars$label, "poverty"), c("name", "label", "concept")]
#sahie_vars[grep(x = sahie_vars$label, "African"), c("name", "label", "concept")]
#sahie_vars[grep(x = sahie_vars$label, "Hispanic"), c("name", "label", "concept")]
#sahie_vars[grep(x = sahie_vars$label, "Black"), c("name", "label", "concept")]
#sahie_vars[grep(x = sahie_vars$concept, "TOTAL"), c("name", "label", "concept")]

I tabulate my findings. Note that I limit the data to counties with total populations less than 60,000. The typical threshold for rural counties as defined by the State is less 50,000, but because we are using ACS data, I am being more liberal here to take into accounty margins of error (For example, Waller county has always been distinguished as a rural county, but recent 2019 estimates have the population exceeding 50,000 - which may disqualify it.)

sahie_national <- getCensus(
    name = "acs/acs5",
    vars = c("NAME","B17001B_002E","B03002_004E","B03002_014E","B03003_002E","B03003_003E"),
    vintage = 2019,
    region = "county:*")

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
sahie_national$Totalpop <- sahie_national$B03003_002E + sahie_national$B03003_003E 
sahie_national$Blackpop <- sahie_national$B03002_004E + sahie_national$B03002_014E 

pov <- sahie_national %>% 
  mutate(Blackpov=B17001B_002E,
         Blackpop=Blackpop,
         Totalpop=Totalpop,
         Blperpov=Blackpov/Blackpop,
         Blperpop=Blackpop/Totalpop)%>% 
filter(state == 48 & Totalpop < 60000 & Blperpop>.15)%>%
  select(NAME,Blackpov,Blackpop,Totalpop,Blperpov,Blperpop)%>%
  arrange(desc(Blperpop))

pov 
##                           NAME Blackpov Blackpop Totalpop   Blperpov  Blperpop
## 1        Houston County, Texas     1973     5888    22954 0.33508832 0.2565130
## 2         Waller County, Texas     2670    12911    51832 0.20680040 0.2490932
## 3          Falls County, Texas     1490     4233    17272 0.35199622 0.2450787
## 4         Morris County, Texas      843     2882    12373 0.29250520 0.2329265
## 5         Marion County, Texas      654     2294    10017 0.28509154 0.2290107
## 6  San Augustine County, Texas      502     1768     8286 0.28393665 0.2133720
## 7       Anderson County, Texas     1396    12124    57810 0.11514352 0.2097215
## 8      Robertson County, Texas     1060     3474    16990 0.30512378 0.2044732
## 9         Newton County, Texas      945     2823    13914 0.33475027 0.2028892
## 10        Shelby County, Texas     1747     4847    25349 0.36042913 0.1912107
## 11        Panola County, Texas      628     4032    23327 0.15575397 0.1728469
## 12          Cass County, Texas     1787     5195    30059 0.34398460 0.1728268
## 13     Limestone County, Texas     1006     4037    23417 0.24919495 0.1723961
## 14          Rusk County, Texas     1282     9075    53755 0.14126722 0.1688215
## 15     Red River County, Texas      581     2039    12171 0.28494360 0.1675294
## 16          Camp County, Texas      491     2136    12878 0.22986891 0.1658643
## 17        Jasper County, Texas     1828     5889    35506 0.31040924 0.1658593
## 18    Washington County, Texas     1181     5805    35163 0.20344531 0.1650883
## 19       Madison County, Texas      220     2332    14197 0.09433962 0.1642601
## 20     Freestone County, Texas      904     2995    19714 0.30183639 0.1519225
## 21        Grimes County, Texas      862     4244    27984 0.20311027 0.1516581
#sahie_vars[grep(x = sahie_vars$name, "B03003_002E"), c("name", "label", "concept")]

povt <- sahie_national %>% 
  mutate(Blackpov=B17001B_002E,
         Blackpop=Blackpop,
         Totalpop=Totalpop,
         Blperpov=Blackpov/Blackpop,
         Blperpop=Blackpop/Totalpop)%>% 
filter(state == 48 & Totalpop < 60000 & Blperpop>.15)%>%
  select(NAME,Blackpov,Blackpop,Totalpop,Blperpov,Blperpop)%>%
  arrange(desc(Blperpov))

povt 
##                           NAME Blackpov Blackpop Totalpop   Blperpov  Blperpop
## 1         Shelby County, Texas     1747     4847    25349 0.36042913 0.1912107
## 2          Falls County, Texas     1490     4233    17272 0.35199622 0.2450787
## 3           Cass County, Texas     1787     5195    30059 0.34398460 0.1728268
## 4        Houston County, Texas     1973     5888    22954 0.33508832 0.2565130
## 5         Newton County, Texas      945     2823    13914 0.33475027 0.2028892
## 6         Jasper County, Texas     1828     5889    35506 0.31040924 0.1658593
## 7      Robertson County, Texas     1060     3474    16990 0.30512378 0.2044732
## 8      Freestone County, Texas      904     2995    19714 0.30183639 0.1519225
## 9         Morris County, Texas      843     2882    12373 0.29250520 0.2329265
## 10        Marion County, Texas      654     2294    10017 0.28509154 0.2290107
## 11     Red River County, Texas      581     2039    12171 0.28494360 0.1675294
## 12 San Augustine County, Texas      502     1768     8286 0.28393665 0.2133720
## 13     Limestone County, Texas     1006     4037    23417 0.24919495 0.1723961
## 14          Camp County, Texas      491     2136    12878 0.22986891 0.1658643
## 15        Waller County, Texas     2670    12911    51832 0.20680040 0.2490932
## 16    Washington County, Texas     1181     5805    35163 0.20344531 0.1650883
## 17        Grimes County, Texas      862     4244    27984 0.20311027 0.1516581
## 18        Panola County, Texas      628     4032    23327 0.15575397 0.1728469
## 19          Rusk County, Texas     1282     9075    53755 0.14126722 0.1688215
## 20      Anderson County, Texas     1396    12124    57810 0.11514352 0.2097215
## 21       Madison County, Texas      220     2332    14197 0.09433962 0.1642601

I make a prettier table.

library(data.table)
## 
## Attaching package: 'data.table'
## The following objects are masked from 'package:dplyr':
## 
##     between, first, last
library(kableExtra)
## 
## Attaching package: 'kableExtra'
## The following object is masked from 'package:dplyr':
## 
##     group_rows
colnames(pov)     <-  c("County",
                      "Black Population in Poverty",
                      "Black Population",
                      "Total Population",
                      "% Black in Poverty",
                      "% of Population Black")

pov %>%
kbl(caption = "Poverty Status in Texas' Majority Black Rural Cities")%>%
add_header_above(c(" ", "Counts" = 3, "Percents" = 2),bold = T) %>%
kable_classic_2(full_width = T, html_font = "Times",font_size = 11) %>%
column_spec(1,width = "1.5in")
Poverty Status in Texas’ Majority Black Rural Cities
Counts
Percents
County Black Population in Poverty Black Population Total Population % Black in Poverty % of Population Black
Houston County, Texas 1973 5888 22954 0.3350883 0.2565130
Waller County, Texas 2670 12911 51832 0.2068004 0.2490932
Falls County, Texas 1490 4233 17272 0.3519962 0.2450787
Morris County, Texas 843 2882 12373 0.2925052 0.2329265
Marion County, Texas 654 2294 10017 0.2850915 0.2290107
San Augustine County, Texas 502 1768 8286 0.2839367 0.2133720
Anderson County, Texas 1396 12124 57810 0.1151435 0.2097215
Robertson County, Texas 1060 3474 16990 0.3051238 0.2044732
Newton County, Texas 945 2823 13914 0.3347503 0.2028892
Shelby County, Texas 1747 4847 25349 0.3604291 0.1912107
Panola County, Texas 628 4032 23327 0.1557540 0.1728469
Cass County, Texas 1787 5195 30059 0.3439846 0.1728268
Limestone County, Texas 1006 4037 23417 0.2491949 0.1723961
Rusk County, Texas 1282 9075 53755 0.1412672 0.1688215
Red River County, Texas 581 2039 12171 0.2849436 0.1675294
Camp County, Texas 491 2136 12878 0.2298689 0.1658643
Jasper County, Texas 1828 5889 35506 0.3104092 0.1658593
Washington County, Texas 1181 5805 35163 0.2034453 0.1650883
Madison County, Texas 220 2332 14197 0.0943396 0.1642601
Freestone County, Texas 904 2995 19714 0.3018364 0.1519225
Grimes County, Texas 862 4244 27984 0.2031103 0.1516581
colnames(povt)     <-  c("County",
                      "Black Population in Poverty",
                      "Black Population",
                      "Total Population",
                      "% Black in Poverty",
                      "% of Population Black")

povt %>%
kbl(caption = "Poverty Status in Texas' Majority Black Rural Cities")%>%
add_header_above(c(" ", "Counts" = 3, "Percents" = 2),bold = T) %>%
kable_classic_2(full_width = T, html_font = "Times",font_size = 11) %>%
column_spec(1,width = "1.5in")
Poverty Status in Texas’ Majority Black Rural Cities
Counts
Percents
County Black Population in Poverty Black Population Total Population % Black in Poverty % of Population Black
Shelby County, Texas 1747 4847 25349 0.3604291 0.1912107
Falls County, Texas 1490 4233 17272 0.3519962 0.2450787
Cass County, Texas 1787 5195 30059 0.3439846 0.1728268
Houston County, Texas 1973 5888 22954 0.3350883 0.2565130
Newton County, Texas 945 2823 13914 0.3347503 0.2028892
Jasper County, Texas 1828 5889 35506 0.3104092 0.1658593
Robertson County, Texas 1060 3474 16990 0.3051238 0.2044732
Freestone County, Texas 904 2995 19714 0.3018364 0.1519225
Morris County, Texas 843 2882 12373 0.2925052 0.2329265
Marion County, Texas 654 2294 10017 0.2850915 0.2290107
Red River County, Texas 581 2039 12171 0.2849436 0.1675294
San Augustine County, Texas 502 1768 8286 0.2839367 0.2133720
Limestone County, Texas 1006 4037 23417 0.2491949 0.1723961
Camp County, Texas 491 2136 12878 0.2298689 0.1658643
Waller County, Texas 2670 12911 51832 0.2068004 0.2490932
Washington County, Texas 1181 5805 35163 0.2034453 0.1650883
Grimes County, Texas 862 4244 27984 0.2031103 0.1516581
Panola County, Texas 628 4032 23327 0.1557540 0.1728469
Rusk County, Texas 1282 9075 53755 0.1412672 0.1688215
Anderson County, Texas 1396 12124 57810 0.1151435 0.2097215
Madison County, Texas 220 2332 14197 0.0943396 0.1642601

Now, I want to map poverty status using Table 2, which arranges the data by percent of Black people in poverty.

Shelby County, which according to my findings has the largest percent of Blacks. I decide to map the top 4 poorest Black counties (Shelby, Falls, Cass and Houston).

library(tidycensus)
library(tidyverse)
library(sf)
library(ggplot2)
library(mapview)
library(RColorBrewer)
library(tmap)
library(tmap)
library(tmaptools)
library(classInt)
library(patchwork)


v15_Profile <- load_variables(2019 , "acs5/profile", cache = TRUE) #demographic profile tables

#Search for variables by keywords in the label 
#v15_Profile[grep(x = v15_Profile$label, "POVERTY"), c("name", "label")]
#v15_Profile[grep(x = v15_Profile$label, "Black"), c("name", "label", "concept")]
#v15_Profile[grep(x = v15_Profile$label, "Black"), c("name", "label")]

#v15_Profile[grep(x = v15_Profile$label, "POVERTY"), c("name", "label", "concept")]

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…

I want to map Black poverty for the county and the actual tracts.

#https://spatialanalysis.github.io/lab_tutorials/4_R_Mapping.html

Shelby_acs <-get_acs(geography = "tract",
                state="TX",
                county = c("Shelby"),
                year = 2018,
                variables=c( "DP03_0119PE", 
                            "DP05_0078P","DP05_0078") ,
                geometry = T, output = "wide")
## 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)`.
## Using the ACS Data Profile
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |                                                                      |   1%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   6%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |=======================                                               |  32%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |=====================================================                 |  76%
  |                                                                            
  |======================================================                |  77%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |=============================================================         |  88%
  |                                                                            
  |======================================================================| 100%
#create a county FIPS code - 5 digit
Shelby_acs$county<-substr(Shelby_acs$GEOID, 1, 5)
Shelby_acs$tract <-substr(Shelby_acs$NAME, 7, 17)

#rename variables and filter missing cases
Shelby_acs2V<-Shelby_acs%>%
  mutate(pblack= DP05_0078PE, ppov=DP03_0119PE) %>%
#  st_transform(crs = 102740)%>%
  na.omit()


shlpov <- tm_shape(Shelby_acs2V) +
  tm_fill("ppov", palette="BuPu") +
  tm_borders(col="black",lwd=.85,lty=1)+#lwd = thickness, lty = line type (1-solid,2-dash,3-dotted)
  #tm_text("tract", size = .45, remove.overlap = TRUE)+  
  tm_legend(show=FALSE)+
  tm_layout(frame=FALSE)

shlpov_bytract <- tm_shape(Shelby_acs2V) +
  tm_fill("ppov", title="Poverty (%)", palette="BuPu") +
  tm_borders(col="black",lwd=.85,lty=1)+ #lwd = thickness, lty = line type (1-solid,2-dash,3-dotted)
  tm_text("DP05_0078E", size = .85, col = "black", fontface = "bold", remove.overlap = TRUE)+
  tm_facets(by = "tract")+  
  tm_layout(legend.position = c("right", "bottom"))
 # tm_layout(main.title = "Poverty Estimates Shelby County (2019)", main.title.size = 0.9, main.title.position = c("left","top"),
            #frame = FALSE)

  #shlpbl <- tm_shape(Shelby_acs2V) +
  #tm_fill("pblack", palette="BuPu") +
  #tm_borders(col="black",lwd=.85,lty=1)+#lwd = thickness, lty = line type (1-solid,2-dash,3-dotted)
  ##tm_text("tract", size = .45, remove.overlap = TRUE)+  
  #tm_legend(show=FALSE)+
  #tm_layout(frame=FALSE)


  #shlpbl_bytract <- tm_shape(Shelby_acs2V) +
  #tm_fill("pblack", title="Black (%)", palette="BuPu") +
  #tm_borders(col="black",lwd=.85,lty=1)+#lwd = thickness, lty = line type (1-solid,2-dash,3-dotted)
  #tm_text("DP05_0078E", size = .85, col = "black", fontface = "bold", remove.overlap = TRUE)+
  #tm_facets(by = "tract")+  
  #tm_layout(legend.position = c("right", "bottom"))
  ##tm_layout(main.title = "Percent Black Shelby County (2019)", main.title.size = 0.9, main.title.position = c("left","top"),
            #frame = FALSE)


#tmap_arrange(shlpov,shlpov_bytract)
#tmap_arrange(shlpbl,shlpbl_bytract)

#https://stackoverflow.com/questions/10776139/r-grid-layout-title
library(grid)
grid.newpage()
pushViewport(viewport(layout = grid.layout(2, 2, heights = unit(c(0.5, 5, 5), "null"))))   
print(shlpov, vp=viewport(layout.pos.row = 2, layout.pos.col = 1))
print(shlpov_bytract, vp=viewport(layout.pos.row = 2, layout.pos.col = 2))
  grid.text("Shelby County Poverty Estimates (ACS 2018)", vp = viewport(layout.pos.row = 1, layout.pos.col = 1:2))

#grid.newpage()
#pushViewport(viewport(layout = grid.layout(2, 2, heights = unit(c(0.5, 5, 5), "null"))))   
#print(shlpbl, vp=viewport(layout.pos.row = 2, layout.pos.col = 1))
#print(shlpbl_bytract, vp=viewport(layout.pos.row = 2, layout.pos.col = 2))
#grid.text("Shelby County Percent Black Estimates (ACS 2018)", vp = viewport(layout.pos.row = 1, layout.pos.col = 1:2))
#https://spatialanalysis.github.io/lab_tutorials/4_R_Mapping.html

Falls_acs <-get_acs(geography = "tract",
                state="TX",
                county = c("Falls"),
                year = 2018,
                variables=c( "DP03_0119PE", 
                            "DP05_0078P","DP05_0078") ,
                geometry = T, output = "wide")
## 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)`.
## Using the ACS Data Profile
#create a county FIPS code - 5 digit
Falls_acs$county<-substr(Falls_acs$GEOID, 1, 5)
Falls_acs$tract <-substr(Falls_acs$NAME, 7, 14)

#rename variables and filter missing cases
Falls_acs2V<-Falls_acs%>%
  mutate(pblack= DP05_0078PE, ppov=DP03_0119PE) %>%
#  st_transform(crs = 102740)%>%
  na.omit()


falpov <- tm_shape(Falls_acs2V) +
  tm_fill("ppov", palette="BuPu") +
  tm_borders(col="black",lwd=.85,lty=1)+#lwd = thickness, lty = line type (1-solid,2-dash,3-dotted)
  #tm_text("tract", size = .45, remove.overlap = TRUE)+  
  tm_legend(show=FALSE)+
  tm_layout(frame=FALSE)

falpov_bytract <- tm_shape(Falls_acs2V) +
  tm_fill("ppov", title="Poverty (%)", palette="BuPu") +
  tm_borders(col="black",lwd=.85,lty=1)+ #lwd = thickness, lty = line type (1-solid,2-dash,3-dotted)
  tm_text("DP05_0078E", size = .85, col = "black", fontface = "bold", remove.overlap = TRUE)+
  tm_facets(by = "tract")+  
  tm_layout(legend.position = c("right", "bottom"))
 # tm_layout(main.title = "Poverty Estimates Falls County (2019)", main.title.size = 0.9, main.title.position = c("left","top"),
            #frame = FALSE)

  #falpbl <- tm_shape(Falls_acs2V) +
  #tm_fill("pblack", palette="BuPu") +
  #tm_borders(col="black",lwd=.85,lty=1)+#lwd = thickness, lty = line type (1-solid,2-dash,3-dotted)
  ##tm_text("tract", size = .45, remove.overlap = TRUE)+  
  #tm_legend(show=FALSE)+
  #tm_layout(frame=FALSE)


  #falpbl_bytract <- tm_shape(Falls_acs2V) +
  #tm_fill("pblack", title="Black (%)", palette="BuPu") +
  #tm_borders(col="black",lwd=.85,lty=1)+#lwd = thickness, lty = line type (1-solid,2-dash,3-dotted)
  #tm_text("DP05_0078E", size = .85, col = "black", fontface = "bold", remove.overlap = TRUE)+
  #tm_facets(by = "tract")+  
  #tm_layout(legend.position = c("right", "bottom"))
  #tm_layout(main.title = "Percent Black Falls County (2019)", main.title.size = 0.9, main.title.position = c("left","top"),
            #frame = FALSE)


#tmap_arrange(falpov,falpov_bytract)
#tmap_arrange(falpbl,falpbl_bytract)

#https://stackoverflow.com/questions/10776139/r-grid-layout-title
library(grid)
grid.newpage()
pushViewport(viewport(layout = grid.layout(2, 2, heights = unit(c(0.5, 5, 5), "null"))))   
print(falpov, vp=viewport(layout.pos.row = 2, layout.pos.col = 1))
print(falpov_bytract, vp=viewport(layout.pos.row = 2, layout.pos.col = 2))
  grid.text("Falls County Poverty Estimates (ACS 2018)", vp = viewport(layout.pos.row = 1, layout.pos.col = 1:2))

#grid.newpage()
#pushViewport(viewport(layout = grid.layout(2, 2, heights = unit(c(0.5, 5, 5), "null"))))   
#print(falpbl, vp=viewport(layout.pos.row = 2, layout.pos.col = 1))
#print(falpbl_bytract, vp=viewport(layout.pos.row = 2, layout.pos.col = 2))
#grid.text("Falls County Percent Black Estimates (ACS 2018)", vp = viewport(layout.pos.row = 1, layout.pos.col = 1:2))
#https://spatialanalysis.github.io/lab_tutorials/4_R_Mapping.html

Cass_acs <-get_acs(geography = "tract",
                state="TX",
                county = c("Cass"),
                year = 2018,
                variables=c( "DP03_0119PE", 
                            "DP05_0078P","DP05_0078") ,
                geometry = T, output = "wide")
## 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)`.
## Using the ACS Data Profile
#create a county FIPS code - 5 digit
Cass_acs$county<-substr(Cass_acs$GEOID, 1, 5)
Cass_acs$tract <-substr(Cass_acs$NAME, 7, 17)

#rename variables and filter missing cases
Cass_acs2V<-Cass_acs%>%
  mutate(pblack= DP05_0078PE, ppov=DP03_0119PE) %>%
#  st_transform(crs = 102740)%>%
  na.omit()


caspov <- tm_shape(Cass_acs2V) +
  tm_fill("ppov", palette="BuPu") +
  tm_borders(col="black",lwd=.85,lty=1)+#lwd = thickness, lty = line type (1-solid,2-dash,3-dotted)
  #tm_text("tract", size = .45, remove.overlap = TRUE)+  
  tm_legend(show=FALSE)+
  tm_layout(frame=FALSE)

caspov_bytract <- tm_shape(Cass_acs2V) +
  tm_fill("ppov", title="Poverty (%)", palette="BuPu") +
  tm_borders(col="black",lwd=.85,lty=1)+ #lwd = thickness, lty = line type (1-solid,2-dash,3-dotted)
  tm_text("DP05_0078E", size = .85, col = "black", fontface = "bold", remove.overlap = TRUE)+
  tm_facets(by = "tract")+  
  tm_layout(legend.position = c("right", "bottom"))
 # tm_layout(main.title = "Poverty Estimates Cass County (2019)", main.title.size = 0.9, main.title.position = c("left","top"),
            #frame = casSE)

  #caspbl <- tm_shape(Cass_acs2V) +
  #tm_fill("pblack", palette="BuPu") +
  #tm_borders(col="black",lwd=.85,lty=1)+#lwd = thickness, lty = line type (1-solid,2-dash,3-dotted)
  ##tm_text("tract", size = .45, remove.overlap = TRUE)+  
  #tm_legend(show=casSE)+
  #tm_layout(frame=casSE)


  #caspbl_bytract <- tm_shape(Cass_acs2V) +
  #tm_fill("pblack", title="Black (%)", palette="BuPu") +
  #tm_borders(col="black",lwd=.85,lty=1)+#lwd = thickness, lty = line type (1-solid,2-dash,3-dotted)
  #tm_text("DP05_0078E", size = .85, col = "black", fontface = "bold", remove.overlap = TRUE)+
  #tm_facets(by = "tract")+  
  #tm_layout(legend.position = c("right", "bottom"))
  #tm_layout(main.title = "Percent Black Cass County (2019)", main.title.size = 0.9, main.title.position = c("left","top"),
            #frame = casSE)


#tmap_arrange(caspov,caspov_bytract)
#tmap_arrange(caspbl,caspbl_bytract)

#https://stackoverflow.com/questions/10776139/r-grid-layout-title
library(grid)
grid.newpage()
pushViewport(viewport(layout = grid.layout(2, 2, heights = unit(c(0.5, 5, 5), "null"))))   
print(caspov, vp=viewport(layout.pos.row = 2, layout.pos.col = 1))
print(caspov_bytract, vp=viewport(layout.pos.row = 2, layout.pos.col = 2))
  grid.text("Cass County Poverty Estimates (ACS 2018)", vp = viewport(layout.pos.row = 1, layout.pos.col = 1:2))

#grid.newpage()
#pushViewport(viewport(layout = grid.layout(2, 2, heights = unit(c(0.5, 5, 5), "null"))))   
#print(caspbl, vp=viewport(layout.pos.row = 2, layout.pos.col = 1))
#print(caspbl_bytract, vp=viewport(layout.pos.row = 2, layout.pos.col = 2))
#grid.text("Cass County Percent Black Estimates (ACS 2018)", vp = viewport(layout.pos.row = 1, layout.pos.col = 1:2))
#https://spatialanalysis.github.io/lab_tutorials/4_R_Mapping.html

Houston_acs <-get_acs(geography = "tract",
                state="TX",
                county = c("Houston"),
                year = 2018,
                variables=c( "DP03_0119PE", 
                            "DP05_0078P","DP05_0078") ,
                geometry = T, output = "wide")
## 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)`.
## Using the ACS Data Profile
#create a county FIPS code - 5 digit
Houston_acs$county<-substr(Houston_acs$GEOID, 1, 5)
Houston_acs$tract <-substr(Houston_acs$NAME, 7, 17)

#rename variables and filter missing houes
Houston_acs2V<-Houston_acs%>%
  mutate(pblack= DP05_0078PE, ppov=DP03_0119PE) %>%
#  st_transform(crs = 102740)%>%
  na.omit()


houpov <- tm_shape(Houston_acs2V) +
  tm_fill("ppov", palette="BuPu") +
  tm_borders(col="black",lwd=.85,lty=1)+#lwd = thickness, lty = line type (1-solid,2-dash,3-dotted)
  #tm_text("tract", size = .45, remove.overlap = TRUE)+  
  tm_legend(show=FALSE)+
  tm_layout(frame=FALSE)

houpov_bytract <- tm_shape(Houston_acs2V) +
  tm_fill("ppov", title="Poverty (%)", palette="BuPu") +
  tm_borders(col="black",lwd=.85,lty=1)+ #lwd = thickness, lty = line type (1-solid,2-dash,3-dotted)
  tm_text("DP05_0078E", size = .85, col = "black", fontface = "bold", remove.overlap = TRUE)+
  tm_facets(by = "tract")+  
  tm_layout(legend.position = c("right", "bottom"))
 # tm_layout(main.title = "Poverty Estimates Houston County (2019)", main.title.size = 0.9, main.title.position = c("left","top"),
            #frame = HoustonE)

  #houpbl <- tm_shape(Houston_acs2V) +
  #tm_fill("pblack", palette="BuPu") +
  #tm_borders(col="black",lwd=.85,lty=1)+#lwd = thickness, lty = line type (1-solid,2-dash,3-dotted)
  ##tm_text("tract", size = .45, remove.overlap = TRUE)+  
  #tm_legend(show=HoustonE)+
  #tm_layout(frame=HoustonE)


  #houpbl_bytract <- tm_shape(Houston_acs2V) +
  #tm_fill("pblack", title="Black (%)", palette="BuPu") +
  #tm_borders(col="black",lwd=.85,lty=1)+#lwd = thickness, lty = line type (1-solid,2-dash,3-dotted)
  #tm_text("DP05_0078E", size = .85, col = "black", fontface = "bold", remove.overlap = TRUE)+
  #tm_facets(by = "tract")+  
  #tm_layout(legend.position = c("right", "bottom"))
  #tm_layout(main.title = "Percent Black Houston County (2019)", main.title.size = 0.9, main.title.position = c("left","top"),
            #frame = HoustonE)


#tmap_arrange(houpov,houpov_bytract)
#tmap_arrange(houpbl,houpbl_bytract)

#https://stackoverflow.com/questions/10776139/r-grid-layout-title
library(grid)
grid.newpage()
pushViewport(viewport(layout = grid.layout(2, 2, heights = unit(c(0.5, 5, 5), "null"))))   
print(houpov, vp=viewport(layout.pos.row = 2, layout.pos.col = 1))
print(houpov_bytract, vp=viewport(layout.pos.row = 2, layout.pos.col = 2))
  grid.text("Houston County Poverty Estimates (ACS 2018)", vp = viewport(layout.pos.row = 1, layout.pos.col = 1:2))

#grid.newpage()
#pushViewport(viewport(layout = grid.layout(2, 2, heights = unit(c(0.5, 5, 5), "null"))))   
#print(houpbl, vp=viewport(layout.pos.row = 2, layout.pos.col = 1))
#print(houpbl_bytract, vp=viewport(layout.pos.row = 2, layout.pos.col = 2))
#grid.text("Houston County Percent Black Estimates (ACS 2018)", vp = viewport(layout.pos.row = 1, layout.pos.col = 1:2))
#library(mapview)
#library(RColorBrewer)
#pal <- colorRampPalette(brewer.pal(6, "Blues")) #set colors
#
#
#top4_acs <-get_acs(geography = "tract",
#                state="TX",
#                county = c("Shelby","Falls","Cass","Houston"),
#                year = 2018,
#                variables=c( "DP03_0119PE", 
#                            "DP05_0078P","DP05_0078") ,
#                geometry = T, output = "wide")
#
#mapView(top4_acs)