Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

timezone parameter for scale_x_datetime has no effect #4007

Closed
swebs opened this issue May 18, 2020 · 6 comments · Fixed by #4429
Closed

timezone parameter for scale_x_datetime has no effect #4007

swebs opened this issue May 18, 2020 · 6 comments · Fixed by #4429
Labels
bug an unexpected problem or unintended behavior scales 🐍
Milestone

Comments

@swebs
Copy link

swebs commented May 18, 2020

I am trying to use the timezone parameter to scale_x_datetime to control how the tick labels are displayed, but I can't get it to work. Changing the timezone does not seem to make a difference to plot output. This probably applies equally to scale_y_datetime. Potentially I am not using the correct syntax and the documentation could be expanded.

library(tidyverse)
library(lubridate)

plotdata <- tribble(
  ~t_stamp, ~value,
  "2019-08-17T06:00:00", 1,
  "2019-08-17T07:00:00", 2,
  "2019-08-17T08:00:00", 3
)

plotdata <- plotdata %>% 
  mutate(t_stamp=force_tz(ymd_hms(t_stamp),tzone="US/Mountain"))

ggplot(plotdata) +
  geom_point(mapping=aes(x=t_stamp, y=value)) +
  scale_x_datetime(timezone = "US/Mountain")

ggplot(plotdata) +
  geom_point(mapping=aes(x=t_stamp, y=value)) +
  scale_x_datetime(timezone = "US/Eastern")

# both plots are the same
@smouksassi
Copy link

seems timezone not applied when the date_labels are a waiver ?
below work

ggplot(plotdata) +
  geom_point(mapping=aes(x=t_stamp, y=value)) +
  scale_x_datetime(date_labels= "%H:%M",
                   timezone = "US/Eastern")

ggplot(plotdata) +
  geom_point(mapping=aes(x=t_stamp, y=value)) +
  scale_x_continuous(trans = time_trans(tz = "US/Eastern"))

@yutannihilation
Copy link
Member

yutannihilation commented May 19, 2020

Thanks, it actually processed differently depending on whether data_labels() is supplied or not:

ggplot2/R/scale-date.r

Lines 285 to 290 in f802c5e

if (!is.waive(date_labels)) {
labels <- function(self, x) {
tz <- if (is.null(self$timezone)) "UTC" else self$timezone
date_format(date_labels, tz)(x)
}
}

But, even if it's waive(), it should be handled as trans:

self$trans <- time_trans(self$timezone)

However, scales::time_trans() seems to ignore the timezone...? I'm not yet sure what is the expected behaviour here...

library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union

x <- c(
  "2019-08-17T06:00:00",
  "2019-08-17T07:00:00",
  "2019-08-17T08:00:00"
)

x <- force_tz(ymd_hms(x), tzone = "US/Mountain")

# all returns the same results
t1 <- scales::time_trans("US/Mountain")
t1$transform(x)
#> [1] 1566043200 1566046800 1566050400
t1$format(t1$breaks(range(x)))
#> [1] "06:00" "06:30" "07:00" "07:30" "08:00"

t2 <- scales::time_trans("US/Eastern")
t2$transform(x)
#> [1] 1566043200 1566046800 1566050400
t2$format(t2$breaks(range(x)))
#> [1] "06:00" "06:30" "07:00" "07:30" "08:00"

Created on 2020-05-19 by the reprex package (v0.3.0)

@swebs
Copy link
Author

swebs commented May 19, 2020

Aha, but look at this, these are different:

> t1$inverse(t1$transform(x))
[1] "2019-08-17 06:00:00 MDT" "2019-08-17 07:00:00 MDT" "2019-08-17 08:00:00 MDT"
> t2$inverse(t2$transform(x))
[1] "2019-08-17 08:00:00 EDT" "2019-08-17 09:00:00 EDT" "2019-08-17 10:00:00 EDT"

@swebs
Copy link
Author

swebs commented May 23, 2020

Ok, so, I think to summarize: I believe the intention is that when you pass the timezone parameter, the tick labels will use that timezone. This works when you also specify date_labels but not otherwise, so I think this is an "actual problem." It seems like the issue is either in scales or is due to a misinterpretation of the way scales::time_trans is supposed to work by the ggplot2 authors.

Can anyone confirm if my above interpretation is correct? If so, any thoughts as to whether this is a ggplot2 issue or scales issue?

@thomasp85
Copy link
Member

This is a bug in ggplot2

@thomasp85 thomasp85 added bug an unexpected problem or unintended behavior scales 🐍 labels Aug 31, 2020
@thomasp85
Copy link
Member

Just leaving some comments for whoever ends up fixing this:

The issue is that the trans object is never constructed with the given timezone. The code pointed to by @yutannihilation is only for fetching a timezone from the data if none is given.
Second problem is that the breaks function in the transformation returns a named vector and thus the names of the vector is used unquestioned in get_labels() so even if the trans object was constructed correctly it wouldn't matter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior scales 🐍
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants