Skip to content

Instantly share code, notes, and snippets.

View walkerke's full-sized avatar

Kyle Walker walkerke

View GitHub Profile
@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")
@tristen
tristen / index.html
Created November 8, 2016 22:46
Dynamic template
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title></title>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.26.0/mapbox-gl.css' rel='stylesheet' />
<link href='https://www.mapbox.com/base/latest/base.css' rel='stylesheet' />
<link href='site.css' rel='stylesheet' />
</head>
<body class='clip loading'>
library(magrittr)
library(dplyr)
library(ggplot2)
population <- read.csv("https://raw.githubusercontent.com/andriy-gazin/datasets/master/ageSexDistribution.csv")
population %<>%
tidyr::gather(sex, number, -year, - ageGroup) %>%
mutate(ageGroup = gsub("100 і старше", "≥100", ageGroup),
ageGroup = factor(ageGroup,
purrr::map_df(2009:2014, function(i) {
api_info <- jsonlite::fromJSON(sprintf("http://api.census.gov/data/%s/acs5/", i))
api_info$dataset[, c("title", "c_unavailableMessage")]
})
@hrecht
hrecht / README.md
Last active February 7, 2017 11:36
Mapbox GL JS choropleth with hover and sidebar tooltips

Choropleth example using Mapbox GL JS

Choropleth using an external geojson (DC Census Tracts by Population Change - 2000), data-driven styling, and queryRenderedFeatures to show hovered polygon's features in a sidebar tooltip.

This hit #rstats today:

Has anyone made a dumbbell dot plot in #rstats, or better yet exported to @plotlygraphs using the API? https://t.co/rWUSpH1rRl

— Ken Davis (@ken_mke) October 23, 2015
<script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>

So, I figured it was worth a cpl mins to reproduce.

While the US gov did give the data behind the chart it was all the data and a pain to work with so I used WebPlotDigitizer to transcribe the points and then some data wrangling in R to clean it up and make it work well with ggplot2.

It is possible to make the top "dumbbell" legend in ggplot2 (but not by using a guide) and color the "All Metro A

#' @export
coord_proj <- function(proj="+proj=robin +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs",
inverse = FALSE, degrees = TRUE,
ellps.default="sphere", xlim = NULL, ylim = NULL) {
try_require("proj4")
coord(
proj = proj,
inverse = inverse,
ellps.default = ellps.default,
degrees = degrees,

Main differences are:

  • All the initial data acquisition & munging is kept in one pipeline and the data structure is kept as a tbl_df
  • I ended up having to use html_session since the site was rejecting the access (login req'd) w/o it
  • The for loop is now an apply iteration and pbapply gives you a progress bar for free which is A Good Thing given how long that operation took :-)
  • The move to pbapply makes it possible to do the row-binding and left-joining in a single pipeline, which keeps everything in a tbl_df.
  • Your for solution can be made almost as efficient if you do a img_list <- vector("list", 150) so the list size is pre-allocated.

BTW: the popup code is brilliant and tigris is equally as brilliant!

@RutgerK
RutgerK / Bokeh_test
Last active August 29, 2015 14:21
Bokeh_test
This file has been truncated, but you can view the full file.
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
@ramhiser
ramhiser / leaflet-county-explorer.r
Created May 4, 2015 18:25
Leaflet app in R to explore U.S. Census demographics by county
# TODO: Add a Shiny dropdown to select demographic variable
library(leaflet)
library(noncensus)
library(dplyr)
data("counties", package="noncensus")
data("county_polygons", package="noncensus")
data("quick_facts", package="noncensus")
counties <- counties %>%