[R] ggplot & annotating charts

From: simeon duckworth <simeonduckworth_at_gmail.com>
Date: Tue, 04 Nov 2008 14:49:07 +0000


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. Received on Tue 04 Nov 2008 - 14:52:37 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 04 Nov 2008 - 16:30:22 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive