Skip to content

Instantly share code, notes, and snippets.

@hrbrmstr
Last active April 9, 2021 06:10
Show Gist options
  • Save hrbrmstr/51f961198f65509ad863 to your computer and use it in GitHub Desktop.
Save hrbrmstr/51f961198f65509ad863 to your computer and use it in GitHub Desktop.
hexbin faceted choropleths in R
library(rgdal)
library(rgeos)
library(ggplot2)
library(readr)
library(tidyr)
library(dplyr)
library(grid)
us <- readOGR("us_states_hexgrid.geojson", "OGRGeoJSON")
centers <- cbind.data.frame(data.frame(gCentroid(us, byid=TRUE), id=us@data$iso3166_2))
us_map <- fortify(us, region="iso3166_2")
intensity <- c(D0="Abnormally Dry", D1="Moderate Drought", D2="Severe Drought",
D3="Extreme Drought", D4="Exceptional Drought")
today <- format(Sys.Date(), "%Y%m%d")
read_csv(sprintf("http://droughtmonitor.unl.edu/USDMStatistics.ashx/?mode=table&aoi=state&date=%s", today)) %>%
gather(drought_level, value, D0, D1, D2, D3, D4) %>%
mutate(intensity=factor(intensity[drought_level],
levels=as.character(intensity), ordered=TRUE)) -> drought
gg <- ggplot()
gg <- gg + geom_map(data=us_map, map=us_map,
aes(x=long, y=lat, map_id=id),
color="white", size=0.5)
gg <- gg + geom_map(data=drought, map=us_map,
aes(fill=value, map_id=State))
gg <- gg + geom_map(data=drought, map=us_map,
aes(map_id=State),
fill="#ffffff", alpha=0, color="white",
show_guide=FALSE)
gg <- gg + geom_text(data=centers, aes(label=id, x=x, y=y), color="white", size=4)
gg <- gg + scale_fill_distiller(name="State\nDrought\nCoverage", palette="RdPu", na.value="#7f7f7f",
labels=sprintf("%d%%", c(0, 25, 50, 75, 100)))
gg <- gg + coord_map()
gg <- gg + facet_wrap(~intensity, ncol=2)
gg <- gg + labs(x=NULL, y=NULL, title=sprintf("U.S. Drought Conditions as of %s\n", Sys.Date()))
gg <- gg + theme_bw()
gg <- gg + theme(plot.title=element_text(face="bold", hjust=0, size=24))
gg <- gg + theme(panel.border=element_blank())
gg <- gg + theme(panel.margin=unit(3, "lines"))
gg <- gg + theme(panel.grid=element_blank())
gg <- gg + theme(axis.ticks=element_blank())
gg <- gg + theme(axis.text=element_blank())
gg <- gg + theme(strip.background=element_blank())
gg <- gg + theme(strip.text=element_text(face="bold", hjust=0, size=14))
gg <- gg + theme(legend.position=c(0.75, 0.15))
gg <- gg + theme(legend.direction="horizontal")
gg <- gg + theme(legend.title.align=1)
png(sprintf("%s.png", today), width=800, height=800)
print(gg)
dev.off()
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@irichgreen
Copy link

Hi,

I've got an error message in the line number 9 code.

"us <- readOGR("us_states_hexgrid.geojson", "OGRGeoJSON")"

Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv, :

GDAL Error 3: Cannot open file 'us_states_hexgrid.geojson'

Could you please resolve it?

@bnjcbsn
Copy link

bnjcbsn commented May 17, 2015

Curious about this error as well. Interesting topic.

@hrbrmstr
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment