Skip to content

Commit 000a939

Browse files
authored
Rename trans arguments to transform (#5566)
* switch `trans` -> `transform` in constructors * swap `trans` -> `transform` in secondary axes * migrate `trans` -> `transform` in scale functions * propagate `trans` -> `transform` in other functions * adjust test verbiage * reoxygenate * add news bullet * document sec axis * don't namespace deprecated * rename trans field * fix sec axis bug * fallback mechanism * deprecation message * transformer -> transformation * rename `AxisSecondary$trans` slot * Implement `get_transformation()` method * Use `get_transformation()` * Document `get_transformation()` method
1 parent 8e1c085 commit 000a939

37 files changed

+328
-208
lines changed

NEWS.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
# ggplot2 (development version)
22

3+
* The `trans` argument in scales and secondary axes has been renamed to
4+
`transform`. The `trans` argument itself is deprecated. To access the
5+
transformation from the scale, a new `get_transformation()` method is
6+
added to Scale-classes (#5558).
7+
38
* `guide_*()` functions get a new `theme` argument to style individual guides.
49
The `theme()` function has gained additional arguments for styling guides:
510
`legend.key.spacing{.x/.y}`, `legend.frame`, `legend.axis.line`,
611
`legend.ticks`, `legend.ticks.length`, `legend.text.position` and
712
`legend.title.position`. Previous style arguments in the `guide_*()` functions
813
have been soft-deprecated.
9-
14+
1015
* When legend titles are larger than the legend, title justification extends
1116
to the placement of keys and labels (#1903).
1217

R/axis-secondary.R

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
#' secondary axis, positioned opposite of the primary axis. All secondary
55
#' axes must be based on a one-to-one transformation of the primary axes.
66
#'
7-
#' @param trans A formula or function of transformation
7+
#' @param transform A formula or function of transformation
8+
#'
9+
#' @param trans `r lifecycle::badge("deprecated")`
810
#'
911
#' @param name The name of the secondary axis
1012
#'
@@ -94,14 +96,20 @@
9496
#' )
9597
#'
9698
#' @export
97-
sec_axis <- function(trans = NULL, name = waiver(), breaks = waiver(), labels = waiver(),
98-
guide = waiver()) {
99+
sec_axis <- function(transform = NULL,
100+
name = waiver(), breaks = waiver(), labels = waiver(),
101+
guide = waiver(), trans = deprecated()) {
102+
if (lifecycle::is_present(trans)) {
103+
deprecate_soft0("3.5.0", "sec_axis(trans)", "sec_axis(transform)")
104+
transform <- trans
105+
}
106+
99107
# sec_axis() historically accepted two-sided formula, so be permissive.
100-
if (length(trans) > 2) trans <- trans[c(1,3)]
108+
if (length(transform) > 2) transform <- transform[c(1,3)]
101109

102-
trans <- as_function(trans)
110+
transform <- as_function(transform)
103111
ggproto(NULL, AxisSecondary,
104-
trans = trans,
112+
transform = transform,
105113
name = name,
106114
breaks = breaks,
107115
labels = labels,
@@ -111,8 +119,9 @@ sec_axis <- function(trans = NULL, name = waiver(), breaks = waiver(), labels =
111119
#' @rdname sec_axis
112120
#'
113121
#' @export
114-
dup_axis <- function(trans = ~., name = derive(), breaks = derive(), labels = derive(), guide = derive()) {
115-
sec_axis(trans, name, breaks, labels, guide)
122+
dup_axis <- function(transform = ~., trans = deprecated(),
123+
name = derive(), breaks = derive(), labels = derive(), guide = derive()) {
124+
sec_axis(transform, trans = trans, name, breaks, labels, guide)
116125
}
117126

118127
is.sec_axis <- function(x) {
@@ -144,7 +153,7 @@ is.derived <- function(x) {
144153
#' @usage NULL
145154
#' @export
146155
AxisSecondary <- ggproto("AxisSecondary", NULL,
147-
trans = NULL,
156+
transform = NULL,
148157
axis = NULL,
149158
name = waiver(),
150159
breaks = waiver(),
@@ -156,26 +165,27 @@ AxisSecondary <- ggproto("AxisSecondary", NULL,
156165
detail = 1000,
157166

158167
empty = function(self) {
159-
is.null(self$trans)
168+
is.null(self$transform %||% self$trans)
160169
},
161170

162171
# Inherit settings from the primary axis/scale
163172
init = function(self, scale) {
164173
if (self$empty()) {
165174
return()
166175
}
167-
if (!is.function(self$trans)) {
176+
transform <- self$transform %||% self$trans
177+
if (!is.function(transform)) {
168178
cli::cli_abort("Transformation for secondary axes must be a function.")
169179
}
170180
if (is.derived(self$name) && !is.waive(scale$name)) self$name <- scale$name
171181
if (is.derived(self$breaks)) self$breaks <- scale$breaks
172-
if (is.waive(self$breaks)) self$breaks <- scale$trans$breaks
182+
if (is.waive(self$breaks)) self$breaks <- scale$transformation$breaks
173183
if (is.derived(self$labels)) self$labels <- scale$labels
174184
if (is.derived(self$guide)) self$guide <- scale$guide
175185
},
176186

177187
transform_range = function(self, range) {
178-
self$trans(range)
188+
self$transform(range)
179189
},
180190

181191
mono_test = function(self, scale){
@@ -186,8 +196,9 @@ AxisSecondary <- ggproto("AxisSecondary", NULL,
186196
return()
187197
}
188198

199+
transformation <- scale$get_transformation()
189200
along_range <- seq(range[1], range[2], length.out = self$detail)
190-
old_range <- scale$trans$inverse(along_range)
201+
old_range <- transformation$inverse(along_range)
191202

192203
# Create mapping between primary and secondary range
193204
full_range <- self$transform_range(old_range)
@@ -204,8 +215,9 @@ AxisSecondary <- ggproto("AxisSecondary", NULL,
204215
self$mono_test(scale)
205216

206217
# Get scale's original range before transformation
218+
transformation <- scale$get_transformation()
207219
along_range <- seq(range[1], range[2], length.out = self$detail)
208-
old_range <- scale$trans$inverse(along_range)
220+
old_range <- transformation$inverse(along_range)
209221

210222
# Create mapping between primary and secondary range
211223
full_range <- self$transform_range(old_range)
@@ -225,8 +237,8 @@ AxisSecondary <- ggproto("AxisSecondary", NULL,
225237

226238
# patch for date and datetime scales just to maintain functionality
227239
# works only for linear secondary transforms that respect the time or date transform
228-
if (scale$trans$name %in% c("date", "time")) {
229-
temp_scale <- self$create_scale(new_range, trans = scale$trans)
240+
if (transformation$name %in% c("date", "time")) {
241+
temp_scale <- self$create_scale(new_range, transformation = transformation)
230242
range_info <- temp_scale$break_info()
231243
old_val_trans <- rescale(range_info$major, from = c(0, 1), to = range)
232244
old_val_minor_trans <- rescale(range_info$minor, from = c(0, 1), to = range)
@@ -237,7 +249,7 @@ AxisSecondary <- ggproto("AxisSecondary", NULL,
237249
# Map the break values back to their correct position on the primary scale
238250
if (!is.null(range_info$major_source)) {
239251
old_val <- stats::approx(full_range, old_range, range_info$major_source)$y
240-
old_val_trans <- scale$trans$transform(old_val)
252+
old_val_trans <- transformation$transform(old_val)
241253

242254
# rescale values from 0 to 1
243255
range_info$major[] <- round(
@@ -253,7 +265,7 @@ AxisSecondary <- ggproto("AxisSecondary", NULL,
253265

254266
if (!is.null(range_info$minor_source)) {
255267
old_val_minor <- stats::approx(full_range, old_range, range_info$minor_source)$y
256-
old_val_minor_trans <- scale$trans$transform(old_val_minor)
268+
old_val_minor_trans <- transformation$transform(old_val_minor)
257269

258270
range_info$minor[] <- round(
259271
rescale(
@@ -280,14 +292,14 @@ AxisSecondary <- ggproto("AxisSecondary", NULL,
280292
},
281293

282294
# Temporary scale for the purpose of calling break_info()
283-
create_scale = function(self, range, trans = transform_identity()) {
295+
create_scale = function(self, range, transformation = transform_identity()) {
284296
scale <- ggproto(NULL, ScaleContinuousPosition,
285297
name = self$name,
286298
breaks = self$breaks,
287299
labels = self$labels,
288300
limits = range,
289301
expand = c(0, 0),
290-
trans = trans
302+
transformation = transformation
291303
)
292304
scale$train(range)
293305
scale

R/coord-transform.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,8 @@ transform_value <- function(trans, value, range) {
190190
# TODO: can we merge this with view_scales_from_scale()?
191191
view_scales_from_scale_with_coord_trans <- function(scale, coord_limits, trans, expand = TRUE) {
192192
expansion <- default_expansion(scale, expand = expand)
193-
scale_trans <- scale$trans %||% transform_identity()
194-
coord_limits <- coord_limits %||% scale_trans$inverse(c(NA, NA))
193+
transformation <- scale$get_transformation() %||% transform_identity()
194+
coord_limits <- coord_limits %||% transformation$inverse(c(NA, NA))
195195
scale_limits <- scale$get_limits()
196196

197197
if (scale$is_discrete()) {
@@ -204,7 +204,7 @@ view_scales_from_scale_with_coord_trans <- function(scale, coord_limits, trans,
204204
)
205205
} else {
206206
# transform user-specified limits to scale transformed space
207-
coord_limits <- scale$trans$transform(coord_limits)
207+
coord_limits <- transformation$transform(coord_limits)
208208
continuous_ranges <- expand_limits_continuous_trans(
209209
scale_limits,
210210
expansion,

R/fortify-multcomp.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#' ggplot(mapping = aes(lhs, estimate)) +
2323
#' geom_linerange(aes(ymin = lwr, ymax = upr), data = CI) +
2424
#' geom_point(aes(size = p), data = summary(wht)) +
25-
#' scale_size(trans = "reverse")
25+
#' scale_size(transform = "reverse")
2626
#'
2727
#' cld <- cld(wht)
2828
#' fortify(cld)

R/guide-axis-logticks.R

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ NULL
5555
#' geom_density() +
5656
#' scale_x_continuous(
5757
#' breaks = c(-10^(4:0), 0, 10^(0:4)),
58-
#' trans = "pseudo_log"
58+
#' transform = "pseudo_log"
5959
#' )
6060
#'
6161
#' # The log ticks are mirrored when 0 is included
@@ -149,20 +149,20 @@ GuideAxisLogticks <- ggproto(
149149

150150
# Reconstruct a transformation if user has prescaled data
151151
if (!is.null(params$prescale_base)) {
152-
trans_name <- scale$scale$trans$name
152+
trans_name <- scale$scale$transformation$name
153153
if (trans_name != "identity") {
154154
cli::cli_warn(paste0(
155155
"The {.arg prescale_base} argument will override the scale's ",
156156
"{.field {trans_name}} transformation in log-tick positioning."
157157
))
158158
}
159-
trans <- transform_log(base = params$prescale_base)
159+
transformation <- transform_log(base = params$prescale_base)
160160
} else {
161-
trans <- scale$scale$trans
161+
transformation <- scale$get_transformation()
162162
}
163163

164164
# Reconstruct original range
165-
limits <- trans$inverse(scale$get_limits())
165+
limits <- transformation$inverse(scale$get_limits())
166166
has_negatives <- any(limits <= 0)
167167

168168
if (!has_negatives) {
@@ -190,7 +190,7 @@ GuideAxisLogticks <- ggproto(
190190
}
191191

192192
# Set ticks back into transformed space
193-
ticks <- trans$transform(c(tens, fives, ones))
193+
ticks <- transformation$transform(c(tens, fives, ones))
194194
nticks <- c(length(tens), length(fives), length(ones))
195195

196196
logkey <- data_frame0(

R/limits.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ limits.numeric <- function(lims, var, call = caller_env()) {
122122
trans <- "identity"
123123
}
124124

125-
make_scale("continuous", var, limits = lims, trans = trans, call = call)
125+
make_scale("continuous", var, limits = lims, transform = trans, call = call)
126126
}
127127

128128
make_scale <- function(type, var, ..., call = NULL) {

0 commit comments

Comments
 (0)