Skip to content

Instantly share code, notes, and snippets.

View arvi1000's full-sized avatar

Arvi Sreenivasan arvi1000

View GitHub Profile
@arvi1000
arvi1000 / brevet_hilliness.R
Created April 25, 2019 20:53
How much climbing in RUSA brevets in California? How does Boonville 300k brevet compare?
# 'dat' from https://rusa.org/cgi-bin/permsearch_GF.pl
boonville_300 <- data.frame(distance_km = 192.8/.62,
climbing_ft = 10460,
label_x = 325,
label_y = 5500,
label_txt='Boonville 300 Brevet')
ggplot(dat, aes(x=distance_km, y=climbing_ft)) +
# perms
geom_point(alpha=.5) +
@arvi1000
arvi1000 / rnaturalearth_ggplot.R
Last active April 12, 2019 17:24
Richer map data with rnaturalearth
library(rnaturalearth)
library(tidyverse)
world <- ne_countries(scale = "medium", returnclass = "sf")
world %>%
filter(continent == 'Asia') %>%
ggplot() +
geom_sf(aes(fill=income_grp),
color='#A98743', size=0.1) +
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) <-
@arvi1000
arvi1000 / pie_extract.R
Created February 16, 2019 00:03
Code to extract data from a pie chart PNG and make a better visual
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)
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
@arvi1000
arvi1000 / barug_scrape.R
Created February 10, 2017 04:32
Batch download BARUG docs, with rvest/download.file
# 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)
@arvi1000
arvi1000 / mutate_cols_DT.R
Last active December 9, 2016 21:00
A setcols function for data.table
# 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))
@arvi1000
arvi1000 / make_map.R
Last active November 16, 2016 22:45
Which Congressional districts have spoken out against Bannon?
# 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)
@arvi1000
arvi1000 / this_many_dots.R
Created November 8, 2016 18:52
sometimes you just want to visualize a quantity; this shows a given number of dots
# 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),
@arvi1000
arvi1000 / jitterbox.R
Created October 26, 2016 02:18
Roughly replicating Erica's plot
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) +