library(readxl)
library(ggplot2)
library(gplots)
## 
## Attaching package: 'gplots'
## The following object is masked from 'package:stats':
## 
##     lowess
library(maps) 
library(ggmap)
## The legacy packages maptools, rgdal, and rgeos, underpinning the sp package,
## which was just loaded, will retire in October 2023.
## Please refer to R-spatial evolution reports for details, especially
## https://r-spatial.org/r/2023/05/15/evolution4.html.
## It may be desirable to make the sf package available;
## package maintainers should consider adding sf to Suggests:.
## The sp package is now running under evolution status 2
##      (status 2 uses the sf package in place of rgdal)
## ℹ Google's Terms of Service: <https://mapsplatform.google.com>
## ℹ Please cite ggmap if you use it! Use `citation("ggmap")` for details.
library(RColorBrewer)
library(ggthemes)
library(usdata)
library(usmap)
library(sf)
## Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
library(dplyr)
## 
## Attaching package: 'dplyr'
## 
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## 
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(geojsonio)
## Registered S3 method overwritten by 'geojsonsf':
##   method        from   
##   print.geojson geojson
## 
## Attaching package: 'geojsonio'
## 
## The following object is masked from 'package:base':
## 
##     pretty
library(leaflet)
#load in data
Iowa_businesses <- read_excel("/users/allijohnson/desktop/comp_stat-112/midterm/business-size.xls")
# Filter data to include only county data and only the total number of businesses
County_only <- Iowa_businesses%>%
  filter(County != "State of Iowa")%>%
  mutate(total_businesses = `Total employer establishments`,
         county = County)%>%
  select(county, total_businesses)
#Install County Boundary Data from GeoJson file

iacounties <- geojsonio::geojson_read("https://gist.githubusercontent.com/mcwhittemore/96aaa132af24d3114643/raw/9cea5ced7476cc419621acf0505554e4b45ca459/iowa-counties.geojson", what = "sp") 

#Create Bins for Colors
bins <- c(0, 500, 1000, 2000, 4000, 8000, 16000, 32000) #Bins allow us to tell leaflet what scale for the coloring is
pal <- colorBin("plasma", domain = County_only$total_businesses, bins = bins) # Assigns Color values to data bins

#Variable for Interactive Labels
#Labels will pop up when mouse hovers over them to show the name of the county and the total number of businesses in that county
labels <- sprintf("<strong>%s</strong><br/>%g Businesses",
  County_only$county, County_only$total_businesses
) %>% lapply(htmltools::HTML)

#Make Map use leaflet using iacounties geojson file and addTiles
leaflet(iacounties) %>%
  addTiles()%>%
# Add Title
  addControl(html = "Number of Businesses in Iowa by County",
             position = "topright") %>%
# use add polygons to fill each county based on how many businesses are in each county using fill color
  addPolygons(
    fillColor = ~pal(County_only$total_businesses),
  weight = 2, # how thick the lines around county is
  opacity = 1, # how transparent lines are scale from 0-1
  color = "white", # color of outline
  dashArray = "3", # makes line dashed 
  fillOpacity = 0.7, # specifies transparency of fill color for counties
  # to add interactivity when mouse hovers over counties highlight county
  highlightOptions = highlightOptions(
    weight = 5, # how think highlight boundary line is
    color = "#669", # color of highlight
    dashArray = "", # make highlight dashed or not
    fillOpacity = 0.7, # transparency
    bringToFront = TRUE), # make the highlight be front layer
  label = labels, # makes labels listing county name and number of businesses (uses variable "labels" from earlier)
  labelOptions = labelOptions( #change label formatting
    style = list("font-weight" = "normal", padding = "5px 10px"), 
    textsize = "20px",
    direction = "auto"))%>%
  addLegend(pal = pal, values = ~County_only$total_businesses, opacity = 0.7, title = "Number of Businesses per County", #adds legend for color scale and number of businesses
    position = "bottomright")