library(ggplot2)
library(tidycensus)
library(tigris)
library(dplyr)
library(mapview)
bex_tract<-get_acs(geography="tract", state = "TX", county = "Bexar", year=2018,variables = c("DP05_0078PE","DP05_0071PE"), 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)`.
## Using the ACS Data Profile
bex_tract<-bex_tract%>%
  mutate(pct_bl = DP05_0078PE, pct_his=DP05_0071PE, cofips=substr(GEOID, 1,5))

pa_counties<-get_acs(geography="county", state = "TX",output="wide",  year=2018,variables = c("DP05_0078PE","DP05_0071PE"))
## Getting data from the 2014-2018 5-year ACS
## Using the ACS Data Profile
bex_county<-pa_counties%>%
  filter(GEOID=="48029")%>%
  mutate(pct_blc = DP05_0078PE, pct_hisc=DP05_0071PE,)%>%
  select(GEOID, pct_blc, pct_hisc)

bexar_merge<-geo_join(bex_tract, bex_county, by_sp="cofips", by_df="GEOID")

#bexar_merge$bb_ratio<-bexar_merge$pct_bb_tr/bexar_merge$pct_bb

Now we create a map of the percent black and percent hispanic in the tracts within Bexar County.

p1<-bexar_merge%>%
  filter(is.na(pct_bl)==F, is.na(pct_his)==F)%>%
  mutate(pblack = cut(pct_bl, breaks = c(0, 4.5, 25, 34, 60, 99) , include.lowest = T))%>%
  ggplot()+geom_sf(aes(fill=pblack))+
  scale_fill_brewer(palette = "Blues")+
    scale_color_brewer(palette = "Blues")+
  ggtitle("Percent of Population that is Non Hispanic Black", subtitle = "Bexar County, TX - 2018 ACS")

p2<-bexar_merge%>%
  filter(is.na(pct_bl)==F, is.na(pct_his)==F)%>%
  mutate(phis = cut(pct_his, breaks = c(0, 4.5, 25, 34, 60, 99) , include.lowest = T))%>%
  ggplot()+geom_sf(aes(fill=phis))+
  scale_fill_brewer(palette = "Blues")+
    scale_color_brewer(palette = "Blues")+
  ggtitle("Percent of Population that is Hispanic", subtitle = "Bexar County, TX - 2018 ACS")

library(patchwork)
p1+p2

mapviewOptions(vector.palette = colorRampPalette(RColorBrewer::brewer.pal(n = 7, name = "Blues")),
               na.color = "grey",
               layers.control.pos = "topright")

mapview(bexar_merge, zcol = c("pct_bl", "pct_his"))