Skip to content

Instantly share code, notes, and snippets.

@JoFrhwld
Created October 31, 2011 22:10
Show Gist options
  • Save JoFrhwld/1329187 to your computer and use it in GitHub Desktop.
Save JoFrhwld/1329187 to your computer and use it in GitHub Desktop.
Making a google motion chart
##########
## Block 1: Preparing the data
library(plyr)
library(reshape)
## Create a Subsystem Column
all_philly$Subsystem <- all_philly$VClass
levels(all_philly$Subsystem) <- c("Vhr", "Vw", "V", "misc", "Vh", "Vh", "Vhr", "Vw", "Vy",
"Vy", "V", "Vy", "Vy", "Vy", "V", "Vw", "Vy", "Vy", "Vhr",
"V", "Vh", "Vw", "Vw", "Vhr", "Vy", "V", "V", "Vw", "Vhr")
all_philly$Manner <- as.character(all_philly$Manner)
all_philly$Manner[is.na(all_philly$Manner)] <- ""
## Excluding following /l/ and /r/, unless the vowel is in the Vhr class
to.mean <- subset(all_philly, Manner != "lateral")
vhr <- subset(all_philly, Subsystem == "Vhr")
to.mean <- subset(to.mean, Manner != "central" & Subsystem != "Vhr")
to.mean <- rbind(to.mean, vhr)
## This is a messy way of getting around a bug in plyr
data.l <- dlply(to.mean, speaker.id, function(x)x, .progress = "text")
means1.l <- llply(data.l, function(x){
ddply(x, .(VClass, Subsystem), summarise, F1 = mean(F1.n), F2 = mean(F2.n))
}, .progress = "text")
means1.df <- ldply(means1.l, function(x)x, .progress = "text")
data.m <- melt(means1.df, id = c("Name", "File","DOB","VClass", "Subsystem"), measure = c("F1","F2"))
###############
########
## Block 2: Fitting the smoothing lines
loess.mods <- dlply(data.m, .(VClass, Subsystem, variable), with, loess(value ~ DOB, span = 0.75), .progress = "text")
pred <- data.frame(DOB = sort(unique(data.m$DOB)))
loess.preds.m <- ldply(loess.mods, function(x, pred){
pred$value <- predict(x, newdata = pred)
return(pred)
}, pred = pred)
loess.preds.c <- cast(loess.preds.m, VClass + Subsystem + DOB ~ variable)
loess.preds.c <- loess.preds.c[ ,c("F2","F1","DOB","VClass","Subsystem")]
loess.preds.c <- transform(loess.preds.c, F1 = -F1, F2 = -F2)
############
#############
## Block 3: Making the motion chart
library(googleVis)
philly.motion <- gvisMotionChart(loess.preds.c, idvar ="VClass", timevar = "DOB")
print(philly.motion, file = "phillymotionchart.html")
#############
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment