When I first saw Noam Ross’ blog post “The null model for age effects with overdispersed infection”, I immediately liked the look of his ggplot2 graphs. I was even more delighted when I discovered that he has made his theme available on github. Even though I am all into rCharts, I still love a beautiful publication quality R graphic.
Below is some code to combine Noam Ross’ theme with
autoplot.zoo which makes plotting
xts/zoo objects with ggplot2 easy.
require(ggplot2)<br />require(grid)<br />require(RColorBrewer)<br />require(quantmod)<br /><br /># get closing values for S&P 500 from Yahoo! Finance<br />sp500 <- getSymbols("^GSPC", auto.assign = FALSE)[, 4]<br />
<br />sp500$ma <- runMean(sp500, n = 200)<br /><br />colnames(sp500) <- c("S&P500", "200d Mov Avg")<br /><br /># get noam ross ggplot2 theme from github<br />source("https://raw.github.com/noamross/noamtools/master/R/theme_nr.R")<br /># for some reason on my computer panel.background still shows up gray this<br /># fixes it but might not be necessary for others<br />theme_nr <- theme_nr() + theme(panel.background = element_rect(fill = "white")) + <br /> theme(plot.title = element_text(size = rel(2), hjust = -0.05, vjust = -0.3)) + <br /> theme(plot.margin = unit(c(1, 1, 2, 1), "cm"))<br /><br />autoplot(sp500, facets = NULL) + theme_nr + theme(legend.position = "none") + <br /> scale_colour_manual(values = brewer.pal("Blues", n = 9)[c 1="3)" language="(6,"][/c]) + geom_line(size = 1.15) + <br /> xlab(NULL) + ggtitle("S&P 500 (ggplot2 theme by Noam Ross)")<br />
It isn’t perfect, but I think it offers a very nice starting point. Using ggplot2 directly would have allowed us more control over the bothersome details.