Skip to content

Instantly share code, notes, and snippets.

View ramnathv's full-sized avatar

Ramnath Vaidyanathan ramnathv

View GitHub Profile
# Find winner and percent of votes secured
candidates = names(df)[4:9];
df$winner = candidates[apply(df[, candidates], 1, which.max)];
df$winpct = apply(df[, candidates], 1, max);
# Assign palettes to winners
df$colpal = sapply(df$winner, switch,
'braun' = 'PuRd', 'chico' = 'Greens',
'delvalle' = 'Oranges', 'emanuel' = 'Purples');
# London Sport Participation
london <- readShapeSpatial('1_data/maps/london_sport.shp');
choro4 = plotChoropleth(.poly = london,
id1 = 'name', field = 'Partic_Per',
title = 'London Sport Participation'
legtitle = 'Partic_Per',
colpal = 'Blues', fixed = T,
breaks = seq(0, 30, by = 5)) +
opts(legend.position = c(0.90, 0.25);
# Animated Choropleth of Population Density Change
# read data with population
df = read.csv('http://goo.gl/yJQs8');
names(df) = sub('Persons.', '', names(df));
# read data with areas
df_area = read.xls('http://goo.gl/UJbKE',
sheet = 2, pattern = 'Code')[,c('Code', 'Total')];
df_area$Total = with(df_area, sapply(as.character(Total), sub, pa = ",", r = ''))
# FUNCTION TO CREATE A CHOROPLETH MAP USING SPPLOT
choropleth_sp <- function(.poly, id1, .df = .poly@data, id2 = id1, field,
.title = "", .legtitle = "Values", colpal = 'PuRd', ...){
# load required libraries
library(maptools); library(spatial); library(RColorBrewer);
library(classInt); library(ggplot2); gpclibPermit();
# create id to match shapefile with data
id1 = llply(id1, as.name);
@ramnathv
ramnathv / gist:1155074
Created August 18, 2011 20:17
Monthplot with 3 Series
require(ggplot2)
require(lubridate)
# df is the data frame on the page http://goo.gl/MvG02 in the answer by Andrie
# this solution modifies his approach to allow multiple time series to be
# incorporated into the month plot
df = transform(df, values2 = values + 100, values3 = values + 200)
dfm = melt(df, id = 'dates')
dfm$month <- factor(month(dfm$dates), levels=1:12, labels=month.abb, ordered=TRUE)
dfm$year <- year(dfm$dates)
@ramnathv
ramnathv / auto_plant_loc.R
Created September 29, 2011 18:07
Map of US Auto Plant Locations
# LOAD LIBRARIES ---------------------------------------------------------------
library(ggplot2)
library(maps)
# LOAD DATA --------------------------------------------------------------------
# read data and add column names
auto_plants = read.csv('Data.csv', as.is = TRUE,
col.names = c('location', 'firm', 'lat', 'long', 'Production', 'temparature'))
# classify firm as others if total plants < 5
@ramnathv
ramnathv / kiplingers_refactored.R
Created November 14, 2011 07:09
Kiplinger's Best Value Rankings (Refactored)
# REFACTORED VERSION OF CODE IN GIST: git://gist.github.com/1362541.git
# FUNCTION TO GET TABLE BASED ON COLLEGE TYPE
get_table <- function(college_type){
require(XML)
url_base <- "http://www.kiplinger.com/tools/privatecolleges/index.php?table"
url_1 <- paste(url_base, college_type, sep = "=")
tab <- readHTMLTable(url_1, header = TRUE, stringsAsFactors = FALSE)
return(tab[[1]])
}
@ramnathv
ramnathv / color-sec-heads.tex
Created January 30, 2012 19:27
Define Colors for Section Headings
% DEFINE COLORS FOR SECTION HEADINGS ----
\titleformat*{\section}{\sffamily\Large\bfseries\color{DarkRed}}
\titleformat*{\subsection}{\sffamily\large\bfseries\color{DarkBlue}}
\titleformat*{\subsubsection}{\sffamily\normalsize\bfseries\color{DarkGreen}}
@ramnathv
ramnathv / dollar_growth_chart.R
Created February 7, 2012 15:21
Growth of $1 Chart using ggplot2
# LOAD LIBRARIES
require(quantmod)
require(ggplot2)
# GET DATA, CONVERT TO DATA FRAMES
getSymbols("VBMFX", from = "1990-01-01", to = Sys.Date(), adjust = TRUE)
getSymbols("VFINX", from = "1990-01-01", to = Sys.Date(), adjust = TRUE)
vbfmx <- data.frame(VBMFX, date = index(VBMFX))
vfinx <- data.frame(VFINX, date = index(VFINX))
@ramnathv
ramnathv / ext-env.tex
Created February 26, 2012 03:39
Collection of knitr Hooks
% Full Screen Environment for Beamer Slides
\newenvironment{changemargin}[2]{%
\begin{list}{}{%
\setlength{\topsep}{0pt}%
\setlength{\leftmargin}{#1}%
\setlength{\rightmargin}{#2}%
\setlength{\listparindent}{\parindent}%
\setlength{\itemindent}{\parindent}%
\setlength{\parsep}{\parskip}%
}%