Performance with ggplot2

September 16, 2011
By

(This article was first published on Timely Portfolio, and kindly contributed to R-bloggers)

Now after Reporting Good Enough to Share, let’s use ggplot2 and PerformanceAnalytics to turn this

image4

into this

From TimelyPortfolio

I have been notified that the colors aren’t great.  How does everyone like this?

R code (click to download):

require(quantmod)
require(ggplot2)
require(PerformanceAnalytics)   clientPerf <- read.csv("clientperf.csv",stringsAsFactors=FALSE)
clientPerf[,2:NCOL(clientPerf)] <- lapply(clientPerf[,2:NCOL(clientPerf)],as.numeric)
clientPerf <- as.xts(clientPerf[,2:NCOL(clientPerf)]/100,
order.by = as.Date(clientPerf[,1],format="%m-%d-%y"))
colnames(clientPerf) <- c("Client","BarclaysAgg","SP500")     returns.cumul <- xts(apply((1+clientPerf),MARGIN=2,FUN=cumprod),order.by=index(clientPerf))
returns.cumul.Melt <- melt(as.data.frame(cbind(index(returns.cumul),
coredata(returns.cumul))),id.vars=1)
colnames(returns.cumul.Melt) <- c("Date","Portfolio","Growth")
a<-ggplot(returns.cumul.Melt,stat="identity",
aes(x=Date,y=Growth,colour=Portfolio)) +
geom_line(lwd=1) +
scale_x_date() +
scale_colour_manual(values=c("cadetblue","darkolivegreen3","gray70","bisque3","purple")) +
theme_bw() +
labs(x = "", y = "") +
opts(title = "Cumulative Returns",plot.title = theme_text(size = 20, hjust = 0))   returnTable <- table.TrailingPeriods(clientPerf,
periods=c(12,24,36,NROW(clientPerf)),
FUNCS="Return.annualized")
rownames(returnTable) <- c(paste(c(1:3)," Year",sep=""),"Since Inception")
returnMelt <- melt(cbind(rownames(returnTable),returnTable*100))
colnames(returnMelt)<-c("Period","Portfolio","Value")
b<-ggplot(returnMelt, stat="identity",
aes(x=Period,y=Value,fill=Portfolio)) +
geom_bar(position="dodge") +
scale_fill_manual(values=c("cadetblue","darkolivegreen3","gray70")) +
theme_bw() +
labs(x = "", y = "") +
opts(title = "Returns (annualized)",plot.title = theme_text(size = 20, hjust = 0))     downsideTable<-table.DownsideRisk(clientPerf)[c(1,3,5,7,8),]
downsideMelt<-melt(cbind(rownames(downsideTable),
downsideTable))
colnames(downsideMelt)<-c("Statistic","Portfolio","Value")
c<-ggplot(downsideMelt, stat="identity",
aes(x=Statistic,y=Value,fill=Portfolio)) +
geom_bar(position="dodge") + coord_flip() +
scale_fill_manual(values=c("cadetblue","darkolivegreen3","gray70")) +
theme_bw() +
labs(x = "", y = "") +
opts(title = "Risk Measures",plot.title = theme_text(size = 20, hjust = 0))
#geom_hline(aes(y = 0))
#opts(axis.line = theme_segment(colour = "red"))
#opts(panel.grid.major = theme_line(linetype = "dotted"))   #jpeg(filename="performance ggplot.jpg",quality=100,width=6.5, height = 8, units="in",res=96)
#pdf("perf ggplot.pdf", width = 8.5, height = 11)   grid.newpage()
pushViewport(viewport(layout = grid.layout(3, 1)))   vplayout <- function(x, y)
viewport(layout.pos.row = x, layout.pos.col = y)
print(a, vp = vplayout(1, 1))
print(b, vp = vplayout(2, 1))
print(c, vp = vplayout(3, 1))
#dev.off()

Created by Pretty R at inside-R.org

To leave a comment for the author, please follow the link and comment on his blog: Timely Portfolio.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: ,

Comments are closed.