library(rgdal)
## Loading required package: sp
## rgdal: version: 0.9-1, (SVN revision 518)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 1.11.1, released 2014/09/24
## Path to GDAL shared files: /usr/local/Cellar/gdal/1.11.1_1/share/gdal
## Loaded PROJ.4 runtime: Rel. 4.8.0, 6 March 2012, [PJ_VERSION: 480]
## Path to PROJ.4 shared files: (autodetected)
library(leaflet)
# Set up the color palette and breaks
colors <- c("#FFEDA0", "#FED976", "#FEB24C", "#FD8D3C", "#FC4E2A", "#E31A1C", "#BD0026", "#800026")
bins <- c(-Inf, 10, 20, 50, 100, 200, 500, 1000, Inf) + 0.00000001
pal <- colorBin(colors, NULL, bins)
# Read us-states.json into a SpatialPolygonsDataFrame
us.states <- readOGR("us-states.json", "OGRGeoJSON")
## OGR data source with driver: GeoJSON
## Source: "us-states.json", layer: "OGRGeoJSON"
## with 52 features and 2 fields
## Feature type: wkbPolygon with 2 dimensions
shapes <- polygons(us.states)
alaska <- shapes[which(us.states$name == "Alaska")]
leaflet(us.states) %>% setView(-96, 37.8, 4) %>%
addTiles() %>%
addPolygons(
weight = 2,
opacity = 1,
color = 'white',
dashArray = '3',
fillOpacity = 0.7,
fillColor = ~pal(density),
popup = ~sprintf("<strong>%s</strong><br/>%g people/mi<sup>2</sup>", name, density)
)