Animated S&P 500 Tree Map

In this post we will animate the S&P 500 tree map across monthly returns. Ideally we would colour code the returns according their level as per this example. Unfortunately there is no easy way to do this using the treemap package without colour coding from scratch. Negative returns were made positive by taking absolute values, hence the square areas of the plot may be misleading.

l

setwd("C:/University of Warwick/R experiments/Tree Map")

library(quantmod)
library(timeSeries)
library(RColorBrewer)
library(treemap)
library(animation)

# Load the Data #############################################################

all.list<-as.matrix(read.table('datacsv.csv',header=T,sep=','))

n<-nrow(all.list)
b<-as.Date('2011-01-01')
e<-as.Date('2012-01-01')

assets<-NULL
for(i in 1:n){
  getSymbols(all.list[i,1],from=b,to=e)
  print(paste('Downloading data for :',all.list[i,1]))
  assets$returns[[i]]<-as.timeSeries(monthlyReturn(get(all.list[i,1])[,4],type='arithmetic')*100)
}

matrix.returns<-NULL
for(i in 1:n){
  matrix.returns<-cbind(matrix.returns,assets$returns[[i]])
}

mat<-t(matrix.returns)
colnames(mat)<-paste('month',seq(12))
rownames(mat)<-all.list[,1]
mat<-substituteNA(mat,type='zeros')

treemap.matrix<-data.frame(cbind(all.list,mat))
write.csv(treemap.matrix,file='map.csv',col.names=T,sep=',')
tmap<-read.csv('map.csv',header=T,sep=',')[,-1]

temp<-data.frame(cbind(all.list,abs(mat)))
write.csv(temp,file='mapabs.csv',col.names=T,sep=',')
tmap.abs<-read.csv('mapabs.csv',header=T,sep=',')[,-1]

#############################################################################

# Draw Tree Map #############################################################

for(i in 3:14){
  png(paste('tmapplot',i-2,'.png',sep=''))
  tmPlot(title=month.name[i-2],palette='Blues',tmap.abs,index=c('Sector','Company'),vSize=paste('month.',i-2,sep=''),vColor=paste('month.',i-2,sep=''),type='linked',fontsize.labels=10,lowerbound.cex.labels=.1)
  dev.off()
}

#ani.options(convert = ('C:/Program Files (x86)/ImageMagick-6.7.8-Q16/convert.exe'))
files = sprintf('tmapplot%d.png',1:12)
im.convert(files, output = 'tanim.gif')

lll

Colour coding the returns gives a more intuitive picture of performance, with the red squares corresponding to negative returns and the green ones representing positive performance.

f

# Draw Tree Map ################################################################

for(i in 3:14){
  map<-makecmap(tmap[,i], n = 0, colFn = colorRampPalette(c('darkred', 'darkgreen')),symm=T)
  myColors <- cmap(tmap[,i], map = map)

  png(paste('tmapplotcol',i-2,'.png',sep=''))
  tmPlot(title=month.name[i-2],palette=myColors,tmap.abs,index=c('Sector','Company'),vSize=paste('month.',i-2,sep=''),vColor=paste('month.',i-2,sep=''),type='linked',fontsize.labels=10,lowerbound.cex.labels=.1)
  dev.off()
}

#ani.options(convert = ('C:/Program Files (x86)/ImageMagick-6.7.8-Q16/convert.exe'))
files = sprintf('tmapplotcol%d.png',1:12)
im.convert(files, output = 'tanimcol.gif')

Using the googleVis package to draw the treemap for january data yields the following plots. The Sector fields are clickable.

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

The Industrials sector contains the following companies

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: