Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename trans arguments to transform #5566

Merged
merged 25 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
e4b065b
switch `trans` -> `transform` in constructors
teunbrand Dec 4, 2023
ddd8084
swap `trans` -> `transform` in secondary axes
teunbrand Dec 4, 2023
4cb5c61
migrate `trans` -> `transform` in scale functions
teunbrand Dec 4, 2023
84b54c8
propagate `trans` -> `transform` in other functions
teunbrand Dec 4, 2023
190ba17
adjust test verbiage
teunbrand Dec 4, 2023
43606d3
reoxygenate
teunbrand Dec 4, 2023
986373b
add news bullet
teunbrand Dec 4, 2023
b2f5ecc
document sec axis
teunbrand Dec 4, 2023
a121505
don't namespace deprecated
teunbrand Dec 5, 2023
2278ca4
Merge branch 'main' into trans_to_transform
teunbrand Dec 5, 2023
a5ad41c
Merge branch 'main' into trans_to_transform
teunbrand Dec 5, 2023
d34cfde
rename trans field
teunbrand Dec 6, 2023
7bbe90c
Merge branch 'trans_to_transform' of https://github.com/teunbrand/ggp…
teunbrand Dec 6, 2023
1a2335d
Merge branch 'main' into trans_to_transform
teunbrand Dec 6, 2023
398158a
fix sec axis bug
teunbrand Dec 6, 2023
69656fd
resolve merge conflict
teunbrand Dec 11, 2023
2e31c0e
fallback mechanism
teunbrand Dec 11, 2023
7940355
deprecation message
teunbrand Dec 11, 2023
3d55f85
transformer -> transformation
teunbrand Dec 11, 2023
a98974e
rename `AxisSecondary$trans` slot
teunbrand Dec 11, 2023
9842121
Implement `get_transformation()` method
teunbrand Dec 14, 2023
adbdb40
Use `get_transformation()`
teunbrand Dec 14, 2023
0b47ab4
Merge branch 'main' into trans_to_transform
teunbrand Dec 14, 2023
8cf9cd1
Document `get_transformation()` method
teunbrand Dec 14, 2023
f3901c8
Merge branch 'trans_to_transform' of https://github.com/teunbrand/ggp…
teunbrand Dec 14, 2023
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
7 changes: 6 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
# ggplot2 (development version)

* The `trans` argument in scales and secondary axes has been renamed to
`transform`. The `trans` argument itself is deprecated. To access the
transformation from the scale, a new `get_transformation()` method is
added to Scale-classes (#5558).

* `guide_*()` functions get a new `theme` argument to style individual guides.
The `theme()` function has gained additional arguments for styling guides:
`legend.key.spacing{.x/.y}`, `legend.frame`, `legend.axis.line`,
`legend.ticks`, `legend.ticks.length`, `legend.text.position` and
`legend.title.position`. Previous style arguments in the `guide_*()` functions
have been soft-deprecated.

* When legend titles are larger than the legend, title justification extends
to the placement of keys and labels (#1903).

Expand Down
54 changes: 33 additions & 21 deletions R/axis-secondary.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
#' secondary axis, positioned opposite of the primary axis. All secondary
#' axes must be based on a one-to-one transformation of the primary axes.
#'
#' @param trans A formula or function of transformation
#' @param transform A formula or function of transformation
#'
#' @param trans `r lifecycle::badge("deprecated")`
#'
#' @param name The name of the secondary axis
#'
Expand Down Expand Up @@ -94,14 +96,20 @@
#' )
#'
#' @export
sec_axis <- function(trans = NULL, name = waiver(), breaks = waiver(), labels = waiver(),
guide = waiver()) {
sec_axis <- function(transform = NULL,
name = waiver(), breaks = waiver(), labels = waiver(),
guide = waiver(), trans = deprecated()) {
if (lifecycle::is_present(trans)) {
deprecate_soft0("3.5.0", "sec_axis(trans)", "sec_axis(transform)")
transform <- trans
}

# sec_axis() historically accepted two-sided formula, so be permissive.
if (length(trans) > 2) trans <- trans[c(1,3)]
if (length(transform) > 2) transform <- transform[c(1,3)]

trans <- as_function(trans)
transform <- as_function(transform)
ggproto(NULL, AxisSecondary,
trans = trans,
transform = transform,
name = name,
breaks = breaks,
labels = labels,
Expand All @@ -111,8 +119,9 @@ sec_axis <- function(trans = NULL, name = waiver(), breaks = waiver(), labels =
#' @rdname sec_axis
#'
#' @export
dup_axis <- function(trans = ~., name = derive(), breaks = derive(), labels = derive(), guide = derive()) {
sec_axis(trans, name, breaks, labels, guide)
dup_axis <- function(transform = ~., trans = deprecated(),
name = derive(), breaks = derive(), labels = derive(), guide = derive()) {
sec_axis(transform, trans = trans, name, breaks, labels, guide)
}

is.sec_axis <- function(x) {
Expand Down Expand Up @@ -144,7 +153,7 @@ is.derived <- function(x) {
#' @usage NULL
#' @export
AxisSecondary <- ggproto("AxisSecondary", NULL,
trans = NULL,
transform = NULL,
axis = NULL,
name = waiver(),
breaks = waiver(),
Expand All @@ -156,26 +165,27 @@ AxisSecondary <- ggproto("AxisSecondary", NULL,
detail = 1000,

empty = function(self) {
is.null(self$trans)
is.null(self$transform %||% self$trans)
},

# Inherit settings from the primary axis/scale
init = function(self, scale) {
if (self$empty()) {
return()
}
if (!is.function(self$trans)) {
transform <- self$transform %||% self$trans
if (!is.function(transform)) {
cli::cli_abort("Transformation for secondary axes must be a function.")
}
if (is.derived(self$name) && !is.waive(scale$name)) self$name <- scale$name
if (is.derived(self$breaks)) self$breaks <- scale$breaks
if (is.waive(self$breaks)) self$breaks <- scale$trans$breaks
if (is.waive(self$breaks)) self$breaks <- scale$transformation$breaks
if (is.derived(self$labels)) self$labels <- scale$labels
if (is.derived(self$guide)) self$guide <- scale$guide
},

transform_range = function(self, range) {
self$trans(range)
self$transform(range)
},

mono_test = function(self, scale){
Expand All @@ -186,8 +196,9 @@ AxisSecondary <- ggproto("AxisSecondary", NULL,
return()
}

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

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

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

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

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

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

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

range_info$minor[] <- round(
rescale(
Expand All @@ -280,14 +292,14 @@ AxisSecondary <- ggproto("AxisSecondary", NULL,
},

# Temporary scale for the purpose of calling break_info()
create_scale = function(self, range, trans = transform_identity()) {
create_scale = function(self, range, transformation = transform_identity()) {
scale <- ggproto(NULL, ScaleContinuousPosition,
name = self$name,
breaks = self$breaks,
labels = self$labels,
limits = range,
expand = c(0, 0),
trans = trans
transformation = transformation
)
scale$train(range)
scale
Expand Down
6 changes: 3 additions & 3 deletions R/coord-transform.R
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ transform_value <- function(trans, value, range) {
# TODO: can we merge this with view_scales_from_scale()?
view_scales_from_scale_with_coord_trans <- function(scale, coord_limits, trans, expand = TRUE) {
expansion <- default_expansion(scale, expand = expand)
scale_trans <- scale$trans %||% transform_identity()
coord_limits <- coord_limits %||% scale_trans$inverse(c(NA, NA))
transformation <- scale$get_transformation() %||% transform_identity()
coord_limits <- coord_limits %||% transformation$inverse(c(NA, NA))
scale_limits <- scale$get_limits()

if (scale$is_discrete()) {
Expand All @@ -204,7 +204,7 @@ view_scales_from_scale_with_coord_trans <- function(scale, coord_limits, trans,
)
} else {
# transform user-specified limits to scale transformed space
coord_limits <- scale$trans$transform(coord_limits)
coord_limits <- transformation$transform(coord_limits)
continuous_ranges <- expand_limits_continuous_trans(
scale_limits,
expansion,
Expand Down
2 changes: 1 addition & 1 deletion R/fortify-multcomp.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#' ggplot(mapping = aes(lhs, estimate)) +
#' geom_linerange(aes(ymin = lwr, ymax = upr), data = CI) +
#' geom_point(aes(size = p), data = summary(wht)) +
#' scale_size(trans = "reverse")
#' scale_size(transform = "reverse")
#'
#' cld <- cld(wht)
#' fortify(cld)
Expand Down
12 changes: 6 additions & 6 deletions R/guide-axis-logticks.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ NULL
#' geom_density() +
#' scale_x_continuous(
#' breaks = c(-10^(4:0), 0, 10^(0:4)),
#' trans = "pseudo_log"
#' transform = "pseudo_log"
#' )
#'
#' # The log ticks are mirrored when 0 is included
Expand Down Expand Up @@ -149,20 +149,20 @@ GuideAxisLogticks <- ggproto(

# Reconstruct a transformation if user has prescaled data
if (!is.null(params$prescale_base)) {
trans_name <- scale$scale$trans$name
trans_name <- scale$scale$transformation$name
if (trans_name != "identity") {
cli::cli_warn(paste0(
"The {.arg prescale_base} argument will override the scale's ",
"{.field {trans_name}} transformation in log-tick positioning."
))
}
trans <- transform_log(base = params$prescale_base)
transformation <- transform_log(base = params$prescale_base)
} else {
trans <- scale$scale$trans
transformation <- scale$get_transformation()
}

# Reconstruct original range
limits <- trans$inverse(scale$get_limits())
limits <- transformation$inverse(scale$get_limits())
has_negatives <- any(limits <= 0)

if (!has_negatives) {
Expand Down Expand Up @@ -190,7 +190,7 @@ GuideAxisLogticks <- ggproto(
}

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

logkey <- data_frame0(
Expand Down
2 changes: 1 addition & 1 deletion R/limits.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ limits.numeric <- function(lims, var, call = caller_env()) {
trans <- "identity"
}

make_scale("continuous", var, limits = lims, trans = trans, call = call)
make_scale("continuous", var, limits = lims, transform = trans, call = call)
}

make_scale <- function(type, var, ..., call = NULL) {
Expand Down
Loading
Loading