library(rjson)
library(highcharter)
## Highcharts (www.highcharts.com) is a Highsoft software product which is
## not free for commercial and Governmental use
library(jsonlite)
##
## Attaching package: 'jsonlite'
## The following objects are masked from 'package:rjson':
##
## fromJSON, toJSON
library(tigris)
##
## Attaching package: 'tigris'
## The following object is masked from 'package:graphics':
##
## plot
library(ggplot2)
library(purrr)
##
## Attaching package: 'purrr'
## The following object is masked from 'package:jsonlite':
##
## flatten
library(tidyr)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:purrr':
##
## contains, order_by
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(sp)
library(rgeos)
## rgeos version: 0.3-22, (SVN revision 544)
## GEOS runtime version: 3.5.0-CAPI-1.9.0 r4084
## Linking to sp version: 1.2-4
## Polygon checking: TRUE
library(rgdal)
## rgdal: version: 1.2-5, (SVN revision 648)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 2.0.1, released 2015/09/15
## Path to GDAL shared files: C:/Users/green/Documents/R/win-library/3.3/rgdal/gdal
## Loaded PROJ.4 runtime: Rel. 4.9.2, 08 September 2015, [PJ_VERSION: 492]
## Path to PROJ.4 shared files: C:/Users/green/Documents/R/win-library/3.3/rgdal/proj
## Linking to sp version: 1.2-4
library(maptools)
## Checking rgeos availability: TRUE
library(ggthemes)
library(viridis)
URL <- "http://graphics8.nytimes.com/newsgraphics/2016/01/15/drug-deaths/c23ba79c9c9599a103a8d60e2329be1a9b7d6994/data.json"
fil <- "epidemic.json"
if (!file.exists(fil)) download.file(URL, fil)
data <- fromJSON("epidemic.json")
drugs <- gather(data, year, value, -fips)
drugs$year <- sub("^y", "", drugs$year)
drugs <- filter(drugs, year != "2012")
qcty <- quietly(counties)
us <- qcty(setdiff(state.abb, c("AK", "HI")), cb=TRUE)$result
us <- suppressWarnings(SpatialPolygonsDataFrame(gBuffer(gSimplify(us, 0.1, TRUE), byid=TRUE, width=0), us@data))
us_map <- fortify(us, region="GEOID")
gg <- ggplot()
gg <- gg + geom_map(data=us_map, map=us_map,
aes(x=long, y=lat, map_id=id),
color="#2b2b2b", size=0, fill=NA)
## Warning: Ignoring unknown aesthetics: x, y
gg <- gg + geom_map(data=drugs, map=us_map,
aes(fill=value, map_id=fips),
color=NA, size=0)
gg <- gg + scale_fill_viridis(name="Overdose deaths per 100,000",
option="magma")
gg <- gg + facet_wrap(~year, ncol=3)
gg <- gg + coord_map("polyconic")
gg <- gg + theme_map()
gg <- gg + theme(strip.background=element_blank())
gg <- gg + theme(strip.text=element_text(hjust=0))
gg <- gg + theme(legend.position="top")
gg

URL <- "http://graphics8.nytimes.com/newsgraphics/2016/01/15/drug-deaths/c23ba79c9c9599a103a8d60e2329be1a9b7d6994/data.json"
data("uscountygeojson")
data("unemployment")
data <- fromJSON(URL) %>%
tbl_df() %>%
gather(year, value, -fips) %>%
mutate(year = sub("^y", "", year),
value = ifelse(is.na(value), 0, value))
head(data)
## # A tibble: 6 × 3
## fips year value
## <chr> <chr> <dbl>
## 1 01001 2002 1
## 2 01003 2002 2
## 3 01005 2002 0
## 4 01007 2002 1
## 5 01009 2002 2
## 6 01011 2002 0
ds <- data %>%
group_by(fips) %>%
do(item = list(
fips = first(.$fips),
sequence = .$value,
value = first(.$value))) %>%
.$item
hc <- highchart(type = "map") %>%
hc_add_series(data = ds,
name = "drug deaths per 100,000",
mapData = uscountygeojson,
joinBy = "fips",
borderWidth = 0.01) %>%
hc_colorAxis(stops = color_stops()) %>%
hc_title(text = "How the Epidemic of Drug Overdose Deaths Ripples") %>%
hc_legend(layout = "vertical", reversed = TRUE,
floating = TRUE, align = "right") %>%
hc_add_theme(hc_theme_smpl()) %>%
hc_motion(
enabled = TRUE,
axisLabel = "year",
labels = sort(unique(data$year)),
series = 0,
updateIterval = 50,
magnet = list(
round = "floor",
step = 0.1
)
)
hc