[R] ggplot/time series with indicators question

Chuck White chuckwhite8 at charter.net
Tue Feb 2 03:06:52 CET 2010


Hello, I am trying to plot time-series data with certain weeks highlighted using symbols.

require(ggplot2)

#plotting time series data
timescale <- seq(as.Date("01/01/09","%m/%d/%y"), length.out=12, by=7)
data.all <- data.frame(
    id = c(rep('111',12),rep('222',12),rep('333',12)),
    week=c(timescale,timescale,timescale),
    value=c(rnorm(12,15,3),rnorm(12,30,5),rnorm(12,20,5))
)

p <- ggplot(data.all, aes(x=week, y=value, group=id, colour=id)) + geom_line(size=1.05)

#adding indicators on each line
data.all$ind <- c(rep("",4),rep("2",2),rep("",4),rep("3",2),rep("",2),rep("2",4),rep("",3),rep("6",3),rep("",8),rep("1",4))

ind.uniq = setdiff(unique(data.all$ind),c(""))
names(ind.uniq) <- paste("ind", ind.uniq, sep="")
ind.df <- data.frame(lapply(ind.uniq, function(x) ifelse(data.all$ind==x, data.all$value, NA)))
ind.df <- cbind(data.all[,c("id","week")],ind.df)

How can I add markers based on the data in ind.df? In the above example, there are four indicators 1,2,3 and 6.  I'd like to be able to show each using a different shape (circle, triangle, square, cross). Additionally, I'd like to use the same colors for the markers as the lines they are plotted on.

Thanks.



More information about the R-help mailing list