[R] time plotting problem
jim holtman
jholtman at gmail.com
Tue Nov 13 22:31:12 CET 2007
I have had some problems with dates on axis in certain ranges. Here
is a version of axis.POSIXct that I have been using that seems to do a
better job:
my.axis.POSIXct <-
function (side, x, format, inc)
{
.diff <- diff(range(unclass(x), na.rm = TRUE))
if (.diff < 30) {
.by <- "sec"
.for <- "%M:%S"
}
else if (.diff < 5 * 60) {
.by <- "15 secs"
.for <- "%M:%S"
}
else if (.diff < 30 * 60) {
.by <- "2 min"
.for <- "%H:%M"
}
else if (.diff < 2 * 3600) {
.by <- "15 mins"
.for <- "%H:%M"
}
else if (.diff < 12 * 3600) {
.by <- "hour"
.for <- "%H:00"
}
else if (.diff < 48 * 3600) {
.by <- "6 hours"
.for <- "%d-%H"
}
else if (.diff < 7 * 24 * 3600) {
.by <- "12 hours"
.for <- "%d-%H"
}
else if (.diff < 14 * 24 * 3600) {
.by <- "day"
.for <- "%m/%d"
}
else {
.inc <- diff(pretty(x))[1]
if (.inc < 7 * 84600) {
.by <- paste(as.integer(.inc/86400), "days")
}
else {
.by <- paste(as.integer(.inc/(86400 * 7)) * 7, "days")
}
.for <- "%m/%d/%y"
}
.range <- range(x, na.rm = TRUE)
axis.POSIXct(side, at = seq(trunc(.range[1], "day"), trunc(.range[2] +
86400, "day"), by = ifelse(missing(inc), .by, inc)),
format = ifelse(missing(format), .for, format))
}
With your code I did the following which may give you better results:
plot(as.POSIXct(mydata[,1]),mydata[,2], xlab="Dates", ylab="Blood
pressure",
ylim=c(minmax[4], minmax[1]), col= "red",
type="l", xaxt='n')
my.axis.POSIXct(1, as.POSIXct(mydata[,1]))
On Nov 13, 2007 9:08 AM, John Kane <jrkrideau at yahoo.ca> wrote:
> I clearly spoke too soon.
>
> With the actual data I am not getting sensible x-axis
> units. The program with the actual data below. Graph
> output is here:
> http://ca.geocities.com/jrkrideau/R/hd.png .
>
> I seem to be getting only a single entry for the
> x-axis of "2007". However dates range from
> First Date Last Date
> "2006-09-26" "2007-11-10"
>
> I must be missing something blindingly obvious but I
> don't see it.
>
> Thanks for any suggestions.
>
>
> Actual data and test program
> ================================================
> mydata <-
> read.table("http://ca.geocities.com/jrkrideau/R/heartdata.txt",
> sep="\t",
> header=FALSE)
> mydata[,1] <- as.Date(mydata[,1],"%m/%d/%y")
> names(mydata) <- Cs(dates, sy,dys,pulse, weight)
> minmax <- c(max(mydata[,2]),min(mydata[,2]),
> max(mydata[,3]),min(mydata[,3]))
> names(minmax) <- c("maxsys", "minsys", "maxdsy",
> "mindys") ; minmax
>
> min.max.dates <- c(min(mydata[,1]), max(mydata[,1]))
> names(min.max.dates) <- c("First Date", "Last Date");
> min.max.dates
>
> ss <- lm(mydata[,2]~mydata[,1])
> plot(mydata[,1],mydata[,2], xlab="Dates", ylab="Blood
> pressure",
> ylim=c(minmax[4], minmax[1]), col= "red",
> type="l")
> abline (ss, col ="yellow")
> dd <- lm(mydata[,3]~mydata[,1])
> points(mydata[,1], mydata[,3], type="l", col="blue" )
> abline(dd, col= "yellow", lwd=2)
>
>
>
>
>
>
> > Thanks to Gabor and Jim. I am not sure if the first
> > entry year = 2009 is all the problem I'm getting but
> > it is certainly seems like the worst of it.
> >
> > My stupidity: Someone sent me the data set in Excel
> >
> > and I didn't do the basic data checks on. I _KNEW_
> > the
> > data went from 2006 to 2007.
> >
> > --- Gabor Grothendieck <ggrothendieck at gmail.com>
> > wrote:
> >
> > > In your examples the first line of your data
> > refers
> > > to the
> > > year 2009 and Oct 1st is repeated. Is that really
> > > what
> > > you meant?
> > >
> > > I can't tell what your problem is from your
> > > description
> > > other than the data problems cited but there are
> > > lots of
> > > examples of plotting with zoo in the following
> > which
> > > may
> > > help you:
> > > vignette("zoo")
> > > vignette("zoo-quickref")
> > > ?plot.zoo
> > > ?xyplot.zoo
> > >
> > > Note that zoo series must be time series, i.e.
> > they
> > > must
> > > have unique times.
> > >
> > > On Nov 12, 2007 1:47 PM, John Kane
>
> > > > I am completely misunderstanding how to handle
> > > dates.
> > > > I want to plot a couple of data series against
> > > some
> > > > dates. Simple example 1 below works fine.
> > > > Unfortunately I have multiple observations per
> > day
> > > (no
> > > > time breakdowns) and observations across years.
> > > > (example 2 very simplistic version )
> > > >
> > > > Can anyone suggest a quick fix or point me to
> > > > something to read? I thought that zoo might do
> > it
> > > but
> > > > I seem to be missing something there too.
> > > >
> > > > Any suggestions gratefully recieved.
> > > >
> > > >
> > > > Example 1 consecutive dates same year.
> > > >
> > =================================================
> > > > x <- "days
> > > > 9/26/09
> > > > 9/27/06
> > > > 9/28/06
> > > > 9/29/06
> > > > 9/29/06
> > > > 9/29/06
> > > > 10/1/06
> > > > 10/1/06
> > > > 10/2/06
> > > > 10/3/06"
> > > >
> > > > mydata <- read.table(textConnection(x),
> > > header=TRUE,
> > > > as.is=TRUE); mydata
> > > >
> > > > mydates <- as.Date(mydata[,1], "%m/%d/%y");
> > > mydates
> > > > mynums <- rnorm(10)
> > > > plot(mydates, mynums)
> > > > ================================================
> > > > Example 2 (things go blooy!)
> > > > non-consecutive dates different years.
> > > >
> > > >
> > =================================================
> > > > x <- "days
> > > > 9/26/09
> > > > 9/27/06
> > > > 9/28/06
> > > > 9/29/06
> > > > 9/29/06
> > > > 9/29/06
> > > > 10/1/07 # <- year changes
> > > > 10/1/07
> > > > 10/2/07
> > > > 10/3/07"
> > > >
> > > > mydata <- read.table(textConnection(x),
> > > header=TRUE,
> > > > as.is=TRUE); mydata
> > > >
> > > > mydates <- as.Date(mydata[,1], "%m/%d/%y");
> > > mydates
> > > > mynums <- rnorm(10)
> > > > plot(mydates, mynums)
> > > >
> > > > ______________________________________________
> > > > 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.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem you are trying to solve?
More information about the R-help
mailing list