Connecticut Census Tracts

library(tigris)
library(sf)
library(ggplot2)
library(plotly)
library(stringr)
library(mapview)
library(leaflet)
library(leafpop)

options(tigris_use_cache = TRUE)

ct <- read_sf("../Shapefiles/tl_2019_09_tract/tl_2019_09_tract.shp")
town <- read.csv("../Shapefiles/tract2town-2010.csv")
town$fips <- str_pad(town$tract_fips, width=11, pad="0")


ct <- merge(ct, town, by.x="GEOID", by.y="fips")
# Project the shapefile
ct.p <- st_transform(ct, 26918)

# Remove Water census tracts
ct.p <- ct.p[ct.p$ALAND !=0,]

p <- ggplot(ct.p, aes(geometry=geometry, label=GEOID, text=paste("Town =", town, "\nCounty =", county))) + 
  geom_sf(fill="white", color="darkblue", linewidth=0.2) +
  theme_bw()

ggplotly(p,
         tooltip=list("label", "text")) 

After Zooming in, double click map to return to default zoom.

Connecticut Places

# Places map

# CT Outline
ct.out <- read_sf("../Shapefiles/CT/CT.shp")
ct.out.p <- st_transform(ct.out, 26918)

# https://www.census.gov/cgi-bin/geo/shapefiles/index.php?year=2019&layergroup=Places
ct.places <- read_sf("../Shapefiles/tl_2019_09_place/tl_2019_09_place.shp")

ct.pl.p <- st_transform(ct.places, 26918)

# st_crs(ct.pl.p)== st_crs(ct.out.p)

ct.pl.p.crop <- st_intersection(ct.pl.p, ct.out.p$geometry)
ct.pl.p.crop <- sf::st_cast(ct.pl.p.crop, "MULTIPOLYGON")

p <- ggplot(ct.out.p, aes(geometry=geometry)) +
  geom_sf(fill="white", color="black", linewidth=0.2) +
  geom_sf(data=ct.pl.p.crop, mapping=aes(geometry=geometry, text=NAME), fill="beige") +
  theme_bw()

ggplotly(p,
         tooltip=list("text")) 
# mapview(list(ct.pl.p.crop, ct.p), 
#         layer.name=c("Places", "Census Tracts"), 
#         label=c("NAME", "GEOID"),
#         col.regions=c("lightblue", "gray"))
ct.p$hover <- paste0("Tract: ", ct.p$GEOID, "; ",
                     "Town: ", ct.p$town, "; ",
                     "County: ", ct.p$county)
mapview(ct.pl.p.crop, 
        layer.name="Places", 
        label=c("NAME"),
        col.regions=c("lightblue"),
        map.types="CartoDB.Positron") +
  mapview(ct.p, 
        layer.name="Census Tracts", 
        label=c("hover"),
        # popup = popupTable(ct.p, 
        #                    zcol=c("GEOID", "town", "county")),
        col.regions=c("gray"))