Skip to content

Commit 56c20f6

Browse files
authored
Making copies of trained position scales (#6286)
* add freeze method to viewscales * add test * inverse transform breaks * add news bullet * change `freeze()` to `make_fixed_copy()`
1 parent 1f3fc2e commit 56c20f6

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,8 @@
323323
(@teunbrand, #5093).
324324
* (internal) When `validate_subclass()` fails to find a class directly, it tries
325325
to retrieve the class via constructor functions (@teunbrand).
326+
* (internal) The ViewScale class has a `make_fixed_copy()` method to permit
327+
copying trained position scales (#3441).
326328

327329
# ggplot2 3.5.1
328330

R/scale-view.R

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,5 +150,31 @@ ViewScale <- ggproto("ViewScale", NULL,
150150
}
151151

152152
self$rescale(b)
153+
},
154+
make_fixed_copy = function(self) {
155+
breaks <- self$get_breaks()
156+
minor <- self$get_breaks_minor()
157+
transform <- self$scale$get_transformation()
158+
159+
if (self$scale$is_discrete()) {
160+
limits <- self$get_limits()
161+
} else {
162+
limits <- self$continuous_range
163+
}
164+
165+
if (!is.null(transform)) {
166+
breaks <- transform$inverse(breaks)
167+
minor <- transform$inverse(minor)
168+
}
169+
170+
ggproto(
171+
NULL, self$scale,
172+
breaks = breaks,
173+
minor_breaks = minor,
174+
limits = limits,
175+
expand = c(0, 0, 0, 0),
176+
continuous_limits = self$continuous_range,
177+
train = function (...) NULL
178+
)
153179
}
154180
)

tests/testthat/test-scales.R

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,32 @@ test_that("discrete scales work with NAs in arbitrary positions", {
748748

749749
})
750750

751+
test_that("ViewScales can make fixed copies", {
752+
753+
p1 <- ggplot(mpg, aes(drv, displ)) +
754+
geom_boxplot() +
755+
annotate("point", x = 5, y = 10) +
756+
scale_x_discrete(labels = c("four-wheel", "forward", "reverse"))
757+
758+
b1 <- ggplot_build(p1)$layout$panel_params[[1]]
759+
760+
# We build a second plot with the first plot's scales
761+
p2 <- ggplot(mpg, aes(drv, cyl)) +
762+
geom_violin() +
763+
annotate("point", x = 15, y = 100) +
764+
b1$x$make_fixed_copy() +
765+
b1$y$make_fixed_copy()
766+
b2 <- ggplot_build(p2)
767+
768+
# Breaks and labels should respect p1's limits
769+
x <- get_guide_data(b2, "x")
770+
expect_equal(x$x, 0.6:2.6 / diff(b1$x.range))
771+
expect_equal(x$.label, c("four-wheel", "forward", "reverse"))
772+
773+
y <- get_guide_data(b2, "y")
774+
expect_equal(y$y, rescale(seq(2.5, 10, by = 2.5), from = b1$y.range))
775+
})
776+
751777
test_that("discrete scales can map to 2D structures", {
752778

753779
p <- ggplot(mtcars, aes(disp, mpg, colour = factor(cyl))) +

0 commit comments

Comments
 (0)