Skip to content

Instantly share code, notes, and snippets.

@etachov
etachov / national_parks_squared.R
Last active January 10, 2018 05:40
Make a square-looking map of U.S. National Parks
# load libraries
library(tidyverse)
library(rgdal)
# get state data
states_map <- map_data("state") %>%
# use round to create square border
mutate_at(.funs = funs(round), .vars = vars(long, lat))
# get park data from https://catalog.data.gov/dataset/national-park-boundariesf0a4c
@etachov
etachov / tspDraw.R
Last active April 8, 2018 23:36
Use @aschinchon's traveling salesperson approach to turn iconic logos into line drawings
library(imager)
library(dplyr)
library(ggplot2)
library(scales)
library(TSP)
## this function takes a 3 channel image and turns it into a line drawing
# internals of the function are all from here https://github.com/aschinchon/travelling-salesman-portrait
tspDraw <- function(raw_img, point_sample_size, line_color, back_color) {
@etachov
etachov / mount_marcy_map.R
Last active August 12, 2018 19:29
Plot Mount Marcy elevation using rayshader
# devtools::install_github("tylermorganwall/rayshader")
library(rayshader)
library(raster)
# Mount Marcy DEM data https://cugir.library.cornell.edu/catalog/cugir-008186
mount_marcy = raster("h47elu.dem")
mount_marcy_matrix <- matrix(extract(mount_marcy, extent(mount_marcy), buffer = 1000),
nrow = ncol(mount_marcy),
ncol = nrow(mount_marcy))
@etachov
etachov / nyc_primary_dot_density.R
Created September 17, 2018 03:49
Dot density map of the 2018 NYC Democrat Governor Primary
library(rvest) # web scraping
library(tidyverse) # general data munging
library(sf) # spatial functions and stats
library(lwgeom) # not 100% sure why, but i needed to load this so `udunits` doesn't throw an error
## import data ------------
# import a geojson of assembly district outlines
nyc_ad <- st_read("http://services5.arcgis.com/GfwWNkhOj9bNBqoJ/arcgis/rest/services/nyad/FeatureServer/0/query?where=1=1&outFields=*&outSR=4326&f=geojson")
@etachov
etachov / dead_sea_rayshader.R
Created September 24, 2018 06:24
Mapping elevation around the Dead Sea
# devtools::install_github("tylermorganwall/rayshader")
library(rayshader)
library(raster)
# quick function to extract a matrix from the raw raster
matrix_extract <- function(raw_raster) {
matrix(raster::extract(raw_raster, extent(raw_raster), buffer = 1000),
nrow = ncol(raw_raster),
ncol = nrow(raw_raster))
@etachov
etachov / nyt_style_buildings.R
Created October 14, 2018 16:23
Making NYT-style building maps with data from Microsoft
library(tidyverse)
library(sf)
library(tigris)
# start by picking a state from https://github.com/Microsoft/USBuildingFootprints
# WARNING: these files can be pretty big. using arizona for its copious subdivisions and reasoanable 83MB.
url_footprint <- "https://usbuildingdata.blob.core.windows.net/usbuildings-v1-1/Arizona.zip"
download.file(url_footprint, "Arizona.zip")
unzip("Arizona.zip")