Graphics Artifacts from Quarterly Commentary
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
For my Q2 2012 commentary, I tried multiple graphs to illustrate the disconnect of the US stock markets with the rest of the world. I think I finally settled on this simple Excel bar graph populated by Bloomberg data, but I thought some might like to see some of the R graphical artifacts as I explored how best to illustrate my point.
![]() |
From TimelyPortfolio |
Although I settled on a 1 year performance chart, I really wanted to show more history, but without all the noise of a daily, weekly, or even monthly chart. I tried a chart connecting the 200 day max/min points, but it still seemed noisy and difficult to follow. One big letdown was Yahoo! Finance not providing DJUBS, E1DOW, or P1DOW any more.
![]() |
From TimelyPortfolio |
![]() |
From TimelyPortfolio |
I then thought connecting the end of year points might offer a nice simplification, and I probably liked this best of the R charts. This shows enough of the path to see the universal move up to 2010, and then the disconnect.
![]() |
From TimelyPortfolio |
![]() |
From TimelyPortfolio |
Even one R base graphics chart. Thanks again Josh for the fork.
![]() |
From TimelyPortfolio |
As one other option, I thought I would try to just connect beginning, middle, and end since December 2008. This certainly shows the huge disconnect between the U.S. and the rest of the world, but I found it difficult to describe the methodology within the chart.
![]() |
From TimelyPortfolio |
![]() |
From TimelyPortfolio |
I hope this helps someone somewhere. As always, I very much enjoy comments and opinions.
R code in GIST (do raw for copy/paste):
require(lattice) | |
require(latticeExtra) | |
require(directlabels) | |
require(ggplot2) | |
require(reshape2) | |
require(quantmod) | |
require(PerformanceAnalytics) | |
tckrs <- c("SPY","IWM","EWP","EFA","CRB") #will have to get CRB from Systematic Investor | |
descr <- c("SP500","Russell2000","Spain","EAFE","CRB") | |
#get equity indexes | |
getSymbols(tckrs[1:4],from="2008-12-01") | |
#and since Yahoo no longer provides ^DJUBS historical prices | |
#use Systematic Investor | |
con = gzcon(url('http://www.systematicportfolio.com/sit.gz', 'rb')) | |
source(con) | |
close(con) | |
CRB <- get.CRB()["2008-12-01::",] | |
getmaxmin <- function(prices,n=100) { | |
dc <- DonchianChannel(prices[,4],n) | |
maxmin <- rbind(prices[1,4], | |
prices[which(prices[,4]==dc[,1]),4], | |
prices[which(prices[,4]==dc[,3]),4], | |
prices[NROW(prices),4])/as.numeric(prices[1,4]) | |
maxmin <- as.data.frame(cbind(as.Date(index(maxmin)),coredata(maxmin)),stringsAsFactors=FALSE) | |
colnames(maxmin) <- c("date","price") | |
return(maxmin) | |
} | |
n=200 | |
df <- as.data.frame(cbind(descr[1],getmaxmin(get(tckrs[1]),n)),stringsAsFactors=FALSE) | |
colnames(df)[1] <- "index" | |
for (i in 2:length(tckrs)) { | |
temp <- as.data.frame(cbind(descr[i],getmaxmin(get(tckrs[i]),n)),stringsAsFactors=FALSE) | |
colnames(temp)[1] <- "index" | |
df <- rbind(df,temp) | |
} | |
direct.label( | |
xyplot(price~as.Date(date),groups=index,data=df,lwd=3,type="l",main="Path of World Markets Since Dec 2008"), | |
list("last.qp",hjust=0.35,vjust=-0.25,cex=0.75)) | |
direct.label( | |
ggplot(aes(y=price,x=as.Date(date)),data=df) + geom_line(aes(colour=index)) + theme_bw() + opts(legend.position = "none") + | |
#ggplot(aes(y=price,x=as.Date(date)),data=indexes.melt) + geom_smooth(aes(colour=indexes)) + theme_bw() + opts(legend.position = "none") + | |
opts(panel.grid.minor = theme_blank()) + | |
opts(axis.line = theme_segment()) + | |
opts(panel.border = theme_blank()) + | |
opts(title="Path of World Indexes Since 2008") | |
,list("last.qp",hjust=0.75,vjust=-0.25,cex=0.75)) | |
#do beginning, middle, and end | |
getbeginend <- function(prices,middle=TRUE) { | |
dc <- DonchianChannel(prices[,4],n) | |
beginend <- rbind(prices[1,4], | |
prices[NROW(prices)/2,4], | |
prices[NROW(prices),4])/as.numeric(prices[1,4]) | |
beginend <- as.data.frame(cbind(as.Date(index(beginend)),coredata(beginend)),stringsAsFactors=FALSE) | |
if(middle==FALSE) | |
beginend <- beginend[c(1,3),] | |
colnames(beginend) <- c("date","price") | |
return(beginend) | |
} | |
df <- as.data.frame(cbind(descr[1],getbeginend(get(tckrs[1]),n)),stringsAsFactors=FALSE) | |
colnames(df)[1] <- "index" | |
for (i in 2:length(tckrs)) { | |
temp <- as.data.frame(cbind(descr[i],getbeginend(get(tckrs[i]),n)),stringsAsFactors=FALSE) | |
colnames(temp)[1] <- "index" | |
df <- rbind(df,temp) | |
} | |
asTheEconomist(direct.label(xyplot(price~as.Date(date),groups=index,data=df,lwd=3,type="l",main="Change Since Dec 2008"), | |
"last.qp")) | |
direct.label( | |
ggplot(aes(y=price,x=as.Date(date)),data=df) + geom_line(aes(colour=index)) + theme_bw() + opts(legend.position = "none") + | |
#ggplot(aes(y=price,x=as.Date(date)),data=indexes.melt) + geom_smooth(aes(colour=indexes)) + theme_bw() + opts(legend.position = "none") + | |
opts(panel.grid.minor = theme_blank()) + | |
opts(axis.line = theme_segment()) + | |
opts(panel.border = theme_blank()) + | |
opts(title="Change of Indexes Since 2008") | |
,list("last.qp",hjust=0.75,vjust=-0.25,cex=0.75)) | |
#do yearly | |
indexes <- as.data.frame(to.yearly(get(tckrs[1]))[,4]) | |
for (i in 2:length(tckrs)) { | |
indexes <- cbind(indexes,as.data.frame(to.yearly(get(tckrs[i]))[,4])) | |
} | |
colnames(indexes) <- descr | |
indexes.roc <- ROC(indexes,type="discrete",n=1) | |
indexes.roc[1,] <- 0 | |
labs <- Return.cumulative(indexes.roc) | |
#thanks for the fork http://blog.fosstrading.com/ | |
par(mar=c(4,4,4,5)) | |
chart.CumReturns(indexes.roc[,order(labs)],ylab=NA,xlab=NA,colorset=1:5,main="Path of World Indexes Since Dec 2008") | |
axis(side=4,at=labs,labels=FALSE,las=1,cex.axis=0.75,lwd=0,lwd.ticks=0.5,col.ticks="black",line=-0.25) | |
mtext(colnames(labs)[order(labs)],cex=0.75, 4, at=labs[order(labs)],las=1,col=1:5,line=1) | |
indexes.cumul <- apply(indexes.roc+1,MARGIN=2,cumprod) | |
indexes.melt <- melt(as.data.frame(cbind(as.Date(rownames(indexes.cumul)),indexes.cumul),stringsAsFactors=FALSE), | |
id.vars=1) | |
colnames(indexes.melt) <- c("date","indexes","price") | |
direct.label(asTheEconomist( | |
xyplot(price~as.Date(date),groups=indexes,data=indexes.melt,type="l",main="Path of World Indexes Since Dec 2008")) | |
,list("last.qp",hjust=0.35,vjust=0.1,cex=1)) | |
direct.label( | |
ggplot(aes(y=price,x=as.Date(date)),data=indexes.melt) + geom_line(aes(colour=indexes)) + theme_bw() + opts(legend.position = "none") + | |
#to smooth use the next line instead | |
#ggplot(aes(y=price,x=as.Date(date)),data=indexes.melt) + geom_smooth(aes(colour=indexes)) + theme_bw() + opts(legend.position = "none") + | |
opts(panel.grid.minor = theme_blank()) + | |
opts(axis.line = theme_segment()) + | |
opts(panel.border = theme_blank()) + | |
opts(title="Path of World Indexes Since Dec 2008") | |
,list("last.qp",hjust=0.75,vjust=-0.25,cex=0.75)) | |
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.