#Current status: need to combine wgeo data with county data in order to have a count per county to color code by
library(leaflet)
library(tmaptools)
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6 ✔ purrr 0.3.4
## ✔ tibble 3.1.8 ✔ dplyr 1.0.9
## ✔ tidyr 1.2.0 ✔ stringr 1.4.0
## ✔ readr 2.1.2 ✔ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(data.table)
##
## Attaching package: 'data.table'
##
## The following objects are masked from 'package:dplyr':
##
## between, first, last
##
## The following object is masked from 'package:purrr':
##
## transpose
library(ggmap)
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
library(tidygeocoder)
##
## Attaching package: 'tidygeocoder'
##
## The following object is masked from 'package:ggmap':
##
## geocode
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(pals)
setwd(dir = "C:/Users/Work/OneDrive - Emory University/Archive/Documents/RStudio/Rural Mapping local files")
county<-geojson_read("gz_2010_us_050_00_20m.json",what = "sp")
leaflet(county)%>%addProviderTiles(providers$Stamen.TonerBackground, options = tileOptions(opacity = .2))%>%addPolygons()
#wd is rural mapping local files
raw_wgeo<-("wgeo_dataset.csv")
wgeo<-read_csv(raw_wgeo)%>% mutate_if(is.character,utf8::utf8_encode)
## Rows: 305 Columns: 16
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (13): BPHC_sitenum, sat_name, type_site, status_site, typedesig_site, ad...
## dbl (3): order, longitude, latitude
##
## ℹ 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.
wgeo
## # A tibble: 305 × 16
## BPHC_sitenum sat_n…¹ type_…² statu…³ typed…⁴ address city state zip county
## <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
## 1 Southwest G… Southw… Servic… Active Federa… 900 N … Dona… GA 3984… Semin…
## 2 Southwest G… Southw… Admini… Active Federa… 205 Br… Dona… GA 3984… Semin…
## 3 Primary Hea… Primar… Servic… Active Federa… 13570 … Tren… GA 3075… Dade …
## 4 Primary Hea… Tiger … Servic… Active Federa… 134 Rh… Tunn… GA 3075… Catoo…
## 5 Primary Hea… Primar… Servic… Active Federa… 118 E … Ceda… GA 3012… Polk …
## 6 Primary Hea… Primar… Servic… Active Federa… 11638 … Summ… GA 3074… Chatt…
## 7 Primary Hea… Gilber… Servic… Active Federa… 87 S B… La F… GA 3072… Walke…
## 8 Primary Hea… School… Servic… Active Federa… 87 S B… La F… GA 3072… Walke…
## 9 Primary Hea… Primar… Admini… Active Federa… 205 Je… Ross… GA 3074… Walke…
## 10 Primary Hea… Mobile… Servic… Active Federa… 87 S B… La F… GA 3072… Walke…
## # … with 295 more rows, 6 more variables: tel <chr>, date_added <chr>,
## # status_filter <chr>, order <dbl>, longitude <dbl>, latitude <dbl>, and
## # abbreviated variable names ¹sat_name, ²type_site, ³status_site,
## # ⁴typedesig_site
## # ℹ Use `print(n = ...)` to see more rows, and `colnames()` to see all variable names
raw_countyFIPS<-("county_FIPS.csv")
cFIPS<-read_csv(raw_countyFIPS)%>% mutate_if(is.character,utf8::utf8_encode)
## Rows: 159 Columns: 6
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (5): State, Num, County, Col5, GEO_ID
## dbl (1): Col2
##
## ℹ 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.
indices<-match(x=wgeo$county,table = cFIPS$County) #provides FIPS number for each row in wgeo
add_column(cFIPS[indices,"GEO_ID"],x=wgeo$county)
## # A tibble: 305 × 2
## GEO_ID x
## <chr> <chr>
## 1 0500000US13253 Seminole County
## 2 0500000US13253 Seminole County
## 3 0500000US13083 Dade County
## 4 0500000US13047 Catoosa County
## 5 0500000US13233 Polk County
## 6 0500000US13055 Chattooga County
## 7 0500000US13295 Walker County
## 8 0500000US13295 Walker County
## 9 0500000US13295 Walker County
## 10 0500000US13295 Walker County
## # … with 295 more rows
## # ℹ Use `print(n = ...)` to see more rows
wgeo<-wgeo%>%mutate(GEO_ID=cFIPS$GEO_ID[indices])
wgeo_county_count<-count(wgeo,GEO_ID) #summarizes county wgeo presence
wgeo_county_count
## # A tibble: 123 × 2
## GEO_ID n
## <chr> <int>
## 1 0500000US13001 1
## 2 0500000US13003 3
## 3 0500000US13005 1
## 4 0500000US13007 1
## 5 0500000US13009 4
## 6 0500000US13011 1
## 7 0500000US13013 4
## 8 0500000US13015 1
## 9 0500000US13017 2
## 10 0500000US13019 1
## # … with 113 more rows
## # ℹ Use `print(n = ...)` to see more rows
#end result: each wgeo row has a county FIPS number
# to manip county@data: https://stackoverflow.com/questions/43505467/adding-a-column-from-a-dataframe-to-a-spatialpolygon-dataframe
#create index for matches between cFIPS GEO_ID and county GEO_ID
indicesGA<-match(x=cFIPS$GEO_ID,table=county$GEO_ID)
#modifies json file to only have GA data
county@data<-slice(county@data,indicesGA)
#turns list into df
n2<-lengths(county@polygons)
countydf<-list2DF(list(x=county@polygons,n=n2))
#selects rows from df that are correct GEO_ID only
cdfslice<-slice(countydf,indicesGA)
#turns df back into list
cdf2list<-as.list(cdfslice[,1])
#modifies json file to only have GA polygons
county@polygons<-cdf2list
#below: combines number of rural data spots per county with each county in json file, NA for non-Georgia counties
county@data<-county@data%>%left_join(.,y = wgeo_county_count,by = "GEO_ID")
county$n<-county$n %>% as.character(county$n)
#bins to differentiate the colors
bins<-c(0,1,2,3,4,5,6,7,8,9,Inf)
pal <- colorBin("RdYlGn",domain = county$n,bins = bins)
county_labels<-sprintf("<strong>%s</strong><br/>%s",
county$NAME,"")%>%lapply(htmltools::HTML)
#creates a map with county lines overlaid by wgeo data
map<-leaflet(county)%>%
setView(lng = -84, lat = 33, zoom = 07)%>%
addProviderTiles(providers$Stamen.TonerBackground ,options = providerTileOptions(opacity = 0.2))%>%
addPolygons(color = "white",
dashArray = "3",
#popupOptions = popupOptions(),
weight = 1.5,
opacity = .5,
fillOpacity = 1,
fillColor = ~pal(as.numeric(n)),
label = county_labels,
labelOptions = labelOptions(textOnly = FALSE,
noHide = F,
offset = c(-0,0),
direction = "center"),
highlightOptions = highlightOptions(weight = 5,
color = "white",
fillColor = "white",
fillOpacity = 0.7,
bringToFront = TRUE))%>%
addLegend(pal = pal,
values = ~n,
opacity = .7,
title = "Number of sites available",
position = "bottomright")
#%>%addWMSTiles("https://tigerweb.geo.census.gov/arcgis/services/TIGERweb/tigerWMS_Current/MapServer/WMSServer",layers = "Counties",options = WMSTileOptions(transparent = TRU))
#%>%addCircleMarkers(wgeo,lng = wgeo$longitude,lat = wgeo$latitude,radius = 2,label =wgeo$sat_name)
map
# map_2<-leaflet(county)%>%addPolygons(fillOpacity = 0.2,label = county$n)%>%addTiles()%>%addProviderTiles(providers$Stamen.TonerBackground)%>%addCircleMarkers(wgeo,lng = wgeo$longitude,lat = wgeo$latitude,radius = 2,label =wgeo$sat_name)
#
# map_2
#
#
# #wms layers
# leaflet(wgeo)%>%addWMSTiles("https://tigerweb.geo.census.gov/arcgis/services/TIGERweb/tigerWMS_Current/MapServer/WMSServer",layers = "Counties")%>%addProviderTiles(providers$Stamen.TonerBackground,options = providerTileOptions(opacity = 0.2))
# EmoryMap<-leaflet(Emorylonlat)%>%addProviderTiles(providers$Stamen.TonerBackground)%>%addCircleMarkers(.,color=,radius=2,label = EmoryAddressJoin$label,clusterOptions = markerClusterOptions(spiderLegPolylineOptions = list("weight"=0.5,"color"="gray")))
#
# EmoryMap
#
# EmoryMap2<-leaflet(Emorylonlat2)%>%addProviderTiles(providers$Stamen.TonerBackground)%>%addCircleMarkers(.,color=,radius=1,label = EmoryAddressJoin$label,clusterOptions = markerClusterOptions(freezeAtZoom = 12,spiderLegPolylineOptions = list("weight"=0.5,"color"="gray")))
#
# EmoryMap2
#
# EmoryMap3<-leaflet(Emorylonlat2)%>%addProviderTiles(providers$Stamen.TonerBackground)%>%addCircleMarkers(.,color=,radius=1,label = EmoryAddressJoin$label,cl)
#
# EmoryMap3