[R] Odp: FW: RES: bug in axis.Date? was (Re: newbie needs help plottingtimeseries)

Kathi kathi at dropnet.ch
Wed Jul 2 16:27:11 CEST 2008


Thanks, Petr, for all your help!

I haven't called chron package explicitely, so I don't think it's from chron package. Although I have to 
admit that I don't know. 
I think I'm starting to understand the problem: When I start my plot using
 
plot(x, y, axes=FALSE, ylim=c(405,410),col="red",...)

then R starts drawing the plot and spreads out the x-axis according to the dates in my data file. But 
then I come along and tell R to draw an x-axis that's longer and basically won't fit onto the plotting 
region, and R doesn't like that, to use layman's terms. I haven't tried yet, but telling R right from the 
beginning how big the plot should be (using xlim, as you suggested) should probably do the trick. 
And it would also mean that half a year from now, when I want to do another "shift" by 6 months, I 
can simply do a "search-and-replace" in my code file , and everything should work. (Hopefully!)

Thanks again!

Kathi


On Wed, 2 Jul 2008 15:15:27 +0200, Petr PIKAL wrote
> Hi
> 
> Is it from chron package? As I do not have it available now I can 
> not try it but the problem is not in plotting itself but in custom 
>  x axis labeling when there are missing date on either end of  a series.
> 
> You have several options
> 
> Define fake time values for yet nonexisting data.
> Do your labeling only for existing values.
> Use xlim when calling your plot (which seems to me the most suitable)
> 
> compare
> 
> x<-1:10
> y<-1:10
> x[8:10]<-NA
> y[8:10]<-NA
> plot(x, y, axes=F)
> box()
> axis(1, at=1:10, labels=11:20)
> 
> with this
> 
> x<-seq(as.Date("2004-01-01"), as.Date("2008-06-01"),by="month")
> x[50:54] <-NA
> y <- seq(405,410,length=54)
> y[50:54] <-NA
> plot(x, y, axes=FALSE, ylim=c(405,410),col="red", pch=15,
> type="o", ylab="metres above sea level", main="Main Plot Title")
> axis(2, at=405:410, tck=1, col="gray60")
> axis.Date(1, at=seq(as.Date("2004-01-01"), as.Date("2008-06-01"),
> by="month"), labels=seq(as.Date("2004-01-01"), as.Date("2008-06-01"),
> by="month"), tck=1, col="gray60")
> 
> which shall basically do the same. When you set explicit limits in 
> plotting you can get rid of subsequent error in labeling axis.
> 
> plot(x, y, axes=FALSE, ylim=c(405,410),col="red", pch=15,
> type="o", ylab="metres above sea level", main="Main Plot Title", 
> xlim=c(as.Date("2004-01-01"), as.Date("2008-06-01")))
> 
> OK, It is not bug, but you has to go quite deep into axis.Date 
> function to undestand what is going on and how to avoid such behaviour.
> 
> Regards
> Petr
> 
> r-help-bounces at r-project.org napsal dne 02.07.2008 14:29:24:
> 
> > 
> > Instead of 
> > x<-seq(as.Date("2004-01-01"), as.Date("2008-06-01"),by="month")
> > 
> > from=iso2chron("2004-01-01")
> > to= iso2chron("2008-06-01")
> > 
> > from <-chron2UTCsecs(from)
> >   to<-chron2UTCsecs(to) 
> > 
> > delta##how many secs i.e month time in secs
> > 
> >  xx <- seq(from=from, to=to, by=delta) 
> >     x <- UTCsecs2chron(xx)
> > 
> > I think it will works
> > 
> > 
> > 
> > 
> > -----Original Message-----
> > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
> > On Behalf Of Leandro Marino
> > Sent: Wednesday, July 02, 2008 5:47 PM
> > To: Petr PIKAL; Kathi
> > Cc: r-help at stat.math.ethz.ch
> > Subject: [R] RES: bug in axis.Date? was (Re: newbie needs help
> > plottingtimeseries)
> > 
> > You can do this plot saying to R that your file is a time series with
> > the
> > ts() function.
> > 
> > 
> > serie=ts(x,....)
> > and then you use plot(serie)
> > 
> > -----Mensagem original-----
> > De: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]Em
> > nome de Petr PIKAL
> > Enviada em: quarta-feira, 2 de julho de 2008 05:04
> > Para: Kathi
> > Cc: r-help at stat.math.ethz.ch
> > Assunto: [R] bug in axis.Date? was (Re: newbie needs help plotting
> > timeseries)
> > 
> > 
> > Hi
> > 
> > "Kathi" <kathi at dropnet.ch> napsal dne 01.07.2008 18:48:47:
> > 
> > > On Tue, 1 Jul 2008 14:33:12 +0200, Petr PIKAL wrote
> > > > "Kathi" <kathi at dropnet.ch> napsal dne 01.07.2008 13:39:18:
> > > >
> > > > > Thanks, Petr.
> > > > >
> > > > > After fiddling around some more with my code I found out that this
> > seems
> > > > to
> > > > > have something to do
> > > > > with the number of records the table I'm reading (here: column
> > B112).
> > > > Somehow,
> > > > > changing the
> > > > > number of records for B112 changes the numbers in the error
> > message
> > -
> > > > though I
> > > > > don't quite
> > > > > understand how the number of records influences the axis intervals
> > or
> > > > their
> > > > > labels, since I define
> > > > > those explicitly...
> > > >
> > > > Neither I. So I sent it to r-help maybe somebody else will know. If
> > > > you debug code of axis.Date you find that after some computing there
> > > > is this comparison where range is based on your input data (in your
> > > > case x), and z is from defined at sequence of dates.
> > > >
> > > > z <- z[z >= range[1] & z <= range[2]]
> > > >
> > > > and this comparison results in character(0), when plotting with this
> > > > artificial example.
> > > >
> > > > plot(1:100, seq(405,410,length=100), axes=FALSE, ylim=c(405,410),
> > > > col="red", pch=15,
> > > > type="o", ylab="metres above sea level", main="Main Plot Title")
> > > > axis(2, at=405:410, tck=1, col="gray60")
> > > > axis.Date(1, at=seq(as.Date("2004-01-01"), as.Date("2008-06-01"),
> > > > by="month"), labels=seq(as.Date("2004-01-01"),
> > as.Date("2008-06-01"),
> > > > by="month"), tck=1, col="gray60")
> > > >
> > > > I presume, that your x is from different interval of dates or it is
> > > > not a date.
> > >
> > > My data comes from a groundwater monitoring program. Once a month the
> > water surface is
> > > measured in some 170 stations, and I've been using R to visualize
> > these
> > > measurements. In this
> > > particular case, the water table ranges between approximately 406 and
> > 408
> > > metres above sea level,
> > > hence the y axis interval between 405 and 410.
> > > The values of my column x are dates of the format YYYY-MM-DD. If
> > something
> > > were wrong there,
> > > then I guess I would not be able to plot my data at all. But using the
> > limits
> > > of 2004-01-01 and
> > > 2008-06-01, I can plot all the timelines for my 170 measuring stations
> > without
> > > any problem. But
> > > since it's now July, I wanted to shift the view by half a year. So I
> > used
> > > "search and replace" in my code
> > > to change to 2004-07-01 and 2008-12-01 (plus six months in both
> > cases).
> > That's
> > > when I got the
> > > error. I almost have a feeling that R doesn't like the "empty" part of
> > the
> > > graph for July-December...  I
> > > don't understand why this happens, but maybe it has something to do
> > with
> > the
> > > comparison of ranges
> > > you mentioned.
> > 
> > OK, It seems that you are right. Maybe you could add some expected x
> > date/time values so that x axis will have no NA values.
> > 
> > x<-seq(as.Date("2004-01-01"), as.Date("2008-06-01"),by="month")
> > y <- seq(405,410,length=54)
> > y[50:54] <-NA
> > plot(x, y, axes=FALSE, ylim=c(405,410),col="red", pch=15,
> > type="o", ylab="metres above sea level", main="Main Plot Title")
> > axis(2, at=405:410, tck=1, col="gray60")
> > axis.Date(1, at=seq(as.Date("2004-01-01"), as.Date("2008-06-01"),
> > by="month"), labels=seq(as.Date("2004-01-01"), as.Date("2008-06-01"),
> > by="month"), tck=1, col="gray60")
> > 
> > as you see, then everything works. However I think that it is pretty
> > close
> > to bug in axis.Date, maybe somebody can enlighten us both :-)
> > 
> > Regards
> > Petr
> > 
> > 
> > >
> > > Thanks for all your help!
> > >
> > > Kathi
> > >
> > > >
> > > > Regards
> > > > Petr
> > > >
> > > > >
> > > > > Kathi
> > > > >
> > > > >
> > > > > On Mon, 30 Jun 2008 11:48:43 +0200, Petr PIKAL wrote
> > > > > > Hi
> > > > > >
> > > > > > r-help-bounces at r-project.org napsal dne 30.06.2008 11:14:48:
> > > > > >
> > > > > > > Dear R help list
> > > > > > >
> > > > > > > I have recently started using R to visualize time series of
> > water
> > > > level
> > > > > > > measurements. The code I wrote
> > > > > > > does exactly what I want:
> > > > > > >
> > > > > > > plot(x, a$B112, axes=FALSE, ylim=c(405,410), col="red",
> > pch=15,
> > > > > > > type="o", ylab="metres above sea level", main="Main Plot
> > Title")
> > > > > > > axis(2, at=405:410, tck=1, col="gray60")
> > > > > > > axis.Date(1, at=seq(as.Date("2004-01-01"),
> > as.Date("2008-06-01"),
> > > > > > > by="month"), labels=seq(as.Date("2004-01-01"),
> > > > as.Date("2008-06-01"),
> > > > > > > by="month"), tck=1, col="gray60")
> > > > > > >
> > > > > > > Now I wanted to shift the data visualized by 6 months, so I
> > replaced
> > > >
> > > > > > > "2004-01-01" by "2004-07-01"
> > > > > > > and "2008-06-01" by "2008-12-01". Instead of the desired plot,
> > I
> > get
> > > > an
> > > > > > error
> > > > > > > message stating
> > > > > > > (forgive the perhaps faulty translation into English, I'm
> > working on
> > > > a
> > > > > > German
> > > > > > > version of R):
> > > > > > >
> > > > > > > mistake in axis(side, at = z, labels = labels, ...) :
> > > > > > >   length of 'at' and 'labels' different, 50 != 54
> > > > > >
> > > > > > Those two calls have different length
> > > > > >
> > > > > > at=seq(as.Date("2004-01-01"), as.Date("2008-06-01"), by="month")
> > > > > > labels=seq(as.Date("2004-01-01"), as.Date("2008-06-01"),
> > by="month")
> > > > > >
> > > > > > you can try it by
> > > > > >
> > > > > > length(seq(...))
> > > > > >
> > > > > > could not there be some typo in your call?
> > > > > >
> > > > > > Regards
> > > > > >
> > > > > > Petr
> > > > > >
> > > > > > >
> > > > > > > What have I done wrong? Thank you very much for your help!
> > > > > > >
> > > > > > > Katharina Dubach
> > > > > > >
> > > > > > > ______________________________________________
> > > > > > > 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.
> > > > >
> > > > >
> > > > > --
> > > > > DropNet AG - Das Unternehmen fuer Ihren Internet-Auftritt!
> > > > >
> > > > >
> > >
> > >
> > > --
> > > DropNet AG - Das Unternehmen fuer Ihren Internet-Auftritt!
> > >
> > >
> > 
> > ______________________________________________
> > 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.
> > 
> > ______________________________________________
> > 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.
> > 
> > ______________________________________________
> > 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.


--
DropNet AG - Das Unternehmen fuer Ihren Internet-Auftritt!



More information about the R-help mailing list