[R] Problem with plot() and POSIXt dates

Gabor Grothendieck ggrothendieck at gmail.com
Fri Jan 5 15:13:28 CET 2007


We can force the X axis into the format of our choice by
suppressing it in the plot and explicitly calling
axis.POSIXct with our choice of format
(see ?axis.POSIXct):

x <- seq(from = as.POSIXct("2005-10-14"),
       to = as.POSIXct("2007-01-02"),
       by = "day"
y <- as.numeric(x)

plot(y ~ x, xaxt = "n")
at <- x[seq(1, length(x), length = 5)]
axis.POSIXct(1, at, at, "%y-%b-%d")

Actually your data appear to be dates rather than date times
so I would use Date class rather than POSIXct.  See the Help
Desk article on dates and times in R News 4/1.

[Try making your examples smaller and more self contained
such as shown above.]

On 1/5/07, COMTE Guillaume <g.comte at alliance-ir.net> wrote:
> So maybe i will finaly succeed asking the right way,
>
>
>
> Hy all,
>
> I'm plotting graphs using plot() function, they are on X axes POSIX dates:
>
> "POSIXt"   "oldClass" "POSIXct"  "POSIXlt"
>
> I can't figure out why sometimes it prints the month and days and sometimes it prints the unix timestamps.
>
> Here is an example that reproduce my problem the data's to use with it can be downloaded from http://www.alliance-ir.net/r-project/demo.tar.gz <http://www.alliance-ir.net/r-project/demo.tar.gz>  :
>
> limite_x<-structure(as.numeric(read.table("limite_x")), class=c("POSIXt","POSIXct"))
>
> limite_y<-as.numeric(read.table("limite_y"))
>
> test<-as.numeric(read.table("y_values"))
>
> abscisse_test<-structure(as.numeric(read.table("x_values")), class=c("POSIXt","POSIXct"))
>
> plot((test/10)~abscisse_test,type="s",col="lightgreen",xlab="",ylab="",ylim=limite_y,xlim=limite_x)
>
> length_test<-length(test)
>
> #if i cut the var "test" to be closer to xlim values, i know that xlim is at the end of "test" :
>
> test_short<-test[(length_test-10):length_test]
>
> abscisse_test_short<-abscisse_test[(length_test-10):length_test]
>
> plot((test_short/10)~abscisse_test_short,type="s",col="lightgreen",xlab="",ylab="",ylim=limite_y,xlim=limite_x)
>
> dev.off()
>
> > sessionInfo()
>
> R version 2.4.0 (2006-10-03)
>
> i386-pc-linux-gnu
>
>
>
> locale:
>
> LC_CTYPE=fr_FR at euro;LC_NUMERIC=C;LC_TIME=fr_FR at euro;LC_COLLATE=fr_FR at euro;LC_MONETARY=fr_FR at euro;LC_MESSAGES=fr_FR at euro;LC_PAPER=fr_FR at euro;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=fr_FR at euro;LC_IDENTIFICATION=C
>
>
>
> attached base packages:
>
> [1] "methods"   "stats"     "graphics"  "grDevices" "utils"     "datasets"
>
> [7] "base"
>
>
>
> COMTE Guillaume
>
> Ingénieur Projet
>
> Alliance Technologies
>
> Projet Philharmonie
>
> 24 rue Martre
>
> 92110 Clichy
>
> Tel : 01 40 87 48 06
>
> Fax : 01 40 87 48 14
>
>
>
>
>        [[alternative HTML version deleted]]
>
>
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>
>
>



More information about the R-help mailing list