| 1 |
#' Scale that supports formatted numbers |
| 2 |
#' |
| 3 |
#' @description |
| 4 |
#' `r lifecycle::badge("experimental")` |
| 5 |
#' |
| 6 |
#' This scale is used by default in ggplot2 with columns created with [num()]. |
| 7 |
#' |
| 8 |
#' @inheritDotParams ggplot2::continuous_scale -scale_name |
| 9 |
#' @param guide,position Passed on to [ggplot2::continuous_scale()] |
| 10 |
#' @param rescaler,super Must remain `NULL`. |
| 11 |
#' |
| 12 |
#' @keywords internal |
| 13 |
#' @export |
| 14 |
scale_x_num <- function(..., position = "bottom", guide = ggplot2::waiver(), |
| 15 |
rescaler = NULL, super = NULL) { |
| 16 |
stopifnot(is.null(rescaler)) |
| 17 |
stopifnot(is.null(super)) |
| 18 |
check_installed("ggplot2") |
| 19 |
ggplot2::continuous_scale( |
| 20 |
aesthetics = c( |
| 21 |
"x", "xmin", "xmax", "xend", "xintercept", "xmin_final", "xmax_final", |
| 22 |
"xlower", "xmiddle", "xupper" |
| 23 |
), |
| 24 |
palette = "position_c", |
| 25 |
..., |
| 26 |
guide = guide, |
| 27 |
position = position, |
| 28 |
rescaler = scales::rescale, |
| 29 |
super = MakeScaleContinuousPositionNum() |
| 30 |
) |
| 31 |
} |
| 32 |
|
| 33 |
#' @rdname scale_x_num |
| 34 |
#' @export |
| 35 |
scale_y_num <- function(..., guide = ggplot2::waiver(), |
| 36 |
rescaler = NULL, super = NULL) { |
| 37 |
stopifnot(is.null(rescaler)) |
| 38 |
stopifnot(is.null(super)) |
| 39 |
check_installed("ggplot2") |
| 40 |
ggplot2::continuous_scale( |
| 41 |
aesthetics = c( |
| 42 |
"y", "ymin", "ymax", "yend", "yintercept", "ymin_final", "ymax_final", |
| 43 |
"lower", "middle", "upper" |
| 44 |
), |
| 45 |
palette = "position_c", |
| 46 |
..., |
| 47 |
guide = guide, |
| 48 |
rescaler = scales::rescale, |
| 49 |
super = MakeScaleContinuousPositionNum() |
| 50 |
) |
| 51 |
} |
| 52 |
|
| 53 |
MakeScaleContinuousPositionNum <- function() { |
| 54 |
ggplot2::ggproto("ScaleContinuousPositionNum", ggplot2::ScaleContinuousPosition, |
| 55 |
ptype = NULL, |
| 56 |
train = function(self, x) { |
| 57 |
self$ptype <- vec_ptype2(x, self$ptype) |
| 58 |
ggplot2::ggproto_parent(ggplot2::ScaleContinuousPosition, self)$train(x) |
| 59 |
}, |
| 60 |
get_breaks = function(self, limits = self$get_limits()) { |
| 61 |
out <- ggplot2::ggproto_parent(ggplot2::ScaleContinuousPosition, self)$get_breaks(limits) |
| 62 |
vec_cast(out, self$ptype) |
| 63 |
}, |
| 64 |
get_labels = function(self, breaks = self$get_breaks()) { |
| 65 |
out <- ggplot2::ggproto_parent(ggplot2::ScaleContinuousPosition, self)$get_labels(breaks) |
| 66 |
ansi_strip(out) |
| 67 |
}, |
| 68 |
make_title = function(self, title, ...) { |
| 69 |
out <- ggplot2::ggproto_parent(ggplot2::ScaleContinuousPosition, self)$make_title(title, ...) |
| 70 |
pillar_attr <- attr(self$ptype, "pillar", exact = TRUE) |
| 71 |
if (!is.null(pillar_attr$fixed_exponent)) { |
| 72 |
shaft <- pillar_shaft_number_attr(numeric(), pillar_attr) |
| 73 |
if (pillar_attr$notation == "si") { |
| 74 |
type <- attr(shaft, "type") |
| 75 |
if (!is.null(type)) { |
| 76 |
out <- paste0(out, " ", ansi_strip(type[[1]])) |
| 77 |
} |
| 78 |
} else { |
| 79 |
# paste0() doesn't work here, paste() works like paste0() |
| 80 |
out <- quo_squash(quo(paste(!!out, " [", 10^!!shaft$sci$unit, "]"))) |
| 81 |
} |
| 82 |
} else { |
| 83 |
label <- pillar_attr$label |
| 84 |
if (!is.null(label)) { |
| 85 |
out <- paste0(out, " [", label, "]") |
| 86 |
} |
| 87 |
} |
| 88 |
out |
| 89 |
} |
| 90 |
) |
| 91 |
} |
| 92 |
|
| 93 |
# registered in .onLoad() |
| 94 |
scale_type.pillar_num <- function(x) c("num", "continuous") |
| 95 |
|
| 96 |
# registered in .onLoad() |
| 97 |
rescale.pillar_num <- function(x, |
| 98 |
to = c(0, 1), |
| 99 |
from = range(x, na.rm = TRUE, finite = TRUE), |
| 100 |
...) { |
| 101 |
out <- scales::rescale(vec_data(x), to, from, ...) |
| 102 |
vec_cast(out, x) |
| 103 |
} |