Hi, I am plotting time series by ggplot2, but I believe that my question applies to other plotting tool as well. I want to make my x-axis the quarterly scale, e.g: 2000Q1 2000Q2..... However, scale_x_date and date_format("%m/%d") support all time formats BUT QUARTERs........ library(scales) # to access breaks/formatting functions dt + scale_x_date() dt + scale_x_date(labels = date_format("%m/%d")) Is there any solution? Thanks! [[alternative HTML version deleted]]
Franklin Mairura
2014-Oct-14 08:48 UTC
[R] ggplot "scale_x_date" : to plot quarterly scale?
Try this code, this may get a solution close to what you need. The advantage with this code is that you specify the text you want to appear on the xaxis. The dates have to be supplied as text formats, and located on the xaxis using the axis and at commands. the at command allows for customised location whether intervals are equal or not. The matplot command draws the lineplot, which is basically what a time series is. Using excel makes customised syntax building for R easier, by the CONCATENATE Excel worksheet functuion. Canopy<-(factor(c(1,2,3))) a<-c(0.336,0.852,0.794) b<-c(0.21,0.353333333333333,0.878333333333333) c<-c(0.443333333333333,0.65,1.18833333333333) d<-c(0.052,0.106,0.142) e<-c(0.275,0.203333333333333,0.633333333333333) f<-c(0.35,0.365,0.796666666666667) g<-c(0.16,0.466666666666667,0.696666666666667) h<-c(0.586666666666667,0.873333333333333,2.29) i<-c(0.836666666666667,2.55625,2.675) j<-c(0.536666666666667,1.68,2.52333333333333) k<-c(0.163333333333333,0.136666666666667,0.156666666666667) l<-c(0.186666666666667,0.228333333333333,0.446666666666667) m<-c(0.175,0.265,0.755) n<-c(0.536,1.834,2.378) o<-c(0.248333333333333,1.05666666666667,1.60666666666667) p<-c(1.07,0.9275,2.05) q<-c(0.1225,0.5975,0.6075) r<-c(0.155,0.325,1.635) s<-c(0.418333333333333,1.82,3.68333333333333) t<-c(1.25,1.2925,2.3925) u<-c(1.416,2.304,2.258) v<-c(0.446666666666667,1.235,2.85) RUST<-data.frame(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v) matplot(ylim=c(0,5),Canopy,RUST,type="o",pch=c(1:23),lty=c(1:23),col=c(1:23),xaxt="n",lwd=2,cex=1.5,ylab="Rust Score",xlab="Canopy level") axis(1, at=1:3,lab=c("Top","Middle","Lower"),cex.axis=1,font=4) #mtext("Canopy level",side=1,line=3,) legend("topleft",col=c(1:23),cex=0.6,ncol=2,pch=c(1:23),lwd=2,lty=c(1:23),c("a","b","c","d","e","f","g", "h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w")) segments(1,1.71535,1,2.28465) segments(2,2.868,2,3.532) segments(3,4.169,3,5.031) segments(0.99,1.71535,1.01,1.71535) segments(0.99,2.28465,1.01,2.28465) segments(1.99,2.868,2.01,2.868) segments(1.99,3.532,2.01,3.532) segments(2.99,5.031,3.01,5.031) segments(2.99,4.169,3.01,4.169) On Tuesday, October 14, 2014 10:37 AM, jpm miao <miaojpm at gmail.com> wrote: Hi, I am plotting time series by ggplot2, but I believe that my question applies to other plotting tool as well. I want to make my x-axis the quarterly scale, e.g: 2000Q1 2000Q2..... However, scale_x_date and date_format("%m/%d") support all time formats BUT QUARTERs........ library(scales) # to access breaks/formatting functions dt + scale_x_date() dt + scale_x_date(labels = date_format("%m/%d")) Is there any solution? Thanks! [[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. [[alternative HTML version deleted]]
Gabor Grothendieck
2014-Oct-14 12:14 UTC
[R] ggplot "scale_x_date" : to plot quarterly scale?
On Tue, Oct 14, 2014 at 3:36 AM, jpm miao <miaojpm at gmail.com> wrote:> Hi, > > I am plotting time series by ggplot2, but I believe that my question > applies to other plotting tool as well. > > I want to make my x-axis the quarterly scale, e.g: > 2000Q1 2000Q2..... > > However, scale_x_date and date_format("%m/%d") support all time formats > BUT QUARTERs........ > > library(scales) # to access breaks/formatting functions > dt + scale_x_date() > dt + scale_x_date(labels = date_format("%m/%d")) >1. zoo has a "yearqtr" class and its ggplot2 interface includes scale_x_yearqtr() so: library(zoo) library(ggplot2) library(scales) # test data DF <- data.frame(date = seq(as.Date("2014-01-01"), length = 4, by = "3 months"), y = c(1, 4, 2, 3)) # convert date to yearmon class DF2 <- transform(DF, date = as.yearqtr(date)) ggplot(DF2, aes(date, y)) + geom_line() + scale_x_yearqtr(format = "%YQ%q") 2. zoo also has a zoo method for ggplot2's autoplot generic so we could just convert DF to zoo and write: z <- zoo(DF$y, as.yearqtr(DF$date)) autoplot(z) + scale_x_yearqtr(format = "%YQ%q") In both cases if the format argument is omitted one gets a default of format = "%Y-%q". -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com