Skip to content

Instantly share code, notes, and snippets.

View arvi1000's full-sized avatar

Arvi Sreenivasan arvi1000

View GitHub Profile
# 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')
@arvi1000
arvi1000 / r_leaflet_readxl_example.R
Last active May 5, 2016 15:58
Example of reading data from an Excel file, geocoding, simple calculations, and plotting with Leaflet
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')
@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) +
@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 / 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 / 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 / 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 / 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(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 / 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) +