I am using the quantmod package to pull data on the S&P 500 (SPY) and NASDAQ (IXIC). The quantmod package has very nice graphics and I’m just using this data as practice for plotting in R. I’m going to crudely compare percent growth for the two indexes over the last two years using a couple plots. This post isn’t meant for anything more than exploring plots in R. I’m going to use the base R plotting functions and ggplot2. I used a number of stackoverflow.com posts as references.

library(quantmod)
library(ggplot2)
library(gridExtra)

#Pull data into workspace using quantmod
getSymbols("SPY",src="yahoo",warnings=FALSE)
getSymbols("^IXIC",src="yahoo",warnings=FALSE)

#Restricting timeframe of data
IXIC_timeframe<-index(IXIC$IXIC.Close)>format("2013-06-01",format="%Y-%m-%d") SPY_timeframe<-index(SPY$SPY.Close)>format("2013-06-01",format="%Y-%m-%d")

#Creating dataset with all values in format I want
comp<-data.frame(Date=index(IXIC$IXIC.Close)[IXIC_timeframe], IXIC=round(100*IXIC$IXIC.Close[IXIC_timeframe]/as.numeric(IXIC$IXIC.Close[IXIC_timeframe][1])-100,1), SPY=round(100*SPY$SPY.Close[SPY_timeframe]/as.numeric(SPY$SPY.Close[SPY_timeframe][1])-100,1)) Below is a stacked plot using the base R plotting functions. I set the margins wide on the right side of the graph and also allow for plotting outside of graph using the xpd command inside the par function. I did this so that I can plot an axis on the leftside of the graph and a legend. par(mar=c(5, 5, 2, 7), xpd=TRUE) #Draw IXIC and set up plot environment plot(comp$Date,
comp$IXIC+50, type="l",ylab="", main="",yaxt="n",xlab="Date",ylim=c(0,100),lwd=2) #Draw SPY lines lines(comp$Date,
comp$SPY+50,col=2,lwd=2) legend("topright",inset=c(-0.15,0),c("IXIC","SPY"), col=c(1,2),lty=1,bty="n",cex=0.8,lwd=3) #Take difference between indixes performance indeces_diff<-comp$IXIC-comp$SPY #Plot difference of the indeces lines(comp$Date,
indeces_diff+15,
type="l",xlab="Date",ylab="Difference in Indeces",
main="",lwd=2)
lines(comp$Date, rep(15,length(comp$Date)),
type="l",xlab="Date",ylab="Difference in Indeces",
main="",lwd=1,lty=2)

axis(side=2,at=c(50,70,90),labels=c(0,20,40))
mtext("Perc Change in \nIndeces from 6/1/013", side=2, line= 2.5, at=70)

axis(side=4,at=c(0,15,30),labels=c(-15,0,15))
mtext("Perc Diff Between \nIXIC and SPY", side=4, line= 3, at=15)

#Adding a summary statistics to the bottom of the plot frame
mtext(paste("Perc Diff on 6/1/2015: ",indeces_diff[comp$Date=="2015-06-01"],sep=""), side=1,line=3,adj=0, cex=0.8) Below is the stacked plot using ggplot. The grid.arrange function allows plotting more than one plot on a canvas using ggplot2. These can be found in the gridExtra package. #Formatting data for ggplot2 comp2<-data.frame(Date=rep(comp$Date,2),
Perc=c(comp$SPY,comp$IXIC),
Indeces=c(rep("SPY",length(comp$SPY)),rep("IXIC",length(comp$IXIC))),
stringsAsFactors=FALSE)

#Plot first graph
p1<-ggplot(comp2,aes(x=Date,y=Perc,color=Indeces))+
geom_line(size=1.1)+
scale_color_manual(values=c("#000000","red"))+
scale_y_continuous("Perc Change in \nIndeces from 6/1/013")+
scale_x_date("")+
theme_bw()+
theme(legend.justification=c(1,0), legend.position=c(1,0),
axis.text.x=element_blank(),axis.ticks=element_blank(),
plot.margin = unit(c(1,1,-0.5,1), "cm"))

#Plot second graph
p2<-ggplot(comp,aes(x=Date,y=IXIC.Close-SPY.Close))+
geom_line(size=1.1)+
scale_y_continuous("Perc Diff Between \nIXIC and SPY")+
theme_bw()+
theme(plot.margin = unit(c(-0.5,1,1,1), "cm"))

#Call grid.arrange to plot both graphs in same frame
grid.arrange(p1,p2,ncol=1,main="Comparison of S&P 500 and NASDAQ (6/1/2013-6/1/2015)")

A next step would be adding further summary statistics using a table format. Possibly using tableGrob from RGraphics package. I’ll save that for another example…