Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,8 @@ export(render_strips)
export(resolution)
export(scale_alpha)
export(scale_alpha_continuous)
export(scale_alpha_date)
export(scale_alpha_datetime)
export(scale_alpha_discrete)
export(scale_alpha_identity)
export(scale_alpha_manual)
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# ggplot2 2.2.1.9000

* Updated datetime scales for `alpha`, `size`, `colour`, and `fill` can take
`date_breaks` and `date_labels` arguments (@karawoo, #1526).

* `scale_alpha()` gains date and date-time variants (@karawoo, #1526).

* Axes positioned on the top and to the right can now customize their ticks and
lines separately (@thomasp85, #1899)

Expand Down
14 changes: 14 additions & 0 deletions R/scale-alpha.r
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,17 @@ scale_alpha_discrete <- function(..., range = c(0.1, 1)) {
discrete_scale("alpha", "alpha_d",
function(n) seq(range[1], range[2], length.out = n), ...)
}

#' @rdname scale_alpha
#' @export
#' @usage NULL
scale_alpha_datetime <- function(..., range = c(0.1, 1)) {
datetime_scale("alpha", "time", palette = rescale_pal(range), ...)
}

#' @rdname scale_alpha
#' @export
#' @usage NULL
scale_alpha_date <- function(..., range = c(0.1, 1)){
datetime_scale("alpha", "date", palette = rescale_pal(range), ...)
}
166 changes: 120 additions & 46 deletions R/scale-date.r
Original file line number Diff line number Diff line change
Expand Up @@ -45,71 +45,129 @@ 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") {
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",
datetime_scale(
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
palette = identity,
breaks = breaks,
date_breaks = date_breaks,
labels = labels,
date_labels = date_labels,
minor_breaks = minor_breaks,
date_minor_breaks = date_minor_breaks,
guide = "none",
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") {
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",
datetime_scale(
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
palette = identity,
breaks = breaks,
date_breaks = date_breaks,
labels = labels,
date_labels = date_labels,
minor_breaks = minor_breaks,
date_minor_breaks = date_minor_breaks,
guide = "none",
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(),
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",
datetime_scale(
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
palette = identity,
breaks = breaks,
date_breaks = date_breaks,
labels = labels,
date_labels = date_labels,
minor_breaks = minor_breaks,
date_minor_breaks = date_minor_breaks,
timezone = timezone,
guide = "none",
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(),
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",
datetime_scale(
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
palette = identity,
breaks = breaks,
date_breaks = date_breaks,
labels = labels,
date_labels = date_labels,
minor_breaks = minor_breaks,
date_minor_breaks = date_minor_breaks,
timezone = timezone,
guide = "none",
limits = limits,
expand = expand,
position = position
)
}

Expand Down Expand Up @@ -168,12 +226,13 @@ scale_y_time <- function(name = waiver(),
)
}

scale_datetime <- function(aesthetics, trans,
## rename to datetime_scale
datetime_scale <- function(aesthetics, trans, palette,
breaks = pretty_breaks(), minor_breaks = waiver(),
labels = waiver(), date_breaks = waiver(),
date_labels = waiver(),
date_minor_breaks = waiver(), timezone = NULL,
...) {
guide = "legend", ...) {


# Backward compatibility
Expand All @@ -197,20 +256,35 @@ scale_datetime <- function(aesthetics, trans,
date = "date",
time = "datetime"
)
scale_class <- switch(trans,
date = ScaleContinuousDate,
time = ScaleContinuousDatetime
)

# x/y position aesthetics should use ScaleContinuousDate or
# ScaleContinuousDatetime; others use ScaleContinuous
if (all(aesthetics %in% c("x", "xmin", "xmax", "xend", "y", "ymin", "ymax", "yend"))) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is the best way to choose the appropriate super class...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remind me why we use ScaleContinuous for the non-position scales?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's so the data will get rescaled correctly, I think. ScaleContinuous uses scales::rescale() to map the data values to the right place on the scale. For position scales this isn't needed because the coordinate system handles it.

scale_class <- switch(
trans,
date = ScaleContinuousDate,
time = ScaleContinuousDatetime
)
} else {
scale_class <- ScaleContinuous
}

sc <- continuous_scale(
aesthetics, name, identity,
breaks = breaks, minor_breaks = minor_breaks, labels = labels,
guide = "none", trans = trans, ..., super = scale_class
aesthetics,
name,
palette = palette,
breaks = breaks,
minor_breaks = minor_breaks,
labels = labels,
guide = guide,
trans = trans,
...,
super = scale_class
)
sc$timezone <- timezone
sc
}


#' @rdname ggplot2-ggproto
#' @format NULL
#' @usage NULL
Expand Down
9 changes: 4 additions & 5 deletions R/scale-size.r
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,13 @@ scale_size_area <- function(..., max_size = 6) {
#' @rdname scale_size
#' @export
#' @usage NULL
scale_size_datetime <- function() {
scale_size_continuous(trans = "time")
scale_size_datetime <- function(..., range = c(1, 6)) {
datetime_scale("size", "time", palette = area_pal(range), ...)
}

#' @rdname scale_size
#' @export
#' @usage NULL
scale_size_date <- function() {
scale_size_continuous(trans = "date")
scale_size_date <- function(..., range = c(1, 6)) {
datetime_scale("size", "date", palette = area_pal(range), ...)
}

65 changes: 57 additions & 8 deletions R/zxx.r
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,39 @@ scale_colour_continuous <- scale_colour_gradient
#' @export
#' @rdname scale_gradient
#' @usage NULL
scale_colour_datetime <- function() {
scale_colour_continuous(trans = "time")
scale_colour_datetime <- function(...,
low = "#132B43",
high = "#56B1F7",
space = "Lab",
na.value = "grey50",
guide = "colourbar") {
datetime_scale(
"colour",
"time",
palette = seq_gradient_pal(low, high, space),
na.value = na.value,
guide = guide,
...
)
}

#' @export
#' @rdname scale_gradient
#' @usage NULL
scale_colour_date <- function() {
scale_colour_continuous(trans = "date")
scale_colour_date <- function(...,
low = "#132B43",
high = "#56B1F7",
space = "Lab",
na.value = "grey50",
guide = "colourbar") {
datetime_scale(
"colour",
"date",
palette = seq_gradient_pal(low, high, space),
na.value = na.value,
guide = guide,
...
)
}

#' @export
Expand All @@ -37,15 +61,40 @@ scale_fill_continuous <- scale_fill_gradient
#' @export
#' @rdname scale_gradient
#' @usage NULL
scale_fill_datetime <- function() {
scale_fill_continuous(trans = "time")
scale_fill_datetime <- function(...,
low = "#132B43",
high = "#56B1F7",
space = "Lab",
na.value = "grey50",
guide = "colourbar") {
datetime_scale(
"fill",
"time",
palette = seq_gradient_pal(low, high, space),
na.value = na.value,
guide = guide,
...
)

}

#' @export
#' @rdname scale_gradient
#' @usage NULL
scale_fill_date <- function() {
scale_fill_continuous(trans = "date")
scale_fill_date <- function(...,
low = "#132B43",
high = "#56B1F7",
space = "Lab",
na.value = "grey50",
guide = "colourbar") {
datetime_scale(
"fill",
"date",
palette = seq_gradient_pal(low, high, space),
na.value = na.value,
guide = guide,
...
)
}


Expand Down
2 changes: 2 additions & 0 deletions man/scale_alpha.Rd

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

Loading