/
scale-date.r
239 lines (215 loc) · 7.83 KB
/
scale-date.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#' Positions scale for date/times
#'
#' Use
#' \code{scale_*_date} for dates (class \code{Date}),
#' \code{scale_*_datetime} for datetimes (class \code{POSIXct}), and
#' \code{scale_*_time} for times (class \code{hms}).
#'
#' @name scale_date
#' @inheritParams continuous_scale
#' @inheritParams scale_x_continuous
#' @param date_breaks A string giving the distance between breaks like "2
#' weeks", or "10 years". If both \code{breaks} and \code{date_breaks} are
#' specified, \code{date_breaks} wins.
#' @param date_minor_breaks A string giving the distance between minor breaks
#' like "2 weeks", or "10 years". If both \code{minor_breaks} and
#' \code{date_minor_breaks} are specified, \code{date_minor_breaks} wins.
#' @param date_labels A string giving the formatting specification for the
#' labels. Codes are defined in \code{\link{strftime}}. If both \code{labels}
#' and \code{date_labels} are specified, \code{date_labels} wins.
#' @param timezone The timezone to use for display on the axes. The default
#' (\code{NULL}) uses the timezone encoded in the data.
#' @seealso \code{\link{scale_continuous}} for continuous position scales.
#' @examples
#' last_month <- Sys.Date() - 0:29
#' df <- data.frame(
#' date = last_month,
#' price = runif(30)
#' )
#' base <- ggplot(df, aes(date, price)) +
#' geom_line()
#'
#' # The date scale will attempt to pick sensible defaults for
#' # major and minor tick marks. Override with date_breaks, date_labels
#' # date_minor_breaks arguments.
#' base + scale_x_date(date_labels = "%b %d")
#' base + scale_x_date(date_breaks = "1 week", date_labels = "%W")
#' base + scale_x_date(date_minor_breaks = "1 day")
#'
#' # Set limits
#' base + scale_x_date(limits = c(Sys.Date() - 7, NA))
NULL
#' @rdname scale_date
#' @export
scale_x_date <- function(name = waiver(),
breaks = waiver(), date_breaks = waiver(),
labels = waiver(), date_labels = waiver(),
minor_breaks = waiver(), date_minor_breaks = waiver(),
limits = NULL, expand = waiver(), position = "bottom") {
scale_datetime(c("x", "xmin", "xmax", "xend"), "date",
name = name,
breaks = breaks, date_breaks = date_breaks,
labels = labels, date_labels = date_labels,
minor_breaks = minor_breaks, date_minor_breaks = date_minor_breaks,
limits = limits, expand = expand, position = position
)
}
#' @rdname scale_date
#' @export
scale_y_date <- function(name = waiver(),
breaks = waiver(), date_breaks = waiver(),
labels = waiver(), date_labels = waiver(),
minor_breaks = waiver(), date_minor_breaks = waiver(),
limits = NULL, expand = waiver(), position = "left") {
scale_datetime(c("y", "ymin", "ymax", "yend"), "date",
name = name,
breaks = breaks, date_breaks = date_breaks,
labels = labels, date_labels = date_labels,
minor_breaks = minor_breaks, date_minor_breaks = date_minor_breaks,
limits = limits, expand = expand, position = position
)
}
#' @export
#' @rdname scale_date
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(),
position = "bottom") {
scale_datetime(c("x", "xmin", "xmax", "xend"), "time",
name = name,
breaks = breaks, date_breaks = date_breaks,
labels = labels, date_labels = date_labels,
minor_breaks = minor_breaks, date_minor_breaks = date_minor_breaks,
timezone = timezone, limits = limits, expand = expand, position = position
)
}
#' @rdname scale_date
#' @export
scale_y_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(),
position = "left") {
scale_datetime(c("y", "ymin", "ymax", "yend"), "time",
name = name,
breaks = breaks, date_breaks = date_breaks,
labels = labels, date_labels = date_labels,
minor_breaks = minor_breaks, date_minor_breaks = date_minor_breaks,
timezone = timezone, limits = limits, expand = expand, position = position
)
}
#' @export
#' @rdname scale_date
scale_x_time <- function(name = waiver(),
breaks = waiver(),
minor_breaks = waiver(),
labels = waiver(),
limits = NULL,
expand = waiver(),
oob = censor,
na.value = NA_real_,
position = "bottom") {
scale_x_continuous(
name = name,
breaks = breaks,
labels = labels,
minor_breaks = minor_breaks,
limits = limits,
expand = expand,
oob = oob,
na.value = na.value,
position = position,
trans = scales::hms_trans()
)
}
#' @rdname scale_date
#' @export
scale_y_time <- function(name = waiver(),
breaks = waiver(),
minor_breaks = waiver(),
labels = waiver(),
limits = NULL,
expand = waiver(),
oob = censor,
na.value = NA_real_,
position = "left") {
scale_y_continuous(
name = name,
breaks = breaks,
labels = labels,
minor_breaks = minor_breaks,
limits = limits,
expand = expand,
oob = oob,
na.value = na.value,
position = position,
trans = scales::hms_trans()
)
}
scale_datetime <- function(aesthetics, trans,
breaks = pretty_breaks(), minor_breaks = waiver(),
labels = waiver(), date_breaks = waiver(),
date_labels = waiver(),
date_minor_breaks = waiver(), timezone = NULL,
...) {
# Backward compatibility
if (is.character(breaks)) breaks <- date_breaks(breaks)
if (is.character(minor_breaks)) minor_breaks <- date_breaks(minor_breaks)
if (!is.waive(date_breaks)) {
breaks <- date_breaks(date_breaks)
}
if (!is.waive(date_minor_breaks)) {
minor_breaks <- date_breaks(date_minor_breaks)
}
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)
}
}
name <- switch(trans,
date = "date",
time = "datetime"
)
scale_class <- switch(trans,
date = ScaleContinuousDate,
time = ScaleContinuousDatetime
)
sc <- continuous_scale(
aesthetics, name, identity,
breaks = breaks, minor_breaks = minor_breaks, labels = labels,
guide = "none", trans = trans, ..., super = scale_class
)
sc$timezone <- timezone
sc
}
#' @rdname ggplot2-ggproto
#' @format NULL
#' @usage NULL
#' @export
ScaleContinuousDatetime <- ggproto("ScaleContinuousDatetime", ScaleContinuous,
timezone = NULL,
transform = function(self, x) {
tz <- attr(x, "tzone")
if (is.null(self$timezone) && !is.null(tz)) {
self$timezone <- tz
self$trans <- time_trans(self$timezone)
}
ggproto_parent(ScaleContinuous, self)$transform(x)
},
map = function(self, x, limits = self$get_limits()) {
self$oob(x, limits)
}
)
#' @rdname ggplot2-ggproto
#' @format NULL
#' @usage NULL
#' @export
ScaleContinuousDate <- ggproto("ScaleContinuousDate", ScaleContinuous,
map = function(self, x, limits = self$get_limits()) {
self$oob(x, limits)
}
)