I've just noticed that sec.axis
does not appear to be supported for date or datetime scales in ggplot2
.
Is this a deliberate decision or an oversight?
I tend to add a duplicate y-axis where there are many data points, particularly where the x-axis is date-based as the most recent data will be on the right of the plot.
Example:
library(tidyverse)
library(lubridate)
dummy_data <-
tribble(~id, ~start, ~finish,
1L, "2017-12-11 09:00:00", "2017-12-11 17:00:00",
2L, "2017-12-12 09:13:00", "2017-12-12 16:58:00",
3L, "2017-12-13 08:51:00", "2017-12-13 17:12:00",
4L, "2017-12-14 09:15:00", "2017-12-14 16:49:00",
5L, "2017-12-15 09:22:00", "2017-12-14 17:03:00") %>%
mutate(start_date = as_date(start),
finish_date = as_date(finish),
start = strftime(start, format = "%H:%M:%S", tz = "UTC"),
finish = strftime(finish, format = "%H:%M:%S", tz = "UTC")) %>%
filter(start_date == finish_date) %>%
gather(start_finish, dummy_time, start, finish) %>%
mutate(dummy_time = ymd_hms(paste("2017-12-11", dummy_time)))
p <-
ggplot(dummy_data,
aes(x = start_date, y = dummy_time,
colour = start_finish)) +
geom_line() +
geom_point()
# this does not work
p + scale_y_datetime(sec.axis = dup_axis())