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]))
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
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
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]))
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/