library(leaflet)
library(scales)
library(tidyverse)
## -- Attaching packages ---------------------------------------------------------------------------------------------------- tidyverse 1.2.1 --
## v ggplot2 3.1.0     v purrr   0.2.5
## v tibble  1.4.2     v dplyr   0.7.8
## v tidyr   0.8.2     v stringr 1.3.1
## v readr   1.3.1     v forcats 0.3.0
## -- Conflicts ------------------------------------------------------------------------------------------------------- tidyverse_conflicts() --
## x readr::col_factor() masks scales::col_factor()
## x purrr::discard()    masks scales::discard()
## x dplyr::filter()     masks stats::filter()
## x dplyr::lag()        masks stats::lag()
library(sp)
library(tigris)
## To enable 
## caching of data, set `options(tigris_use_cache = TRUE)` in your R script or .Rprofile.
## 
## Attaching package: 'tigris'
## The following object is masked from 'package:graphics':
## 
##     plot
library(geosphere) 

rm(list=ls())

cd115 <- congressional_districts(cb = TRUE, resolution = '20m') 
## 
  |                                                                       
  |                                                                 |   0%
  |                                                                       
  |==                                                               |   4%
  |                                                                       
  |====                                                             |   6%
  |                                                                       
  |=======                                                          |  10%
  |                                                                       
  |========                                                         |  13%
  |                                                                       
  |===========                                                      |  17%
  |                                                                       
  |=============                                                    |  20%
  |                                                                       
  |===============                                                  |  24%
  |                                                                       
  |=================                                                |  26%
  |                                                                       
  |====================                                             |  30%
  |                                                                       
  |======================                                           |  33%
  |                                                                       
  |========================                                         |  37%
  |                                                                       
  |==========================                                       |  40%
  |                                                                       
  |============================                                     |  44%
  |                                                                       
  |==============================                                   |  47%
  |                                                                       
  |=================================                                |  51%
  |                                                                       
  |===================================                              |  53%
  |                                                                       
  |=====================================                            |  57%
  |                                                                       
  |=======================================                          |  60%
  |                                                                       
  |==========================================                       |  64%
  |                                                                       
  |===========================================                      |  67%
  |                                                                       
  |==============================================                   |  71%
  |                                                                       
  |================================================                 |  74%
  |                                                                       
  |==================================================               |  77%
  |                                                                       
  |====================================================             |  80%
  |                                                                       
  |=======================================================          |  84%
  |                                                                       
  |=========================================================        |  87%
  |                                                                       
  |===========================================================      |  91%
  |                                                                       
  |=============================================================    |  94%
  |                                                                       
  |===============================================================  |  98%
  |                                                                       
  |=================================================================| 100%
ohio_data = subset(cd115, cd115$STATEFP == "39")


ohio_data$district_perimeter <- perimeter(ohio_data)
ohio_data$polsby_popper <- 4* pi *as.numeric(ohio_data$ALAND) / ohio_data$district_perimeter^2
#dev.off()

bins <- c(0, .05, .1,.15,.2,.25,.3,.35,.4,.45)
pal <- colorBin("YlOrRd", domain = ohio_data$polsby_popper, bins = bins)

labels <- sprintf(
  "<strong>Ohio District %s</strong><br/>Polsby Popper Compactness Score %g ",
  ohio_data$CD115FP, ohio_data$polsby_popper
) %>% lapply(htmltools::HTML)

ohio_congressional_plot <- leaflet(ohio_data) %>%
  addPolygons(
    fillColor = ~pal(polsby_popper),
    weight = 2,
    opacity = 1,
    color = "white",
    dashArray = "3",
    fillOpacity = 0.7,
    highlight = highlightOptions(
      weight = 5,
      color = "#666",
      dashArray = "",
      fillOpacity = 0.7,
      bringToFront = TRUE),
    label = labels,
    labelOptions = labelOptions(
      style = list("font-weight" = "normal", padding = "3px 8px"),
      textsize = "15px",
      direction = "auto")
  )
ohio_congressional_plot