This simple example shows how you can use tidycensus along with ggplot2 (from tidyverse) to download US Census data and create maps and plots.
library(tidycensus)
library(leaflet)
library(ggplot2)
library(sp)
library(dplyr)
library(mapview)
library(sf)
library(tidyr)
library(ggrepel)
counties = c("Kendall", "Comal", "Bexar", "Atascosa", "Bandera", "Guadalupe", "Medina", "Wilson")
dfRent = get_acs(geography = "county", variables = "B25064_001", state = "TX", geometry = TRUE, year=2017) %>%
separate(NAME, into="COUNTY", sep=" " ) %>%
filter(COUNTY %in% counties) %>%
arrange(desc(estimate))
mypal = colorBin(palette = "YlGnBu",domain = dfRent$estimate, bins = 5, pretty = TRUE)
popup = paste0("Name: ", dfRent$COUNTY, "<br>", "Value: ", dfRent$estimate)
bbox = st_bbox(dfRent) %>%
as.vector()
mymap<-leaflet(options = leafletOptions(zoomSnap = 0)) %>%
addProviderTiles("CartoDB.Positron") %>%
fitBounds(bbox[1], bbox[2], bbox[3], bbox[4]) %>%
addPolygons(data = dfRent, fillColor = ~mypal(dfRent$estimate), color = "#b2aeae", fillOpacity = 0.7, weight = 1, smoothFactor = 0.2, popup = popup) %>%
addStaticLabels(dfRent, label = dfRent$COUNTY) %>%
addLegend(pal = mypal, values = dfRent$estimate, position = "bottomright", title = "Rent")
mymap
ggplot(data = dfRent, aes(x = estimate, y = reorder(COUNTY, estimate))) + geom_errorbarh(aes(xmin = estimate - moe, xmax=estimate + moe)) + geom_point(color="red", size=3) + geom_label_repel(aes(label = estimate), size = 3) + ylab("County Name") + xlab("Value")