Skip to content

Instantly share code, notes, and snippets.

View grantmcdermott's full-sized avatar

Grant McDermott grantmcdermott

View GitHub Profile
rm(list=ls())
library(dplyr)
library(ggplot2)
library(cowplot)
library(extrafont)
greens <-
data_frame(pop = seq(0, 50, length = 51)) %>%
mutate(tol = 2 - pop/(50/2)) %>%
### LOAD PACKAGES ###
# install.packages("pacman")
pacman::p_load(tidyverse, bigrquery, DBI, sf, stars, rayshader)
### DOWNLOAD GLOBAL FISHING WATCH DATA FROM GOOGLE BIGQUERY ###
## You'll need a valid BigQuery billing ID for this next part. Luckily, you can
## easily sign up for a 12-month free trial that gives you access to BQ, as well
@grantmcdermott
grantmcdermott / default_aes.md
Created August 20, 2019 01:14 — forked from dpseidel/default_aes.md
Default ggplot2 aesthetics table

Default ggplot2 aesthetics by geom.

geom alpha angle colour family fill fontface height hjust lineheight linetype shape size stroke vjust weight width xmax xmin ymax ymin
GeomAbline NA black 1 0.5
GeomAnnotationMap NA NA grey20 1 0.5
GeomArea NA NA grey20 1 0.5
GeomBar NA NA grey35 1 0.
cl y x fe
1 1.5370923925782167 -20.96212959102115 1
1 -1.8734183679775684 -1.839943693864785 1
1 7.550159713148152 17.039214859757113 1
1 2.8293020975816763 16.50853213076337 1
1 -1.5116432128953963 13.428072424536378 1
1 3.3770810866412213 14.014340195044714 2
1 4.006253032023515 -1.209196878297718 2
1 3.4871187962096153 3.077224608721335 2
1 0.042378253992417764 -5.207642778212252 2
@grantmcdermott
grantmcdermott / bland_scrape.R
Last active November 8, 2019 19:48
RSelenium example
## Background: https://twitter.com/JamesBland_Econ/status/1192519670009434114
# devtools::install_github("ropensci/RSelenium")
library(RSelenium)
## Start Selenium driver and browser client
rsd = rsDriver(browser = "firefox") # "chrome" is the default
rsc = rsd$client
## Navigate to the page
@grantmcdermott
grantmcdermott / hsantanna_scrape.R
Last active November 19, 2019 21:00
Brazilian crime data
## Context: https://twitter.com/hsantanna/status/1196184374481506304
## Thanks to Will May: https://twitter.com/williamcmay/status/1196268860418281472?s=20
library(tidyverse)
library(rvest)
library(memoise)
ssp_url = 'http://www.ssp.sp.gov.br/estatistica/pesquisa.aspx'
## get the region/municipality form
@grantmcdermott
grantmcdermott / coef-plots.R
Created December 27, 2019 20:26
Examples of how to plot multiple coefficients from multiple models (using ggplot2 and broom::tidy)
## Context: https://twitter.com/grant_mcdermott/status/1210654424600731648
library(tidyverse)
library(broom)
library(hrbrthemes) ## theme(s) I like
df =
mtcars %>%
mutate(vs = factor(vs), am = factor(am))
@grantmcdermott
grantmcdermott / zip_census.R
Last active January 23, 2020 18:13
Get (possible) census tracts for address based on zip code
## Context: https://twitter.com/grant_mcdermott/status/1220409051013697553
library(tidycensus)
# census_api_key("YOUR KEY GOES HERE", install = TRUE)
library(tigris)
options(tigris_use_cache = TRUE)
library(sf)
library(tidyverse)
## Get ZIP codes for whole US. First time you run this it will take a while, but
@grantmcdermott
grantmcdermott / replace_comp.R
Last active April 13, 2020 22:31
Replace by position
## Context: https://twitter.com/grant_mcdermott/status/1249825585175523328
set.seed(123)
m = matrix(1:10000000, ncol = 1000)
x_pos = sample.int(nrow(m), size = nrow(m)/2)
y_pos = sample.int(ncol(m), size = ncol(m)/2)
m_pos = (y_pos-1)*nrow(m) + x_pos
library(data.table)
@grantmcdermott
grantmcdermott / hidpi.sh
Last active June 7, 2021 23:10
Script for fixing HiDPI issues on Linux (for programs that I use). Useful for automating steps outlined here: https://wiki.archlinux.org/index.php/HiDPI#Applications
#!/bin/sh
#DDIR=$HOME/hidpi_test ## test version
DDIR='/usr/share/applications'
## There are two basic hidpi cases that we need to distinguish and correct for:
## 1) Electron-based apps and 2) QT-based apps
## 1) Electron-based apps (append '--force-scale-device-factor=2' to executable)
for FILE in code-oss.desktop google-chrome.desktop gparted.desktop skypeforlinux.desktop slack.desktop nteract.desktop