Skip to content

Instantly share code, notes, and snippets.

@johnjosephhorton
Created August 1, 2012 04:49
Show Gist options
  • Save johnjosephhorton/3223763 to your computer and use it in GitHub Desktop.
Save johnjosephhorton/3223763 to your computer and use it in GitHub Desktop.
india_blackout
library(ggplot2)
library(RPostgreSQL)
library(plyr)
library(scales) # now needed for ggplot2
drv <- dbDriver("PostgreSQL")
con <- dbConnect(drv, dbname='db',
host = 'localhost',
user = 'db',
password ='pwd',
port = '1234')
df.raw <- dbGetQuery(con, "select * from analytics.india_blackout_data") # get data from oDesk
df <- ddply(df.raw, .(date, obs_type), transform, # add standard errors
se = sqrt(frac * (1-frac))/sqrt(n))
df.blackout <- subset(df, date == '2012-07-31') # to label blackout date
df.blackout$label <- "Blackout"
df$weekend <- "" # annotate when the weekends are
df$weekend[df$dow == 6] <- "Sat."
df$weekend[df$dow == 7] <- "Sun."
g <- ggplot(data = df, aes(x = date, y = frac)) +
geom_errorbar(aes(ymin = frac - 2*se, ymax = frac + 2*se)) +
geom_line(aes(x = date, y = frac, group = obs_type),linetype=3) +
facet_wrap(~obs_type, ncol = 1) +
geom_point(data = df.blackout, aes(x = date, y = frac), colour = "red", size = 4) +
geom_text(aes(x = date, y = frac, label = weekend),
size = 4, hjust = 1.7, colour="blue") +
geom_text(data = df.blackout, aes(x = date, y = frac, label = label),
colour = "red", vjust = 3, size = 4) +
scale_y_continuous(labels = percent) +
ylab("Hours worked or applications sent by Indian contractors as a percentage of total") +
theme_bw()
print(g)
png("/home/john/Desktop/india_blackout.png", width = 600)
print(g)
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment