Dear "R-listers" I've been trying to figure out how to annotate charts in ggplot (ie add text to line charts, highlighted boxes etc). By and large, I can get close to what i want with base graphics, but would ideally like to use ggplot whenever possible (additionally, i would like to add text labels automatically to the chart). The code is below I suspect I need to use geom_rect, but what is foxing me is how to set up the aes() parameters. I'd be very grateful for any help thanks simeon ################### require(zoo) require(chron) vix <-read.csv(" http://www.cboe.com/publish/ScheduledTask/MktData/datahouse/vixcurrent.csv", header=T,sep=",", na.strings="") names(vix) <- tolower(names(vix)) vix$dates <- chron(as.character(vix$date),out.format="d-mon-y") vix.z <- zoo(vix[,-1],vix$dates) vix.close1<-aggregate(vix.z[,4],as.yearmon,max) # 1990 - 2003 vix.ar1 <- read.csv("C:/Data/MindShare/Recession/vixarchive.csv", header=T,sep=",", quote="",na.strings="n/a") names(vix.ar1) <- tolower(names(vix.ar1)) vix.ar1$dates <- chron(as.character(vix.ar1$date),out.format="d-mon-y") vix.ar1.z <- zoo(vix.ar1[,-1],vix.ar1$dates) vix.close2<-aggregate(vix.ar1.z[,4],as.yearmon,max) vix.close<-rbind(vix.close1,vix.close2) op <- par(mar = c(6, 8, 6, 6) + 0.1) gulf1 <- as.yearmon("1990-8-2") gulf2 <- as.yearmon("1991-2-28") asia1 <- as.yearmon("1997-9-11") asia2 <- as.yearmon("1998-11-14") sep1 <- as.yearmon("2001-9-11") sep2 <- as.yearmon("2002-9-14") crunch1 <- as.yearmon("2007-8-9") crunch2 <- as.yearmon("2009-11-14") plot(vix.close, type = "n",ylab="S&P500 volatility", xlab="Date",cex.axis=1.5,cex.lab=1.7,ylim=c(0,90) ) rect(gulf1, par("usr")[3], gulf2, par("usr")[4],col=rgb(195,188,175,maxColorValue=255),border=0) rect(asia1, par("usr")[3], asia2, par("usr")[4],col=rgb(195,188,175,maxColorValue=255),border=0) rect(sep1, par("usr")[3], sep2, par("usr")[4], col=rgb(195,188,175,maxColorValue=255),border=0) rect(crunch1, par("usr")[3], crunch2, par("usr")[4], col=rgb(195,188,175,maxColorValue=255), border = 0) lines(vix.close,lwd=3,col=rgb(45,12,75,maxColorValue=255)) title(main="US STOCK MARKET VOLATILITY 1962-2008 ",adj=0,cex.main=2.3) [[alternative HTML version deleted]]
Hi Simeon, Could you publish vixarchive.csv too please? Do you have a start on the version in ggplot2? It would be useful to see that code too. Hadley On Tue, Nov 4, 2008 at 8:49 AM, simeon duckworth <simeonduckworth at gmail.com> wrote:> Dear "R-listers" > > I've been trying to figure out how to annotate charts in ggplot (ie add text > to line charts, highlighted boxes etc). By and large, I can get close to > what i want with base graphics, but would ideally like to use ggplot > whenever possible (additionally, i would like to add text labels > automatically to the chart). The code is below > > I suspect I need to use geom_rect, but what is foxing me is how to set up > the aes() parameters. > > I'd be very grateful for any help > > thanks > > simeon > > ################### > require(zoo) > require(chron) > > vix <-read.csv(" > http://www.cboe.com/publish/ScheduledTask/MktData/datahouse/vixcurrent.csv", > header=T,sep=",", na.strings="") > names(vix) <- tolower(names(vix)) > vix$dates <- chron(as.character(vix$date),out.format="d-mon-y") > vix.z <- zoo(vix[,-1],vix$dates) > vix.close1<-aggregate(vix.z[,4],as.yearmon,max) > > # 1990 - 2003 > vix.ar1 <- read.csv("C:/Data/MindShare/Recession/vixarchive.csv", > header=T,sep=",", quote="",na.strings="n/a") > names(vix.ar1) <- tolower(names(vix.ar1)) > vix.ar1$dates <- chron(as.character(vix.ar1$date),out.format="d-mon-y") > vix.ar1.z <- zoo(vix.ar1[,-1],vix.ar1$dates) > vix.close2<-aggregate(vix.ar1.z[,4],as.yearmon,max) > vix.close<-rbind(vix.close1,vix.close2) > > op <- par(mar = c(6, 8, 6, 6) + 0.1) > gulf1 <- as.yearmon("1990-8-2") > gulf2 <- as.yearmon("1991-2-28") > asia1 <- as.yearmon("1997-9-11") > asia2 <- as.yearmon("1998-11-14") > sep1 <- as.yearmon("2001-9-11") > sep2 <- as.yearmon("2002-9-14") > crunch1 <- as.yearmon("2007-8-9") > crunch2 <- as.yearmon("2009-11-14") > plot(vix.close, type = "n",ylab="S&P500 volatility", > xlab="Date",cex.axis=1.5,cex.lab=1.7,ylim=c(0,90) ) > rect(gulf1, par("usr")[3], gulf2, > par("usr")[4],col=rgb(195,188,175,maxColorValue=255),border=0) > rect(asia1, par("usr")[3], asia2, > par("usr")[4],col=rgb(195,188,175,maxColorValue=255),border=0) > rect(sep1, par("usr")[3], sep2, par("usr")[4], > col=rgb(195,188,175,maxColorValue=255),border=0) > rect(crunch1, par("usr")[3], crunch2, par("usr")[4], > col=rgb(195,188,175,maxColorValue=255), border = 0) > lines(vix.close,lwd=3,col=rgb(45,12,75,maxColorValue=255)) > title(main="US STOCK MARKET VOLATILITY 1962-2008 ",adj=0,cex.main=2.3) > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- http://had.co.nz/
Note that even without substantial changes the code could be shortened by making use of read.zoo and other items illustrated below: library(zoo) vix.url <- "http://www.cboe.com/publish/ScheduledTask/MktData/datahouse/vixcurrent.csv" v <- read.zoo(vix.url, header = TRUE, sep = ",", format = "%m/%d/%Y", FUN = as.yearmon, aggregate = max)[, 4] plot(v, type = "n", ylab = "S&P Volatility", xlab = "Date") # might want to make next two lines into function u <- par("usr") rect(as.yearmon(2007-8"), u[3], as.yearmon("2009-11"), u[4], border = 0, col = "grey") lines(v) axis(1, floor(time(v)), labels = FALSE, tcl = -1) There is also an example of background shadings in ?xyplot.zoo On Tue, Nov 4, 2008 at 9:49 AM, simeon duckworth <simeonduckworth at gmail.com> wrote:> Dear "R-listers" > > I've been trying to figure out how to annotate charts in ggplot (ie add text > to line charts, highlighted boxes etc). By and large, I can get close to > what i want with base graphics, but would ideally like to use ggplot > whenever possible (additionally, i would like to add text labels > automatically to the chart). The code is below > > I suspect I need to use geom_rect, but what is foxing me is how to set up > the aes() parameters. > > I'd be very grateful for any help > > thanks > > simeon > > ################### > require(zoo) > require(chron) > > vix <-read.csv(" > http://www.cboe.com/publish/ScheduledTask/MktData/datahouse/vixcurrent.csv", > header=T,sep=",", na.strings="") > names(vix) <- tolower(names(vix)) > vix$dates <- chron(as.character(vix$date),out.format="d-mon-y") > vix.z <- zoo(vix[,-1],vix$dates) > vix.close1<-aggregate(vix.z[,4],as.yearmon,max) > > # 1990 - 2003 > vix.ar1 <- read.csv("C:/Data/MindShare/Recession/vixarchive.csv", > header=T,sep=",", quote="",na.strings="n/a") > names(vix.ar1) <- tolower(names(vix.ar1)) > vix.ar1$dates <- chron(as.character(vix.ar1$date),out.format="d-mon-y") > vix.ar1.z <- zoo(vix.ar1[,-1],vix.ar1$dates) > vix.close2<-aggregate(vix.ar1.z[,4],as.yearmon,max) > vix.close<-rbind(vix.close1,vix.close2) > > op <- par(mar = c(6, 8, 6, 6) + 0.1) > gulf1 <- as.yearmon("1990-8-2") > gulf2 <- as.yearmon("1991-2-28") > asia1 <- as.yearmon("1997-9-11") > asia2 <- as.yearmon("1998-11-14") > sep1 <- as.yearmon("2001-9-11") > sep2 <- as.yearmon("2002-9-14") > crunch1 <- as.yearmon("2007-8-9") > crunch2 <- as.yearmon("2009-11-14") > plot(vix.close, type = "n",ylab="S&P500 volatility", > xlab="Date",cex.axis=1.5,cex.lab=1.7,ylim=c(0,90) ) > rect(gulf1, par("usr")[3], gulf2, > par("usr")[4],col=rgb(195,188,175,maxColorValue=255),border=0) > rect(asia1, par("usr")[3], asia2, > par("usr")[4],col=rgb(195,188,175,maxColorValue=255),border=0) > rect(sep1, par("usr")[3], sep2, par("usr")[4], > col=rgb(195,188,175,maxColorValue=255),border=0) > rect(crunch1, par("usr")[3], crunch2, par("usr")[4], > col=rgb(195,188,175,maxColorValue=255), border = 0) > lines(vix.close,lwd=3,col=rgb(45,12,75,maxColorValue=255)) > title(main="US STOCK MARKET VOLATILITY 1962-2008 ",adj=0,cex.main=2.3) > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >