library(rgdal);library(leaflet)
## Warning: package 'rgdal' was built under R version 3.4.2
## Loading required package: sp
## rgdal: version: 1.2-13, (SVN revision 686)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 2.2.0, released 2017/04/28
## Path to GDAL shared files: C:/Program Files/R/R-3.4.1/library/rgdal/gdal
## Loaded PROJ.4 runtime: Rel. 4.9.3, 15 August 2016, [PJ_VERSION: 493]
## Path to PROJ.4 shared files: C:/Program Files/R/R-3.4.1/library/rgdal/proj
## Linking to sp version: 1.2-5
## Warning: package 'leaflet' was built under R version 3.4.2
# NOTE: setting the working directory in chunk won't work; run this in console
setwd("C:/bac/HU/ANLY512/data/gisData") #update as needed
#getwd() #checking to make sure the directory is set as desired
# read the shapefile from working dirtectory
districts<-readOGR(dsn = ".",layer="cb_2016_42_unsd_500k") #unified school districts
## OGR data source with driver: ESRI Shapefile
## Source: ".", layer: "cb_2016_42_unsd_500k"
## with 501 features
## It has 8 fields
## Integer64 fields read as strings: ALAND AWATER
counties<-readOGR(dsn=".",layer="cb_2016_us_county_500k") #counties
## OGR data source with driver: ESRI Shapefile
## Source: ".", layer: "cb_2016_us_county_500k"
## with 3233 features
## It has 9 fields
## Integer64 fields read as strings: ALAND AWATER
# these shapefiles are big; subset them
districts<-districts["STATEFP"=="42"] # 42 is the FIPS code for Pennsylvania
#counties<-counties["STATEFP"=="42"] # this doesn't work for some reason
counties<-subset(counties,STATEFP=="42")
# use leaflet to plot the map
d<-leaflet(districts) %>% addTiles() %>%
addPolygons(weight=.75,color="green",fillOpacity = .2) #make a districts layer
d #view this object
library(rgdal);library(leaflet)
c <- leaflet(counties) %>%
addTiles() %>%
addPolygons(weight=.75,color="blue",fillOpacity = .2) #make a counties layer
c # view this object
library(RCurl)
## Loading required package: bitops
regions<-getURL("https://raw.githubusercontent.com/bac3917/Cauldron/master/Regions_b.csv")
regions<-read.csv(text=regions)
counties$fips5<-paste0(counties$STATEFP,counties$COUNTYFP)
merge<-merge(counties,regions,by="fips5")
Note 1: use same addPolygons
command from above to include the district layer, being sure to specify data=district
in the second addPolygons
command
** Note 2: use Stamen.Toner
tiles for better background and contrast**
pal<-colorFactor(c("red","blue","green","yellow","black","orange"), domain = merge$RegionSimple)
leaflet(merge) %>% addProviderTiles(provider="Stamen.Toner") %>%
addPolygons(fillColor =~pal(merge$RegionSimple ),fillOpacity=.5) %>%
addLegend(position=c("topright"),
labels=merge$RegionSimple,pal=pal,values=~merge$RegionSimple) %>%
addPolygons(data=districts,weight=.75,color="green",fillOpacity = .2)