Skip to content

Commit 32e699e

Browse files
authored
Improve datetime scales (#2186)
* Rewrite scale_datetime and update position/size/alpha scales * Fix style for scale_(x|y)_(date|datetime) * Update fill and colour scales for datetimes * Add to NEWS.md * Test new datetime alpha/size/colour scales * Test should use scale_colour_datetime, not scale_colour_gradient
1 parent a0a0652 commit 32e699e

File tree

8 files changed

+226
-59
lines changed

8 files changed

+226
-59
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,8 @@ export(render_strips)
368368
export(resolution)
369369
export(scale_alpha)
370370
export(scale_alpha_continuous)
371+
export(scale_alpha_date)
372+
export(scale_alpha_datetime)
371373
export(scale_alpha_discrete)
372374
export(scale_alpha_identity)
373375
export(scale_alpha_manual)

NEWS.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# ggplot2 2.2.1.9000
22

3+
* Updated datetime scales for `alpha`, `size`, `colour`, and `fill` can take
4+
`date_breaks` and `date_labels` arguments (@karawoo, #1526).
5+
6+
* `scale_alpha()` gains date and date-time variants (@karawoo, #1526).
7+
38
* Axes positioned on the top and to the right can now customize their ticks and
49
lines separately (@thomasp85, #1899)
510

R/scale-alpha.r

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,17 @@ scale_alpha_discrete <- function(..., range = c(0.1, 1)) {
3232
discrete_scale("alpha", "alpha_d",
3333
function(n) seq(range[1], range[2], length.out = n), ...)
3434
}
35+
36+
#' @rdname scale_alpha
37+
#' @export
38+
#' @usage NULL
39+
scale_alpha_datetime <- function(..., range = c(0.1, 1)) {
40+
datetime_scale("alpha", "time", palette = rescale_pal(range), ...)
41+
}
42+
43+
#' @rdname scale_alpha
44+
#' @export
45+
#' @usage NULL
46+
scale_alpha_date <- function(..., range = c(0.1, 1)){
47+
datetime_scale("alpha", "date", palette = rescale_pal(range), ...)
48+
}

R/scale-date.r

Lines changed: 120 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -45,71 +45,129 @@ NULL
4545
#' @rdname scale_date
4646
#' @export
4747
scale_x_date <- function(name = waiver(),
48-
breaks = waiver(), date_breaks = waiver(),
49-
labels = waiver(), date_labels = waiver(),
50-
minor_breaks = waiver(), date_minor_breaks = waiver(),
51-
limits = NULL, expand = waiver(), position = "bottom") {
48+
breaks = waiver(),
49+
date_breaks = waiver(),
50+
labels = waiver(),
51+
date_labels = waiver(),
52+
minor_breaks = waiver(),
53+
date_minor_breaks = waiver(),
54+
limits = NULL,
55+
expand = waiver(),
56+
position = "bottom") {
5257

53-
scale_datetime(c("x", "xmin", "xmax", "xend"), "date",
58+
datetime_scale(
59+
c("x", "xmin", "xmax", "xend"),
60+
"date",
5461
name = name,
55-
breaks = breaks, date_breaks = date_breaks,
56-
labels = labels, date_labels = date_labels,
57-
minor_breaks = minor_breaks, date_minor_breaks = date_minor_breaks,
58-
limits = limits, expand = expand, position = position
62+
palette = identity,
63+
breaks = breaks,
64+
date_breaks = date_breaks,
65+
labels = labels,
66+
date_labels = date_labels,
67+
minor_breaks = minor_breaks,
68+
date_minor_breaks = date_minor_breaks,
69+
guide = "none",
70+
limits = limits,
71+
expand = expand,
72+
position = position
5973
)
6074
}
6175

6276
#' @rdname scale_date
6377
#' @export
6478
scale_y_date <- function(name = waiver(),
65-
breaks = waiver(), date_breaks = waiver(),
66-
labels = waiver(), date_labels = waiver(),
67-
minor_breaks = waiver(), date_minor_breaks = waiver(),
68-
limits = NULL, expand = waiver(), position = "left") {
79+
breaks = waiver(),
80+
date_breaks = waiver(),
81+
labels = waiver(),
82+
date_labels = waiver(),
83+
minor_breaks = waiver(),
84+
date_minor_breaks = waiver(),
85+
limits = NULL,
86+
expand = waiver(),
87+
position = "left") {
6988

70-
scale_datetime(c("y", "ymin", "ymax", "yend"), "date",
89+
datetime_scale(
90+
c("y", "ymin", "ymax", "yend"),
91+
"date",
7192
name = name,
72-
breaks = breaks, date_breaks = date_breaks,
73-
labels = labels, date_labels = date_labels,
74-
minor_breaks = minor_breaks, date_minor_breaks = date_minor_breaks,
75-
limits = limits, expand = expand, position = position
93+
palette = identity,
94+
breaks = breaks,
95+
date_breaks = date_breaks,
96+
labels = labels,
97+
date_labels = date_labels,
98+
minor_breaks = minor_breaks,
99+
date_minor_breaks = date_minor_breaks,
100+
guide = "none",
101+
limits = limits,
102+
expand = expand,
103+
position = position
76104
)
77105
}
78106

79107
#' @export
80108
#' @rdname scale_date
81109
scale_x_datetime <- function(name = waiver(),
82-
breaks = waiver(), date_breaks = waiver(),
83-
labels = waiver(), date_labels = waiver(),
84-
minor_breaks = waiver(), date_minor_breaks = waiver(),
85-
timezone = NULL, limits = NULL, expand = waiver(),
110+
breaks = waiver(),
111+
date_breaks = waiver(),
112+
labels = waiver(),
113+
date_labels = waiver(),
114+
minor_breaks = waiver(),
115+
date_minor_breaks = waiver(),
116+
timezone = NULL,
117+
limits = NULL,
118+
expand = waiver(),
86119
position = "bottom") {
87120

88-
scale_datetime(c("x", "xmin", "xmax", "xend"), "time",
121+
datetime_scale(
122+
c("x", "xmin", "xmax", "xend"),
123+
"time",
89124
name = name,
90-
breaks = breaks, date_breaks = date_breaks,
91-
labels = labels, date_labels = date_labels,
92-
minor_breaks = minor_breaks, date_minor_breaks = date_minor_breaks,
93-
timezone = timezone, limits = limits, expand = expand, position = position
125+
palette = identity,
126+
breaks = breaks,
127+
date_breaks = date_breaks,
128+
labels = labels,
129+
date_labels = date_labels,
130+
minor_breaks = minor_breaks,
131+
date_minor_breaks = date_minor_breaks,
132+
timezone = timezone,
133+
guide = "none",
134+
limits = limits,
135+
expand = expand,
136+
position = position
94137
)
95138
}
96139

97140

98141
#' @rdname scale_date
99142
#' @export
100143
scale_y_datetime <- function(name = waiver(),
101-
breaks = waiver(), date_breaks = waiver(),
102-
labels = waiver(), date_labels = waiver(),
103-
minor_breaks = waiver(), date_minor_breaks = waiver(),
104-
timezone = NULL, limits = NULL, expand = waiver(),
144+
breaks = waiver(),
145+
date_breaks = waiver(),
146+
labels = waiver(),
147+
date_labels = waiver(),
148+
minor_breaks = waiver(),
149+
date_minor_breaks = waiver(),
150+
timezone = NULL,
151+
limits = NULL,
152+
expand = waiver(),
105153
position = "left") {
106154

107-
scale_datetime(c("y", "ymin", "ymax", "yend"), "time",
155+
datetime_scale(
156+
c("y", "ymin", "ymax", "yend"),
157+
"time",
108158
name = name,
109-
breaks = breaks, date_breaks = date_breaks,
110-
labels = labels, date_labels = date_labels,
111-
minor_breaks = minor_breaks, date_minor_breaks = date_minor_breaks,
112-
timezone = timezone, limits = limits, expand = expand, position = position
159+
palette = identity,
160+
breaks = breaks,
161+
date_breaks = date_breaks,
162+
labels = labels,
163+
date_labels = date_labels,
164+
minor_breaks = minor_breaks,
165+
date_minor_breaks = date_minor_breaks,
166+
timezone = timezone,
167+
guide = "none",
168+
limits = limits,
169+
expand = expand,
170+
position = position
113171
)
114172
}
115173

@@ -168,12 +226,13 @@ scale_y_time <- function(name = waiver(),
168226
)
169227
}
170228

171-
scale_datetime <- function(aesthetics, trans,
229+
## rename to datetime_scale
230+
datetime_scale <- function(aesthetics, trans, palette,
172231
breaks = pretty_breaks(), minor_breaks = waiver(),
173232
labels = waiver(), date_breaks = waiver(),
174233
date_labels = waiver(),
175234
date_minor_breaks = waiver(), timezone = NULL,
176-
...) {
235+
guide = "legend", ...) {
177236

178237

179238
# Backward compatibility
@@ -197,20 +256,35 @@ scale_datetime <- function(aesthetics, trans,
197256
date = "date",
198257
time = "datetime"
199258
)
200-
scale_class <- switch(trans,
201-
date = ScaleContinuousDate,
202-
time = ScaleContinuousDatetime
203-
)
259+
260+
# x/y position aesthetics should use ScaleContinuousDate or
261+
# ScaleContinuousDatetime; others use ScaleContinuous
262+
if (all(aesthetics %in% c("x", "xmin", "xmax", "xend", "y", "ymin", "ymax", "yend"))) {
263+
scale_class <- switch(
264+
trans,
265+
date = ScaleContinuousDate,
266+
time = ScaleContinuousDatetime
267+
)
268+
} else {
269+
scale_class <- ScaleContinuous
270+
}
271+
204272
sc <- continuous_scale(
205-
aesthetics, name, identity,
206-
breaks = breaks, minor_breaks = minor_breaks, labels = labels,
207-
guide = "none", trans = trans, ..., super = scale_class
273+
aesthetics,
274+
name,
275+
palette = palette,
276+
breaks = breaks,
277+
minor_breaks = minor_breaks,
278+
labels = labels,
279+
guide = guide,
280+
trans = trans,
281+
...,
282+
super = scale_class
208283
)
209284
sc$timezone <- timezone
210285
sc
211286
}
212287

213-
214288
#' @rdname ggplot2-ggproto
215289
#' @format NULL
216290
#' @usage NULL

R/scale-size.r

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,13 @@ scale_size_area <- function(..., max_size = 6) {
8080
#' @rdname scale_size
8181
#' @export
8282
#' @usage NULL
83-
scale_size_datetime <- function() {
84-
scale_size_continuous(trans = "time")
83+
scale_size_datetime <- function(..., range = c(1, 6)) {
84+
datetime_scale("size", "time", palette = area_pal(range), ...)
8585
}
8686

8787
#' @rdname scale_size
8888
#' @export
8989
#' @usage NULL
90-
scale_size_date <- function() {
91-
scale_size_continuous(trans = "date")
90+
scale_size_date <- function(..., range = c(1, 6)) {
91+
datetime_scale("size", "date", palette = area_pal(range), ...)
9292
}
93-

R/zxx.r

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,39 @@ scale_colour_continuous <- scale_colour_gradient
1313
#' @export
1414
#' @rdname scale_gradient
1515
#' @usage NULL
16-
scale_colour_datetime <- function() {
17-
scale_colour_continuous(trans = "time")
16+
scale_colour_datetime <- function(...,
17+
low = "#132B43",
18+
high = "#56B1F7",
19+
space = "Lab",
20+
na.value = "grey50",
21+
guide = "colourbar") {
22+
datetime_scale(
23+
"colour",
24+
"time",
25+
palette = seq_gradient_pal(low, high, space),
26+
na.value = na.value,
27+
guide = guide,
28+
...
29+
)
1830
}
1931

2032
#' @export
2133
#' @rdname scale_gradient
2234
#' @usage NULL
23-
scale_colour_date <- function() {
24-
scale_colour_continuous(trans = "date")
35+
scale_colour_date <- function(...,
36+
low = "#132B43",
37+
high = "#56B1F7",
38+
space = "Lab",
39+
na.value = "grey50",
40+
guide = "colourbar") {
41+
datetime_scale(
42+
"colour",
43+
"date",
44+
palette = seq_gradient_pal(low, high, space),
45+
na.value = na.value,
46+
guide = guide,
47+
...
48+
)
2549
}
2650

2751
#' @export
@@ -37,15 +61,40 @@ scale_fill_continuous <- scale_fill_gradient
3761
#' @export
3862
#' @rdname scale_gradient
3963
#' @usage NULL
40-
scale_fill_datetime <- function() {
41-
scale_fill_continuous(trans = "time")
64+
scale_fill_datetime <- function(...,
65+
low = "#132B43",
66+
high = "#56B1F7",
67+
space = "Lab",
68+
na.value = "grey50",
69+
guide = "colourbar") {
70+
datetime_scale(
71+
"fill",
72+
"time",
73+
palette = seq_gradient_pal(low, high, space),
74+
na.value = na.value,
75+
guide = guide,
76+
...
77+
)
78+
4279
}
4380

4481
#' @export
4582
#' @rdname scale_gradient
4683
#' @usage NULL
47-
scale_fill_date <- function() {
48-
scale_fill_continuous(trans = "date")
84+
scale_fill_date <- function(...,
85+
low = "#132B43",
86+
high = "#56B1F7",
87+
space = "Lab",
88+
na.value = "grey50",
89+
guide = "colourbar") {
90+
datetime_scale(
91+
"fill",
92+
"date",
93+
palette = seq_gradient_pal(low, high, space),
94+
na.value = na.value,
95+
guide = guide,
96+
...
97+
)
4998
}
5099

51100

man/scale_alpha.Rd

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)