Debian Codegraph @

R/scale_units.R

raw
1 #' Continuous scales for units data
2 #'
3 #' These are the default scales for the \code{units} class. These will usually
4 #' be added automatically. To override manually, use \code{scale_{type}_units}.
5 #'
6 #' @param ... arguments passed on to the corresponding continuous scale
7 #' (see the manual page for each \code{scale_{type}} for details).
8 #' @inheritParams ggplot2::scale_x_continuous
9 #' @param unit A unit specification to use for the guide. If given, the values
10 #' will be converted to this unit before plotting. An error will be thrown if
11 #' the specified unit is incompatible with the unit of the data.
12 #'
13 #' @name scale_units
14 #' @aliases NULL
15 #'
16 #' @examples
17 #' if (requireNamespace("ggplot2", quietly=TRUE)) {
18 #'
19 #' library(ggplot2)
20 #'
21 #' mtcars$consumption <- set_units(mtcars$mpg, mi / gallon)
22 #' mtcars$power <- set_units(mtcars$hp, hp)
23 #'
24 #' # Use units encoded into the data
25 #' ggplot(mtcars) +
26 #' geom_point(aes(power, consumption))
27 #'
28 #' # Convert units on the fly during plotting
29 #' ggplot(mtcars) +
30 #' geom_point(aes(power, consumption)) +
31 #' scale_x_units(unit = "W") +
32 #' scale_y_units(unit = "km/l")
33 #'
34 #' # Resolve units when transforming data
35 #' ggplot(mtcars) +
36 #' geom_point(aes(power, 1 / consumption))
37 #'
38 #' # Reverse the y axis
39 #' ggplot(mtcars) +
40 #' geom_point(aes(power, consumption)) +
41 #' scale_y_units(transform="reverse")
42 #'
43 #' }
44 NULL
45
46 #' @rdname scale_units
47 #' @export
48 scale_x_units <- function(..., sec.axis = ggplot2::waiver(), unit = NULL) {
49 sc <- make_scale_units(ggplot2::scale_x_continuous(...), unit)
50 set_sec_axis(sec.axis, sc)
51 }
52
53 #' @rdname scale_units
54 #' @export
55 scale_y_units <- function(..., sec.axis = ggplot2::waiver(), unit = NULL) {
56 sc <- make_scale_units(ggplot2::scale_y_continuous(...), unit)
57 set_sec_axis(sec.axis, sc)
58 }
59
60 #' @rdname scale_units
61 #' @export
62 scale_colour_units <- function(..., unit = NULL) {
63 make_scale_units(ggplot2::scale_colour_continuous(...), unit)
64 }
65
66 #' @rdname scale_units
67 #' @export
68 scale_color_units <- scale_colour_units
69
70 #' @rdname scale_units
71 #' @export
72 scale_fill_units <- function(..., unit = NULL) {
73 make_scale_units(ggplot2::scale_fill_continuous(...), unit)
74 }
75
76 #' @rdname scale_units
77 #' @export
78 scale_alpha_units <- function(..., unit = NULL) {
79 make_scale_units(ggplot2::scale_alpha(...), unit)
80 }
81
82 #' @rdname scale_units
83 #' @export
84 scale_size_units <- function(..., unit = NULL) {
85 make_scale_units(ggplot2::scale_size(...), unit)
86 }
87
88 #' @rdname scale_units
89 #' @export
90 scale_size_area_units <- function(..., unit = NULL) {
91 make_scale_units(ggplot2::scale_size_area(...), unit)
92 }
93
94 #' @rdname scale_units
95 #' @export
96 scale_radius_units <- function(..., unit = NULL) {
97 make_scale_units(ggplot2::scale_radius(...), unit)
98 }
99
100 #' @rdname scale_units
101 #' @export
102 scale_linewidth_units <- function(..., unit = NULL) {
103 make_scale_units(ggplot2::scale_linewidth(...), unit)
104 }
105
106 make_scale_units <- function(parent, unit) {
107 if (!requireNamespace("ggplot2", quietly=TRUE))
108 stop("package 'ggplot2' is required for this functionality", call.=FALSE)
109
110 ggplot2::ggproto(
111 paste0(class(parent)[1], "Units"),
112 parent,
113 units = as_units(unit),
114
115 set_convert = function(self, x) {
116 if (!inherits(x, "units"))
117 return(x)
118
119 if (is.null(self$units))
120 self$units <- units(x)
121 else units(x) <- as_units(1, self$units)
122 drop_units(x)
123 },
124
125 map = function(self, x, limits = self$get_limits()) {
126 x <- self$set_convert(x)
127 ggplot2::ggproto_parent(parent, self)$map(x, limits)
128 },
129
130 train = function(self, x) {
131 x <- self$set_convert(x)
132 ggplot2::ggproto_parent(parent, self)$train(x)
133 },
134
135 transform = function(self, x) {
136 if (!is.null(self$units))
137 units(x) <- as_units(1, self$units)
138
139 if (inherits(self$limits, "units")) {
140 units(self$limits) <- units(x)
141 self$limits <- drop_units(self$limits)
142 }
143
144 new_x <- ggplot2::ggproto_parent(parent, self)$transform(drop_units(x))
145 as_units(new_x, units(x))
146 },
147
148 make_title = function(self, guide_title, scale_title, label_title) {
149 if (missing(label_title)) { # ggplot2 <= 3.5.1
150 title <- guide_title
151 if (!is.null(title))
152 title <- make_unit_label(title, as_units(1, self$units))
153 return(title)
154 }
155
156 if (!is.null(label_title))
157 label_title <- make_unit_label(label_title, as_units(1, self$units))
158 ggplot2::ggproto_parent(parent, self)$make_title(
159 guide_title, scale_title, label_title)
160 }
161 )
162 }
163
164 set_sec_axis <- function(sec.axis, scale) {
165 if (!inherits(sec.axis, "waiver")) {
166 if (inherits(sec.axis, "formula"))
167 sec.axis <- ggplot2::sec_axis(sec.axis)
168 if (!inherits(sec.axis, "AxisSecondary"))
169 stop("Secondary axes must be specified using \"sec_axis()\"", call.=FALSE)
170 sec.axis$units <- scale$units
171 scale$secondary.axis <- sec.axis
172 }
173 scale
174 }
175
176 # registered in .onLoad()
177 scale_type.units <- function(x) {
178 if (!"units" %in% .packages())
179 stop("Variable of class 'units' found, but 'units' package is not attached.\n",
180 " Please, attach it using 'library(units)' to properly show scales with units.")
181 c("units", "continuous")
182 }
183
184 utils::globalVariables("caller_env")
185
186 # registered in .onLoad()
187 limits.units <- function(lims, var, call = caller_env()) {
188 if (length(lims) != 2)
189 stop("`", var, "` must be a two-element vector", call.=FALSE)
190
191 trans <- if (!any(is.na(lims)) && lims[1] > lims[2])
192 "reverse" else "identity"
193 name <- paste0("scale_", var, "_units")
194 sc <- match.fun(name)(limits=lims, transform=trans)
195 sc$call <- if (!is.null(call)) call else str2lang(paste0(name, "()"))
196 sc
197 }