This file contains 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
# 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'); |
This file contains 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
# 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); |
This file contains 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
# 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 = '')) |
This file contains 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
# 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); |
This file contains 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
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) |
This file contains 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
# 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 |
This file contains 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
# 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]]) | |
} |
This file contains 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
% 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}} |
This file contains 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
# 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)) |
This file contains 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
% 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}% | |
}% |