This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(leaflet) | |
library(readxl) # for read_excel() | |
library(ggmap) # for geocode() | |
library(fields) # for rdist.earth() | |
### read data from excel ---- | |
# download to default working directory | |
download.file('https://dl.dropboxusercontent.com/u/12146012/place_names.xlsx', | |
destfile='place_names.xlsx') | |
places <- read_excel('place_names.xlsx') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is a quasi replication of the correlation matrix heatmap viz from Seaborn, | |
# using R and ggplot | |
# see: http://stanford.edu/~mwaskom/software/seaborn/examples/structured_heatmap.html | |
library(data.table) | |
library(ggplot2) | |
# get data | |
brain_url <- paste0('https://raw.githubusercontent.com/', | |
'mwaskom/seaborn-data/master/brain_networks.csv') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(ggplot2) | |
# random data | |
set.seed(5) | |
my_data <- data.frame(y = rnorm(15*5, mean=0, sd=2) + 3, | |
x = rep(letters[1:5], each=5)) | |
# plot: jittered points + boxplot overlay | |
ggplot(my_data, aes(x=x, y=y)) + | |
geom_jitter(aes(color=x, shape=x), width = 0.2) + |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# sometimes you just want to visualize a quantity; this shows a given number of dots | |
library(ggplot2) | |
library(magrittr) | |
dot_count <- 300 | |
size <- ceiling(dot_count^.5) | |
data.frame(x = rep(1:size, each=size), | |
y = rep(size:1, size), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# you must: source('z_load_data.R') | |
stopifnot(exists('dat')) | |
library(tigris) | |
library(leaflet) | |
# load congressional districts | |
cong <- tigris::congressional_districts(cb=TRUE) | |
# add state and district number in form of (AK-1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# I love data.table, but I find the syntax for "mutating" columns a little clunky for such a common task | |
# I wonder if it would be useful to have a setcols() function? | |
# Takes advantage of data.table's pass-by-reference | |
library(data.table) | |
setcols <- function(my_dt, cols, my_fun) { | |
# validate inputs | |
stopifnot('data.table' %in% class(my_dt), | |
'character' == class(cols), | |
'function' == class(my_fun)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Batch download BARUG docs, so as to move from Meetup to Github | |
library(rvest) | |
# 1) build dataframe of download targets, with url and date ---- | |
# fn to get a page of docs from a given offset | |
scrapeBarug <- function(offset) { | |
# get url for a page containing table of BARUG docs | |
# (web shows 5 pages @ offsets 0, 25, 50, 75, 100) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(lubridate) | |
library(tidyverse) | |
# data ---- | |
full_dat <- data.frame( | |
dt = mdy(c('9/19/16', '2/27/17', '4/9/18', '9/17/18', '2/25/19', '8/5/2019', | |
'1/13/2020', '6/22/2020')), | |
num = c(2188,1833,1302,1171,1067,988, 892, 831) | |
) | |
obs <- nrow(full_dat) | |
holdout_days <- 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(png) | |
library(tidyverse) | |
# 1) read in data ---- | |
to_matrix <- function(png_file) { | |
# read PNG to raster (3d array of x * y * color channel) | |
the_png <- readPNG(png_file) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
# read data | |
dat_raw <- read_csv('http://infographics.economist.com/databank/Economist_women-research.csv') | |
# drop stuff that's not the data | |
dat <- dat_raw[2:13,] | |
# fix names | |
names(dat) <- |
OlderNewer