Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@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")
@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 / 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 / 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 / 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 / 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 / timeline_gif.R
Created February 28, 2016 18:41
Make a timeline gif using ggplot, gganimate and ggrepel
### make a timeline gif of upcoming elections using data from IFES
library(ggplot2) # plotting engine
library(dplyr) # data manipulation
library(ggrepel) # smart plot labels
library(gganimate) # easy gif-making
library(lubridate) # dealing with date-time
library(RColorBrewer) # color scale
@etachov
etachov / kasich_trips.csv
Last active August 29, 2015 14:25
Quick parse of Columbus Dispatch StoryMap data on Kasich fundraising trips
We can make this file beautiful and searchable if this error is corrected: Unclosed quoted field in line 9.
"lat","lon","date","date_formatted","details"
42.9956397,-71.4547891,"March 24, 2015",2015-03-24,"Manchester, N.H. Kasich gets a taste of New Hampshire folks' vetting of candidates"
40.7127837,-74.0059413,"March 25, 2015",2015-03-25,"New York City Conservatives questions Kasich's support of Medicaid expansion in NYC"
44.3106241,-69.7794897,"March 25, 2015",2015-03-25,"Augusta, Maine Faber travels to Maine to join Kasich"
42.331427,-83.0457538,"April 13, 2015",2015-04-13,"Detroit, Mich. Gov. John Kasich 'seriously considering' running for president"
34.8526176,-82.3940104,"April 17, 2015",2015-04-17,"Greenville, S.C. Kasich gets some pushback in South Carolina"
42.7653662,-71.467566,"April 18, 2015",2015-04-18,"Nashua, N.H. Kasich shares message, makes friends on the road"
38.9071923,-77.0368707,"April 23, 2015",2015-04-23,"Washington, D.C. Kasich, speaking in Washington, discusses potential presidential runSee the video"
38.9071923,-77.0368707,"May 1, 2015",2015-05-01,"Washington, D.C. Kasich defends his dire
@etachov
etachov / index.html
Created June 15, 2015 17:29
Sample PR Chart
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>MDIF Portfolio by Freedom House and GNI</title>
<script type="text/javascript" src="http://d3js.org/d3.v3.js"></script>
<style type = "text/css">
body {
background-color: #fff;