Skip to content

Commit

Permalink
Support hms position scales
Browse files Browse the repository at this point in the history
Fixes #1752
  • Loading branch information
hadley committed Oct 5, 2016
1 parent 1c970c8 commit b87df24
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 12 deletions.
4 changes: 3 additions & 1 deletion DESCRIPTION
Expand Up @@ -22,7 +22,7 @@ Imports:
MASS,
plyr (>= 1.7.1),
reshape2,
scales (>= 0.3.0),
scales (>= 0.4.0.9002),
stats,
tibble,
lazyeval
Expand All @@ -44,6 +44,8 @@ Suggests:
rpart,
rmarkdown,
svglite
Remotes:
hadley/scales
Enhances: sp
License: GPL-2 | file LICENSE
URL: http://ggplot2.org, https://github.com/hadley/ggplot2
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Expand Up @@ -83,6 +83,7 @@ S3method(scale_type,POSIXt)
S3method(scale_type,character)
S3method(scale_type,default)
S3method(scale_type,factor)
S3method(scale_type,hms)
S3method(scale_type,logical)
S3method(scale_type,numeric)
S3method(scale_type,ordered)
Expand Down Expand Up @@ -420,13 +421,15 @@ export(scale_x_discrete)
export(scale_x_log10)
export(scale_x_reverse)
export(scale_x_sqrt)
export(scale_x_time)
export(scale_y_continuous)
export(scale_y_date)
export(scale_y_datetime)
export(scale_y_discrete)
export(scale_y_log10)
export(scale_y_reverse)
export(scale_y_sqrt)
export(scale_y_time)
export(sec_axis)
export(should_stop)
export(stat_bin)
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
@@ -1,5 +1,8 @@
# ggplot2 2.1.0.9000

* Added scales `scale_x_time()` and `scale_y_time()` which are applied
automatically when you plot objects of type hms (#1752).

* `layer()` gains new `check.aes` and `check.param` arguments. These allow
geom/stat authors to optional suppress checks for known aesthetics/parameters.
Currently this is used only in `geom_blank()` which powers `expand_limits()`
Expand Down
81 changes: 73 additions & 8 deletions R/scale-date.r
@@ -1,10 +1,13 @@
#' Position scale, date & date times
#' Positions scale for date/times
#'
#' Use \code{scale_*_date} with \code{Date} variables, and
#' \code{scale_*_datetime} with \code{POSIXct} variables.
#' 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.
Expand Down Expand Up @@ -71,7 +74,6 @@ scale_y_date <- function(name = waiver(),
)
}


#' @export
#' @rdname scale_date
scale_x_datetime <- function(name = waiver(),
Expand Down Expand Up @@ -109,14 +111,68 @@ scale_y_datetime <- function(name = waiver(),
)
}



#' @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,
...) {

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

# Backward compatibility
if (is.character(breaks)) breaks <- date_breaks(breaks)
Expand All @@ -135,10 +191,19 @@ scale_datetime <- function(aesthetics, trans,
}
}

scale_class <- switch(trans, date = ScaleContinuousDate, time = ScaleContinuousDatetime)
sc <- continuous_scale(aesthetics, name, identity,
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)
guide = "none", trans = trans, ..., super = scale_class
)
sc$timezone <- timezone
sc
}
Expand Down
3 changes: 3 additions & 0 deletions R/scale-type.R
Expand Up @@ -64,3 +64,6 @@ scale_type.Date <- function(x) c("date", "continuous")

#' @export
scale_type.numeric <- function(x) "continuous"

#' @export
scale_type.hms <- function(x) "time"
23 changes: 20 additions & 3 deletions man/scale_date.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b87df24

Please sign in to comment.