Problem with scale_x_datetime timezone parameter

I'm trying to use the timezone parameter of the scale_x_datetime function in ggplot2. Reprex is below. Basically I have data which includes a timezone, but I can't change the tick labels using the timezone parameter as I understand it to work. I was able to use labels=scales::label_date(tz = "US/Eastern") as a workaround, but just wondering if anyone can help me with understanding the issue.
ggplot 3.2.1, lubridate 1.7.4, R 3.6.2

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

This seems like a bug to me. But I'm not great at ggplot2 so maybe let a couple of other folks from the community weigh in before filing a report.

Hi swebs!
It looks like timezone is not an allowed argument for scale_x_datetime, and (unfortunately) it does not warn you when this argument is ignored.
The only allowed arguments are breaks, minor breaks, expand, and the standard arguments for a continuous scale.

However, I think I have a workaround.

Does this achieve your goal?

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

plotdata <- tribble(
  ~t_stamp, ~value,
  "2019-08-17T06:00:00", 1,
  "2019-08-17T07:00:00", 2,
  "2019-08-17T08:00:00", 3
) %>% 
  mutate(t_stamp = ymd_hms(t_stamp), 
         tz = "US/Mountain")

plotdata %>% 
  ggplot(mapping=aes(x=t_stamp, y=value)) +
  geom_point(mapping=aes(x=t_stamp, y=value)) +
  scale_x_datetime()


plotdata %>% 
  mutate(t_stamp = 
        with_tz(t_stamp, tzone="US/Eastern")) %>% 
  ggplot(mapping=aes(x=t_stamp, y=value)) +
  geom_point() +
  scale_x_datetime()

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

@phiggins Actually, it is a valid function parameter but does not seem to have the desired effect.

args(ggplot2::scale_x_datetime)
#> function (name = waiver(), breaks = waiver(), date_breaks = waiver(), 
#>     labels = waiver(), date_labels = waiver(), minor_breaks = waiver(), 
#>     date_minor_breaks = waiver(), timezone = NULL, limits = NULL, 
#>     expand = waiver(), guide = waiver(), position = "bottom", 
#>     sec.axis = waiver()) 
#> NULL

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

But thanks for providing a workaround. I'm sure the OP will find it very handy.

Thanks everyone! Appreciate the replies. I posted an issue for this here:

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.