library(readr)
geojson_path <- "~/Desktop/Maryland_Physical_Boundaries_-_County_Boundaries_(Generalized).geojson"
county_data <- st_read(geojson_path)
## Reading layer `BNDY_CountyPhyBoundaryGen_DoIT' from data source 
##   `/Users/rebeccamulcahy/Desktop/Maryland_Physical_Boundaries_-_County_Boundaries_(Generalized).geojson' 
##   using driver `GeoJSON'
## Simple feature collection with 24 features and 9 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -79.4873 ymin: 37.92397 xmax: -75.04921 ymax: 39.72288
## Geodetic CRS:  WGS 84
library(readr)
MarylandData <- read_csv("~/Downloads/MarylandData.csv")
## Rows: 24 Columns: 9
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): county, creation_d, last_updat
## dbl (6): OBJECTID, district, county_fip, countynum, shape_Length, shape_Area
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
View(MarylandData)
countymd <- filter(county, state== "Maryland")
countymd$name <- sub("County$", "",countymd$name)
colnames(countymd)[colnames(countymd) == "name"] <- "county"
countymd$county <- toupper(countymd$county)
row1 <- countymd[18,]
countymd[18,] <- countymd[19,]
countymd[19,] <-row1
countymd$OBJECTID <- seq_along(countymd$county)
combinedcountydata <- county_data %>%
  semi_join(MarylandData, by = c("county" = "county"))

combinedcountydata2 <- combinedcountydata %>% 
  full_join(countymd, by = c("OBJECTID" = "OBJECTID"))
ggplot() +
  geom_sf(data = combinedcountydata2, aes(fill=poverty)) +
  labs(title = "Maryland Map") +
  theme_minimal()

map <- leaflet() %>%
  addTiles()
colors <- colorNumeric(palette = "PuBu", domain = combinedcountydata2$poverty)
##colors <- scale_fill_gradient(high = "darkblue", low = "lightblue")
labels <- sprintf(
  "<strong>%s</strong><br/>%g Poverty Rate",
  combinedcountydata2$county.x, combinedcountydata2$poverty
) %>% 
  lapply(htmltools::HTML)
map <- map %>%
  addPolygons(
    data = combinedcountydata2, 
    fillColor = ~colors(combinedcountydata2$poverty), 
    fillOpacity = .8 , 
    color = "white", 
    weight = 1 , 
    highlight = highlightOptions(
      weight = 5, 
      color = "black" ,
      bringToFront = TRUE, 
      
      ), 
    label = labels, 
    labelOptions = labelOptions(
      style = list("font-weight" = "normal", padding = "3px 8px"),
      textsize = "15px",
      direction = "auto"))
map <- map %>%
  addLegend(
    pal = colors, 
    values = combinedcountydata2$poverty, 
    position = "bottomright", 
    title = "% by County"
)
title <- "Poverty Rate in Each Maryland County"
caption <- "by: Becky Mulcahy December 6th, 2023"
map <- map %>%
  addControl(
    html = title,
    position = "topright"
  )
map