|
59 | 59 | #' |
60 | 60 | #' # Secondary axes work for date and datetime scales too: |
61 | 61 | #' df <- data.frame( |
62 | | -#' dx = seq(as.POSIXct("2012-02-29 12:00:00", |
63 | | -#' tz = "UTC", |
64 | | -#' format = "%Y-%m-%d %H:%M:%S" |
65 | | -#' ), |
66 | | -#' length.out = 10, by = "4 hour" |
| 62 | +#' dx = seq( |
| 63 | +#' as.POSIXct("2012-02-29 12:00:00", tz = "UTC"), |
| 64 | +#' length.out = 10, |
| 65 | +#' by = "4 hour" |
67 | 66 | #' ), |
68 | 67 | #' price = seq(20, 200000, length.out = 10) |
69 | 68 | #' ) |
70 | 69 | #' |
71 | 70 | #' # This may useful for labelling different time scales in the same plot |
72 | | -#' ggplot(df, aes(x = dx, y = price)) + geom_line() + |
73 | | -#' scale_x_datetime("Date", date_labels = "%b %d", |
74 | | -#' date_breaks = "6 hour", |
75 | | -#' sec.axis = dup_axis(name = "Time of Day", |
76 | | -#' labels = scales::time_format("%I %p"))) |
| 71 | +#' ggplot(df, aes(x = dx, y = price)) + |
| 72 | +#' geom_line() + |
| 73 | +#' scale_x_datetime( |
| 74 | +#' "Date", |
| 75 | +#' date_labels = "%b %d", |
| 76 | +#' date_breaks = "6 hour", |
| 77 | +#' sec.axis = dup_axis( |
| 78 | +#' name = "Time of Day", |
| 79 | +#' labels = scales::time_format("%I %p") |
| 80 | +#' ) |
| 81 | +#' ) |
77 | 82 | #' |
78 | 83 | #' # or to transform axes for different timezones |
79 | | -#' ggplot(df, aes(x = dx, y = price)) + geom_line() + |
80 | | -#' scale_x_datetime("GMT", date_labels = "%b %d %I %p", |
81 | | -#' sec.axis = sec_axis(~ . + 8 * 3600, name = "GMT+8", |
82 | | -#' labels = scales::time_format("%b %d %I %p"))) |
| 84 | +#' ggplot(df, aes(x = dx, y = price)) + |
| 85 | +#' geom_line() + |
| 86 | +#' scale_x_datetime(" |
| 87 | +#' GMT", |
| 88 | +#' date_labels = "%b %d %I %p", |
| 89 | +#' sec.axis = sec_axis( |
| 90 | +#' ~ . + 8 * 3600, |
| 91 | +#' name = "GMT+8", |
| 92 | +#' labels = scales::time_format("%b %d %I %p") |
| 93 | +#' ) |
| 94 | +#' ) |
83 | 95 | #' |
84 | 96 | #' @export |
85 | 97 | sec_axis <- function(trans = NULL, name = waiver(), breaks = waiver(), labels = waiver(), |
|
0 commit comments