Downloading a list of the schools and their locations

The CivicApps API (http://api.civicapps.org/) was used to pull the list of all of the schools in the Portland, Oregon metro area. The data are in the JSON format and included the school name, various coding variables and the latitude and longitude.

library(leaflet)
library(jsonlite)
raw <- fromJSON("http://api.civicapps.org/schools")[[2]]

Cleaning and Formatting

A data.frame was extracted from the JSON file. Latitude and longitude were converted to numeric values and the school type (Elementary, Middle or High school) was converted to a factor variable to allow the type to be color-coded.

dat <- raw
dat <- dat[dat$Zip > 0 & dat$SchoolType != "",]
dat$Latitude <- as.numeric(dat$Latitude)
dat$Longitude <- as.numeric(dat$Longitude)
dat$SchoolType <- factor(dat$SchoolType,
                         levels(factor(dat$SchoolType))[c(1, 4, 3, 2)])
pal <- c("#FFCC00", "#FF6600", "#FF0000", "#CC0000")  # Colors manually selected
levels(dat$SchoolType) <- pal 

The data were plotted using leaflet.

dat %>%
    leaflet() %>%
    addProviderTiles(providers$Esri.WorldGrayCanvas) %>%
    addProviderTiles(providers$Stamen.TonerLines,
                     options = providerTileOptions(opacity = 0.25)) %>%
    addProviderTiles(providers$Stamen.TonerLabels) %>%
    addCircleMarkers(lng = ~Longitude,
                     lat = ~Latitude,
                     color = ~SchoolType,
                     radius = 6,
                     weight = 1,
                     opacity = 0.8,
                     fill = TRUE,
                     fillColor = ~SchoolType,
                     fillOpacity = 0.5,
                     label = ~SchoolName) %>%
    addLegend(labels = c("Elementary School",
                         "Middle School",
                         "High School (G)",
                         "High School (A)"), colors = pal)