Archive

Tag Archives: geo chart

The googleVis package  provides an interface between R and the Google Visualisation API. With the googleVis package users can easily create web pages with interactive charts based on R data frames and display them either via the local R HTTP help server or within their own sites, without uploading the data to Google. A browser with Flash and Internet connection is required. In this post I will plot some of the data from the previous blog entry using these pretty google widgets. Since wordpress.com does not appear to support javascript unless the blog is hosted on a personal server, I will upload the html file onto dropbox and provide a (hopefully) publicly accessible link below.

Depending on the structure of the data frame, we may have to melt the data before calling the google vis functions. 

kk<-melt(plevels.orig,id=rownames(plevels.orig))
mn<-melt(eff.orig,id=rownames(eff.orig))
k<-kk[,3]
tot<-cbind(mn,k)
colnames(tot)<-c('Country','Year','Relative GDP per Capita in PPP','Relative Price Levels')
M1
nn<-rownames(plevels.orig)
ptemp<-data.frame(Country=nn,plevels.orig)
colnames(ptemp)[1]<-'Country'
colnames(ptemp)[2:17]<-1995:2010
G1
stemp<-data.frame(Country=nn,round(sorted.y,2))
colnames(stemp)[1]<-'Country'
colnames(stemp)[2:17]<-1995:2010
tbl
gt<-gvisMerge(gvisMerge(G1,M1,horizontal=TRUE),tbl)
plot(gt)
print(gt, file="C:/University of Warwick/R experiments/Fx/gvis.html")

k

The above code was simply tagged along to the previous blog post code. The gvis.html file contains the html/java script code which takes care of the rest.  The first plot shows a map of the world, colour coded based on the relative price levels across all countries. Hovering the mouse over the country should yield more exact information. The second plot is a motion chart with many plotting options available. The table on the bottom of the page records the residuals from the Balassa-Samuelson regression (i.e.currency misalignments across time and countries). The Treemap plotted below this table visualises the same data set. The titles for the plots were added on after the html file was generated in R.

The following is a snapshot of the plots followed by the drop box link.

bn

The Treemap was added later. We need at least 4 columns of data : the id of each rectangle (country),the parent of each country (global), the values for the countries (absolute values of currency misalignments in 1995 across 189 countries),the colours for the countries (original values of said misalignments).

parentcol<-rep('Global',189)
ttemp<-data.frame(stemp[,1],parentcol)
pptemp<-data.frame(ttemp,cbind(abs(stemp[,2]),stemp[,2]))
colnames(pptemp)<-c('Country','Parent','val','col')

parentrow<-data.frame(cbind('Global',NA,cbind(0,0)))
colnames(parentrow)<-colnames(pptemp)

tottemp<-rbind(pptemp,parentrow)
Tree
gt<-gvisMerge(gvisMerge(gvisMerge(G1,M1,horizontal=TRUE),tbl),Tree,horizontal=FALSE)
print(gt, file="C:/University of Warwick/R experiments/Fx/gvis.html")

f

The dropbox link is here ::  http://dl.dropbox.com/u/99657020/gvis.html