[R] Line Plot with Dates on X-axis

Henrique Dallazuanna wwwhsd at gmail.com
Wed Jan 20 19:20:16 CET 2010


Try the zoo package:

plot(as.yearqtr(time.val), inc, col = 'red', type = 'l')

On Wed, Jan 20, 2010 at 3:41 PM, mah <harwood262 at gmail.com> wrote:
> I am trying to generate a line graph with quarterly time buckets (with
> nice labels) on the x-axis.  The first block of code below will
> generate the graph with nicely formatted x-axis labels, but the
> "type=" and "col=" options are not recognized when factors are used
> for the x-axis.
>
> The second block, where the quarter values are mapped into dates, will
> plot the line nicely but the x-axis is the number of days since
> 1/1/1970.  How do I either get the first block to recognize the
> desired format, or the second block to format the x-axis as quarters?
>
> Thank you in advance!
>
> #block 1
> time.val <- as.factor(c
> ("2004Q1","2004Q2","2004Q3","2004Q4","2005Q1","2005Q2"))
> inc <- c(0.9903797, 1.3741467, 0.9938702, 0.4252443, 0.7700158,
> 1.1770313)
>
> # returns nice x-axis, but black points
> plot(time.val, inc, type="b", col="red")
>
> #block 2
> time2 <-
>  ifelse (substr(time.val,5,6) == "Q1",
>    as.Date(paste("1/1/",substr(time.val,1,4),sep=""),"%m/%d/%Y"),
>  ifelse (substr(time.val,5,6) == "Q2",
>    as.Date(paste("4/1/",substr(time.val,1,4),sep=""),"%m/%d/%Y"),
>  ifelse (substr(time.val,5,6) == "Q3",
>    as.Date(paste("7/1/",substr(time.val,1,4),sep=""),"%m/%d/%Y"),
>    as.Date(paste("10/1/",substr(time.val,1,4),sep=""),"%m/%d/%Y"))))
>
> #returns red lines but the axis is the number of days since 1/1/1970
> plot(time2, inc, type="b", col="red")
>
> ______________________________________________
> 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.
>



-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O



More information about the R-help mailing list