Dynamic charts with googleVis

Dynamic charts are a nice way to visualize patterns over time. With the googleVis package this kind of graph has become easy to create. In this post, I will use the googleVis package to create two interactive moving bubble charts from the Global Terrorism Database (GTD).  

The GTD can after registration be downloaded here

 

The procedure is straightforward. The data only needs to be transformed into a format that gvisMotionChart() can handle. The requirement is to have ident and time-variables in unique combinations. We also need corresponding x- and y-values of each observation (see the Fruits dataframe from gVis for an example). In this way, ggplot2 is a little more convenient to use since it automatically aggregates the data for us.

 

The gvisMotionChart() yields Java-code that can be directly implemented into any website (see below). Depending on the browser and settings the graph can sometimes be a little unstable. 

Code (the data transformation may not be the most efficient way):

require(plyr)
require(reshape)
require(stats)</code>

gtd<-read.csv("globalterrorismdb_0611dist.csv",header=TRUE,sep=";") ##Create Motion Plot for Attacktypes gtd[,c("iyear","region","attacktype1","nkill")]->gtd.r
ddply(gtd.r,.(attacktype1,iyear))->g1
g1$count<-rep(1,nrow(g1))
g1<-g1[,c("attacktype1","iyear","nkill","count")]

g1$attacktype1<-ifelse(g1$attacktype1==4,4,g1$attacktype)
g1$attacktype1<-ifelse(g1$attacktype1==5,4,g1$attacktype)
g1$attacktype1<-ifelse(g1$attacktype1==6,4,g1$attacktype)
g1$attacktype1<-ifelse(g1$attacktype1==7,5,g1$attacktype)
g1$attacktype1<-ifelse(g1$attacktype1==8,5,g1$attacktype)
g1$attacktype1<-ifelse(g1$attacktype1==9,5,g1$attacktype)

g2<-transform(g1,b=interaction(attacktype1,iyear))
g3<-aggregate(.~b,data=g2[,-(1:2)],sum)
g3$attacktype<-substr(g3$b,start=1,stop=1)
g3$year<-substr(g3$b,start=3,stop=6)
g4<-g3[,-1]
g4<-g4[,c("year","attacktype","nkill","count")]
g4$year<-as.Date(paste(g4$year,"01","01",sep="-"))

g4$attacktype<-ifelse(g4$attacktype==1,"Assasination",g4$attacktype)
g4$attacktype<-ifelse(g4$attacktype==2,"Armed Assault",g4$attacktype)
g4$attacktype<-ifelse(g4$attacktype==3,"Bombing / Explosion",g4$attacktype)
g4$attacktype<-ifelse(g4$attacktype==4,"Hostage Taking / Hijacking",g4$attacktype)
g4$attacktype<-ifelse(g4$attacktype==5,"Other ",g4$attacktype)

M1<-gvisMotionChart(data=g4,idvar="attacktype",timevar="year",xvar="nkill",yvar="count") print(M1,'chart',file="mc1.txt") ##Create Motion Plot for Regions ddply(gtd.r,.(region,iyear))->G1
G1$count<-rep(1,nrow(G1))
G1<-G1[,c("region","iyear","nkill","count")]

G1$region<-ifelse(G1$region==1,1,G1$region)
G1$region<-ifelse(G1$region==2,2,G1$region)
G1$region<-ifelse(G1$region==3,2,G1$region)
G1$region<-ifelse(G1$region==4,3,G1$region)
G1$region<-ifelse(G1$region==5,3,G1$region)
G1$region<-ifelse(G1$region==6,3,G1$region)
G1$region<-ifelse(G1$region==7,3,G1$region)
G1$region<-ifelse(G1$region==8,4,G1$region)
G1$region<-ifelse(G1$region==9,4,G1$region)
G1$region<-ifelse(G1$region==10,5,G1$region)
G1$region<-ifelse(G1$region==11,5,G1$region)
G1$region<-ifelse(G1$region==12,6,G1$region)
G1$region<-ifelse(G1$region==13,3,G1$region)

G2<-transform(G1,b=interaction(iyear,region))
G3<-aggregate(.~b,data=G2[,-(1:2)],sum)
G3$region<-substr(G3$b,start=6,stop=7)
G3$year<-substr(G3$b,start=1,stop=4)
G4<-G3[,-1]
G4<-G4[,c("year","region","nkill","count")]
G4$year<-as.Date(paste(G4$year,"01","01",sep="-"))

G4$region<-ifelse(G4$region==1,"North America",G4$region)
G4$region<-ifelse(G4$region==2,"Latin America",G4$region)
G4$region<-ifelse(G4$region==3,"Asia and Oceania",G4$region)
G4$region<-ifelse(G4$region==4,"Europe",G4$region)
G4$region<-ifelse(G4$region==5,"Africa",G4$region)
G4$region<-ifelse(G4$region==6,"Russia",G4$region)

M2<-gvisMotionChart(data=G4,idvar="region",timevar="year",xvar="nkill",yvar="count")

print(M2,'chart',file="mc2.txt")

Leave a Reply

Your email address will not be published. Required fields are marked *