Skip to content

Commit b87df24

Browse files
committed
Support hms position scales
Fixes #1752
1 parent 1c970c8 commit b87df24

File tree

6 files changed

+105
-12
lines changed

6 files changed

+105
-12
lines changed

DESCRIPTION

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Imports:
2222
MASS,
2323
plyr (>= 1.7.1),
2424
reshape2,
25-
scales (>= 0.3.0),
25+
scales (>= 0.4.0.9002),
2626
stats,
2727
tibble,
2828
lazyeval
@@ -44,6 +44,8 @@ Suggests:
4444
rpart,
4545
rmarkdown,
4646
svglite
47+
Remotes:
48+
hadley/scales
4749
Enhances: sp
4850
License: GPL-2 | file LICENSE
4951
URL: http://ggplot2.org, https://github.com/hadley/ggplot2

NAMESPACE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ S3method(scale_type,POSIXt)
8383
S3method(scale_type,character)
8484
S3method(scale_type,default)
8585
S3method(scale_type,factor)
86+
S3method(scale_type,hms)
8687
S3method(scale_type,logical)
8788
S3method(scale_type,numeric)
8889
S3method(scale_type,ordered)
@@ -420,13 +421,15 @@ export(scale_x_discrete)
420421
export(scale_x_log10)
421422
export(scale_x_reverse)
422423
export(scale_x_sqrt)
424+
export(scale_x_time)
423425
export(scale_y_continuous)
424426
export(scale_y_date)
425427
export(scale_y_datetime)
426428
export(scale_y_discrete)
427429
export(scale_y_log10)
428430
export(scale_y_reverse)
429431
export(scale_y_sqrt)
432+
export(scale_y_time)
430433
export(sec_axis)
431434
export(should_stop)
432435
export(stat_bin)

NEWS.md

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

3+
* Added scales `scale_x_time()` and `scale_y_time()` which are applied
4+
automatically when you plot objects of type hms (#1752).
5+
36
* `layer()` gains new `check.aes` and `check.param` arguments. These allow
47
geom/stat authors to optional suppress checks for known aesthetics/parameters.
58
Currently this is used only in `geom_blank()` which powers `expand_limits()`

R/scale-date.r

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
#' Position scale, date & date times
1+
#' Positions scale for date/times
22
#'
3-
#' Use \code{scale_*_date} with \code{Date} variables, and
4-
#' \code{scale_*_datetime} with \code{POSIXct} variables.
3+
#' Use
4+
#' \code{scale_*_date} for dates (class \code{Date}),
5+
#' \code{scale_*_datetime} for datetimes (class \code{POSIXct}), and
6+
#' \code{scale_*_time} for times (class \code{hms}).
57
#'
68
#' @name scale_date
79
#' @inheritParams continuous_scale
10+
#' @inheritParams scale_x_continuous
811
#' @param date_breaks A string giving the distance between breaks like "2
912
#' weeks", or "10 years". If both \code{breaks} and \code{date_breaks} are
1013
#' specified, \code{date_breaks} wins.
@@ -71,7 +74,6 @@ scale_y_date <- function(name = waiver(),
7174
)
7275
}
7376

74-
7577
#' @export
7678
#' @rdname scale_date
7779
scale_x_datetime <- function(name = waiver(),
@@ -109,14 +111,68 @@ scale_y_datetime <- function(name = waiver(),
109111
)
110112
}
111113

114+
115+
116+
#' @export
117+
#' @rdname scale_date
118+
scale_x_time <- function(name = waiver(),
119+
breaks = waiver(),
120+
minor_breaks = waiver(),
121+
labels = waiver(),
122+
limits = NULL,
123+
expand = waiver(),
124+
oob = censor,
125+
na.value = NA_real_,
126+
position = "bottom") {
127+
128+
scale_x_continuous(
129+
name = name,
130+
breaks = breaks,
131+
labels = labels,
132+
minor_breaks = minor_breaks,
133+
limits = limits,
134+
expand = expand,
135+
oob = oob,
136+
na.value = na.value,
137+
position = position,
138+
trans = scales::hms_trans()
139+
)
140+
}
141+
142+
143+
#' @rdname scale_date
144+
#' @export
145+
scale_y_time <- function(name = waiver(),
146+
breaks = waiver(),
147+
minor_breaks = waiver(),
148+
labels = waiver(),
149+
limits = NULL,
150+
expand = waiver(),
151+
oob = censor,
152+
na.value = NA_real_,
153+
position = "left") {
154+
155+
scale_y_continuous(
156+
name = name,
157+
breaks = breaks,
158+
labels = labels,
159+
minor_breaks = minor_breaks,
160+
limits = limits,
161+
expand = expand,
162+
oob = oob,
163+
na.value = na.value,
164+
position = position,
165+
trans = scales::hms_trans()
166+
)
167+
}
168+
112169
scale_datetime <- function(aesthetics, trans,
113170
breaks = pretty_breaks(), minor_breaks = waiver(),
114171
labels = waiver(), date_breaks = waiver(),
115172
date_labels = waiver(),
116173
date_minor_breaks = waiver(), timezone = NULL,
117174
...) {
118175

119-
name <- switch(trans, date = "date", time = "datetime")
120176

121177
# Backward compatibility
122178
if (is.character(breaks)) breaks <- date_breaks(breaks)
@@ -135,10 +191,19 @@ scale_datetime <- function(aesthetics, trans,
135191
}
136192
}
137193

138-
scale_class <- switch(trans, date = ScaleContinuousDate, time = ScaleContinuousDatetime)
139-
sc <- continuous_scale(aesthetics, name, identity,
194+
name <- switch(trans,
195+
date = "date",
196+
time = "datetime"
197+
)
198+
scale_class <- switch(trans,
199+
date = ScaleContinuousDate,
200+
time = ScaleContinuousDatetime
201+
)
202+
sc <- continuous_scale(
203+
aesthetics, name, identity,
140204
breaks = breaks, minor_breaks = minor_breaks, labels = labels,
141-
guide = "none", trans = trans, ..., super = scale_class)
205+
guide = "none", trans = trans, ..., super = scale_class
206+
)
142207
sc$timezone <- timezone
143208
sc
144209
}

R/scale-type.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,6 @@ scale_type.Date <- function(x) c("date", "continuous")
6464

6565
#' @export
6666
scale_type.numeric <- function(x) "continuous"
67+
68+
#' @export
69+
scale_type.hms <- function(x) "time"

man/scale_date.Rd

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

0 commit comments

Comments
 (0)