@@ -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+
751777test_that(" discrete scales can map to 2D structures" , {
752778
753779 p <- ggplot(mtcars , aes(disp , mpg , colour = factor (cyl ))) +
0 commit comments