knitr::opts_chunk$set(
  warning = FALSE,
  message = FALSE)
library(tidyverse)
library(sf)
library(leaflet)
library(tigris)

Interactive Map

# Read the data set and select variables we need for an interactive map.
# Obtain the average violation rates of childcare centers in each zipcode area.
shiny_map_df = read_csv("data/center_specific.csv") %>%
  select(zip_code, rate) %>% 
   group_by(zip_code) %>%
  summarize(rate = sum(rate)/n())

# Import zips shapefile and transform CRS
zip_sf = st_read("data/ZIP_CODE_040114/ZIP_CODE_040114.shp", quiet = TRUE) %>%
  janitor::clean_names() %>%
  select(zipcode, geometry)
shiny_map_df$zip_code = as.character(shiny_map_df$zip_code)
shiny_map_df =
  geo_join(zip_sf, shiny_map_df, "zipcode", "zip_code", how = "inner")

# Interactive map showing childcare centers‘ average violation rate in each zip code area
labels = sprintf(
  "Childcare centers' violation rate in %s zipcode area is %g.", shiny_map_df$zipcode, shiny_map_df$rate) %>%
  lapply(htmltools::HTML)
pal = colorBin(palette = "PuBu", 10, domain = shiny_map_df$rate)
map_interactive = shiny_map_df %>%
  st_transform(crs = "+init=epsg:4326") %>%
  leaflet() %>%
  addProviderTiles(provider = "CartoDB.Positron") %>%
  addPolygons(label = labels,
              stroke = FALSE,
              smoothFactor = 0.5,
              opacity = 1,
              fillOpacity = 0.8,
              fillColor = ~ pal(rate),
              highlightOptions = highlightOptions(weight = 5,
                                                  fillOpacity = 1,
                                                  color = "black",
                                                  opacity = 1,
                                                  bringToFront = TRUE)) %>%
  addLegend("bottomright",
            pal = pal,
            values = ~ rate,
            title = "Childcare center's violation rate",
            opacity = 0.7) %>%
  addTiles("Childcare centers‘ violation rate")

map_interactive

Interpretation

According to the interactive above, the color in northern NYC and eastern NYC is darker compared with other areas. Therefore, it means childcare centers in these zip code areas have higher violation rate.

Reference dataset

In order to show the childcare centers’ violation rates in different zip code area in this interactive map, NYC zip codes dataset was used to help identify areas in NYC. This dataset is also from NYC OpenData website.