xlim and POSIXct

900 views
Skip to first unread message

Pascal B

unread,
Feb 12, 2010, 12:02:37 PM2/12/10
to ggplot2
dear Hadley and ggplo followers,

I have the following error message when trying to use xlim with
posixct objects :
Erreur dans limits.POSIXct(c(...), "x") : objet 'ScaleDateTime'
introuvable

here is a sample code that can reproduce the error :


mkDateTime <- function(x, format = "%Y-%m-%d %H:%M:%S") {
as.POSIXct(strptime(x, format=format))
}

y <- runif(100)
x <- seq.POSIXt( from = mkDateTime("14/11/09", "%d/%m/%y"),
length.out = 100, by = "1 days" )
df <- data.frame(x,y)
p <- ggplot(data=df, aes(x=x, y=y)) + geom_point()
p
p+xlim(c(x[50], x[75]))

Mark Connolly

unread,
Feb 12, 2010, 1:17:25 PM2/12/10
to Pascal B, ggplot2
This might help you get out of a jam until someone else can chime in on
POSIXct/POSIXlt (it may be that ggplot2 does not support datetime
classes). Switch to as.Date().

require(ggplot2)
x <- seq(as.Date("2009/11/14"), length.out = 100, by = "1 days" )
y <- runif(100)


df <- data.frame(x,y)
p <- ggplot(data=df, aes(x=x, y=y)) + geom_point()
p
p+xlim(c(x[50], x[75]))


If you need a piece of the date (year, for example):

as.POSIXlt(x[50])$year + 1900

Brian Diggs

unread,
Feb 12, 2010, 1:53:27 PM2/12/10
to ggplot2

Another temporary work-around, if you don't want to switch to
as.Date(), is

p + coord_cartesian(xlim=c(x[50],x[75]))

It is not quite the same because it is just zooming the plot, not
dropping the data outside that range, but for points, there is no
difference.

The real problem is a bug in ggplot:

in scale-convenience.r, lines 66 and 70 have "ScaleDateTime" and it
should be (I think) "ScaleDatetime" (lower case t in time)

--Brian Diggs

Mark Connolly

unread,
Feb 12, 2010, 2:59:36 PM2/12/10
to Pascal B, ggplot2
On 02/12/2010 12:02 PM, Pascal B wrote:
I am not sure how I missed it the first time -- would have sworn I tried
it, but no matter.

mkDateTime<- function(x, format = "%Y-%m-%d %H:%M:%S") {
as.POSIXct(strptime(x, format=format))
}

y<- runif(100)
x<- seq.POSIXt( from = mkDateTime("14/11/09", "%d/%m/%y"),
length.out = 100, by = "1 days" )
df<- data.frame(x,y)
p<- ggplot(data=df, aes(x=x, y=y)) + geom_point()
p

ggplot(data=df, aes(x=x, y=y)) + geom_point() + scale_x_datetime(limits=c(x[50],x[75]))


hadley wickham

unread,
Feb 15, 2010, 9:33:35 AM2/15/10
to Brian Diggs, ggplot2
> The real problem is a bug in ggplot:
>
> in scale-convenience.r, lines 66 and 70 have "ScaleDateTime" and it
> should be (I think) "ScaleDatetime" (lower case t in time)

Ooops, that was a dumb mistake. Will be fixed in the next version.

Hadley

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

Reply all
Reply to author
Forward
0 new messages