Skip to content

Instantly share code, notes, and snippets.

@cpsievert
Forked from hrbrmstr/nytimesdrugmap.r
Created March 25, 2016 05:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cpsievert/8a9686d37c04dace7751 to your computer and use it in GitHub Desktop.
Save cpsievert/8a9686d37c04dace7751 to your computer and use it in GitHub Desktop.
library(jsonlite)
library(tigris)
library(plotly)
library(purrr)
library(tidyr)
library(dplyr)
library(sp)
library(rgeos)
library(rgdal)
library(maptools)
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)
# https://github.com/walkerke/tigris/issues/16
options(tigris_use_cache = FALSE)
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)
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
ggplotly(gg, width = 600, height = 1200)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment