Skip to content

Instantly share code, notes, and snippets.

@hrbrmstr
hrbrmstr / areachoropleth.R
Last active November 1, 2016 08:48
Area Choropleth - R version of http://bl.ocks.org/mbostock/4206573
library(rgeos)
library(rgdal) # needs gdal > 1.11.0
library(ggplot2)
# map theme
devtools::source_gist("https://gist.github.com/hrbrmstr/33baa3a79c5cfef0f6df")
# topojson from the bl.ocks example
map = readOGR("us.json", "counties")
@hrbrmstr
hrbrmstr / costarica.R
Last active June 30, 2021 20:54
Costa Rica - R version of http://bl.ocks.org/arce/9357998
library(rgeos)
library(rgdal) # needs gdal > 1.11.0
library(ggplot2)
# map theme
devtools::source_gist("https://gist.github.com/hrbrmstr/33baa3a79c5cfef0f6df")
# grab each of the layers
limites = readOGR("division.json", "limites")
@hrbrmstr
hrbrmstr / cantons.R
Last active November 12, 2018 13:50
Swiss Cantons - R version of http://bl.ocks.org/mbostock/4207744
library(rgeos)
library(rgdal) # needs gdal > 1.11.0
library(ggplot2)
# map theme
devtools::source_gist("https://gist.github.com/hrbrmstr/33baa3a79c5cfef0f6df")
map = readOGR("readme-swiss.json", "cantons")
map_df <- fortify(map)
@dfalster
dfalster / addNewData.R
Last active February 19, 2023 00:29
The function addNewData.R modifies a data frame with a lookup table. This is useful where you want to supplement data loaded from file with other data, e.g. to add details, change treatment names, or similar. The function readNewData is also included. This function runs some checks on the new table to ensure it has correct variable names and val…
##' Modifies 'data' by adding new values supplied in newDataFileName
##'
##' newDataFileName is expected to have columns
##' c(lookupVariable,lookupValue,newVariable,newValue,source)
##'
##' Within the column 'newVariable', replace values that
##' match 'lookupValue' within column 'lookupVariable' with the value
##' newValue'. If 'lookupVariable' is NA, then replace *all* elements
##' of 'newVariable' with the value 'newValue'.
##'