2012年5月6日星期日

time series plot with ggplot2 - a simple example


01meetup <- read.csv('MeetupDates.csv', as.is=T)
02names(meetup) <- 'Dates'
03meetup$Dates <- as.Date(meetup$Dates,format='%m/%d/%y')
04files  <- dir(pattern='DC_useR')
05bl <- list()
06for(f in files){
07  bl[[f]] <- read.csv(f, as.is=T)
08  bl[[f]]$Date <- as.Date(bl[[f]]$Date,format='%m/%d/%y')
09}
10dat <- Reduce(function(x,y) merge(x,y), bl) # Merge the data frames by Date
11dat2 <- melt(dat,id=1)
12 
13# Here comes the trick !!
14f1 <- ggplot(dat2, aes(x=Date,y=value,ymin=0,ymax=value))+facet_grid(variable~., scales='free')
15f2 <- f1+geom_step(subset=.(variable=='Total.Members'))
16f3 <- f2+geom_step(subset=.(variable=='Active.Members'))
17f4 <- f3+geom_linerange(subset=.(variable=='Member.Joins'))
18f5 <- f4+geom_linerange(subset=.(variable=='RSVPs'))
19f5+geom_vline(xintercept=meetup$Dates, color='red',alpha=.3)+ylab('')


http://statbandit.wordpress.com/2011/07/29/a-ggplot-trick-to-plot-different-plot-types-in-facets/

没有评论:

发表评论