Skip to content

Instantly share code, notes, and snippets.

@cigrainger
Created February 10, 2014 17:02
Show Gist options
  • Save cigrainger/8919922 to your computer and use it in GitHub Desktop.
Save cigrainger/8919922 to your computer and use it in GitHub Desktop.
# Load required packages
library(reshape2)
library(zoo)
library(data.table)
library(ggplot2)
library(lattice)
library(foreach)
library(doParallel)
# Import data and prepare data frames
load("~/AeroFS/Googlestuff/googleraw20131016/queryres.rdata")
df <- OUT
rm(OUT)
df$nrow <- NULL
longdf <- melt(df, id.vars = c('word','chis','time'))
names(longdf)[[4]] <- 'country'
month.numeric <- seq(min(df$time),max(df$time))
# month <- seq(startdate,enddate,by='month')
monthlookup <- data.frame(time=month.numeric,month=month)
longdt <- data.table(longdf)
monthdt <- data.table(monthlookup)
dt <- merge.data.table(longdt,monthdt)
longdf <- data.frame(dt)
# Create ts plots
workers <- makeCluster(2)
registerDoParallel(workers)
setwd('~/Documents/tstest/')
foreach(i=unique(longdf$country)) %:%
foreach(j=unique(longdf$word)) %dopar% {
plot <- ggplot(longdf[longdf$country==i & longdf$word==j,], aes(x=time,y=value)) + geom_line()
png(paste(i,j,'.png',sep=''),width=480,height=240,res=120)
print(plot)
dev.off()
print(paste(getwd(),i,j,'.png', sep=''))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment