scale_x_datetime() returning the wrong label years

124 views
Skip to first unread message

Adriano Fantini

unread,
Mar 24, 2017, 9:51:07 AM3/24/17
to ggplot2
Hi all,
I have problems with scale_x_datetime() plotting wrong labels (years). See the example below, which includes a small subset of the data.
The data starts from 1995-01-01 00:00:00, which should be indicated "1995". Instead ggplot indicates it as "1994".

​d <-
structure(list(Time = structure(c(788914800, 788918400, 788922000, 
+ 788925600, 788929200, 788932800), .Dim = 6L, class = c("POSIXct", 
+ "POSIXt")), value = structure(c(499, 513, 499, 517, 522, 517), .Dim = 6L)), .Names = c("Time", 
+ "value"), row.names = c(NA, 6L), class = "data.frame")

​#works​, but I want years only in the X axis...
ggplot(d) + geom_point(aes(x=Time, y=value)) + scale_x_datetime()
​#so I can try this: (which does not work, as it says "1994")
​ggplot(d) + geom_point(aes(x=Time, y=value)) + scale_x_datetime(date_breaks = "1 year", labels = date_format("%Y"))
#Or this: (same as above)
​ggplot(d) + geom_point(aes(x=Time, y=value)) + scale_x_datetime(date_breaks = "1 year", date_labels = "%Y") 
#Or this: (which works, but I do not want the time to be displayed in "1995-01-01" format)
​ggplot(d) + geom_point(aes(x=Time, y=value)) + scale_x_datetime(date_breaks = "1 year")

In short, it seems that sometimes ggplot transforms 1995-01-01 00:00:00 in 1994. Is this a bug? If not, how can I fix this and why does this happen?

Thanks in advance,
Adriano


Crump, Ron

unread,
Mar 24, 2017, 10:07:47 AM3/24/17
to Adriano Fantini, ggplot2
Hi Adriano,

>The data starts from 1995-01-01 00:00:00, which should be indicated
>"1995".

I cut and pasted this:
>​d <-
>structure(list(Time = structure(c(788914800, 788918400, 788922000,
>788925600, 788929200, 788932800), .Dim = 6L, class = c("POSIXct",
>"POSIXt")), value = structure(c(499, 513, 499, 517, 522, 517), .Dim =
>6L)), .Names = c("Time",
>"value"), row.names = c(NA, 6L), class = "data.frame")

print(d)
Time value
1 1994-12-31 23:00:00 499
2 1995-01-01 00:00:00 513
3 1995-01-01 01:00:00 499
4 1995-01-01 02:00:00 517
5 1995-01-01 03:00:00 522
6 1995-01-01 04:00:00 517


So in the example you sent the first data point is in 1994 and the others
are all 1 January 1995. Or is that a timezone issue (I'm in the UK). If it
is a timezone issue maybe that is a clue in itself, could be somewhere the
code is treating it as GMT.

Ron.

Adriano Fantini

unread,
Mar 24, 2017, 10:10:47 AM3/24/17
to Crump, Ron, ggplot2
First, thanks.​
Second, uuuummmmmh....

​print(d)
                 Time value
1 1995-01-01 00:00:00   499
2 1995-01-01 01:00:00   513
3 1995-01-01 02:00:00   499
4 1995-01-01 03:00:00   517
5 1995-01-01 04:00:00   522
6 1995-01-01 05:00:00   517

​Looks totally like a timezone issue. Unfortunately the `tz` argument was removed from scale_x_datetime(), so I have to figure out another way to fix it.​
 

Crump, Ron

unread,
Mar 24, 2017, 10:15:05 AM3/24/17
to Adriano Fantini, Crump, Ron, ggplot2


>First, thanks.​
>Second, uuuummmmmh....
>
>​print(d)
> Time value
>1 1995-01-01 00:00:00 499
>2 1995-01-01 01:00:00 513
>3 1995-01-01 02:00:00 499
>4 1995-01-01 03:00:00 517
>5 1995-01-01 04:00:00 522
>6 1995-01-01 05:00:00 517
>
>​Looks totally like a timezone issue. Unfortunately the `tz` argument was
>removed from scale_x_datetime(), so I have to figure out another way to
>fix it.​

There is a tz argument to scales::date_format though.

Try having a play with that. It is defaulting to UTC.

Ron.

Adriano Fantini

unread,
Mar 24, 2017, 10:21:15 AM3/24/17
to Crump, Ron, ggplot2
There it is.

I was defining times like this:
times <- gsub("Seconds since ", "", nc$dim[["time"]]$units)
as.POSIXct(times, format="%Y-%m-%d %H:%M:%S")

While I should have been defining them like this, knowing that the input is UTC and the current timezone in my system is CET:
as.POSIXct(times, format="%Y-%m-%d %H:%M:%S", tz="UTC")

​I wrongly assumed that R was defaulting to UTC, not to the current timezone.​
 

​Thanks!​
Adriano
Reply all
Reply to author
Forward
0 new messages