Last active
June 24, 2020 20:35
-
-
Save mpettis/50632a45882398d5b030cdc5927ff7b5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(lubridate) | |
library(tidyverse) | |
seq( | |
from=as.POSIXct("2012-1-1 0:00", tz="UTC"), | |
to=as.POSIXct("2012-1-3 23:00", tz="UTC"), | |
by="hour" | |
) -> | |
dt | |
tibble( | |
dt_utc = dt, | |
dt_ny = with_tz(dt, tzone = "America/New_York"), | |
dt_la = with_tz(dt, tzone = "America/Los_Angeles"), | |
val = sin(2 * pi * as.numeric(dt) / 86400)) -> | |
df_work | |
## UTC x-axis | |
## Labeled timestamps indeed agree with UTC | |
ggplot(data=df_work) + | |
geom_line(mapping=aes(x=dt_utc, y=val)) + | |
scale_x_datetime(breaks = "1 hour") + | |
theme( | |
axis.text.x = element_text(angle = 90) | |
) + | |
labs( | |
title="UTC Time" | |
) | |
## Pacific times | |
## Indeed, x-axis labels are in America/Los_Angeles | |
ggplot(data=df_work) + | |
geom_line(mapping=aes(x=dt_la, y=val)) + | |
scale_x_datetime(breaks = "1 hour") + | |
theme( | |
axis.text.x = element_text(angle = 90) | |
) + | |
labs( | |
title="America/Los_Angeles Time" | |
) | |
## UTC, but America/Los_Angeles supplied to scale_x_datetime | |
## No, labels are again in UTC. Doesn't behave as I expect. | |
## I think this is rather a 'force_tz' rather than `with_tz'. | |
ggplot(data=df_work) + | |
geom_line(mapping=aes(x=dt_utc, y=val)) + | |
scale_x_datetime(breaks = "1 hour", timezone = "America/Los_Angeles") + | |
theme( | |
axis.text.x = element_text(angle = 90) | |
) + | |
labs( | |
title="Original timestamp in UTC, scale_x_datetime timezone arg is America/Los_Angeles Time" | |
) | |
## UTC, but America/Los_Angeles supplied to scale_x_datetime | |
## Added explicit `date_labels` argument. | |
## Now it works. | |
## See: https://github.com/tidyverse/ggplot2/issues/4007 | |
ggplot(data=df_work) + | |
geom_line(mapping=aes(x=dt_utc, y=val)) + | |
scale_x_datetime(breaks = "1 hour", timezone = "America/Los_Angeles", date_labels = "%F %T") + | |
theme( | |
axis.text.x = element_text(angle = 90) | |
) + | |
labs( | |
title="Original timestamp in UTC, scale_x_datetime timezone arg is America/Los_Angeles Time" | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment