@@ -782,3 +782,30 @@ test_that("discrete scales work with NAs in arbitrary positions", {
782782 expect_equal(test , output )
783783
784784})
785+
786+ test_that(" ViewScales can be frozen" , {
787+
788+ p1 <- ggplot(mpg , aes(drv , displ )) +
789+ geom_boxplot() +
790+ annotate(" point" , x = 5 , y = 10 ) +
791+ scale_x_discrete(labels = c(" four-wheel" , " forward" , " reverse" ))
792+
793+ b1 <- ggplot_build(p1 )$ layout $ panel_params [[1 ]]
794+
795+ # We build a second plot with the first plot's scales
796+ p2 <- ggplot(mpg , aes(drv , cyl )) +
797+ geom_violin() +
798+ annotate(" point" , x = 15 , y = 100 ) +
799+ b1 $ x $ freeze() +
800+ b1 $ y $ freeze()
801+ b2 <- ggplot_build(p2 )
802+
803+ # Breaks and labels should respect p1's limits
804+ x <- get_guide_data(b2 , " x" )
805+ expect_equal(x $ x , 0.6 : 2.6 / diff(b1 $ x.range ))
806+ expect_equal(x $ .label , c(" four-wheel" , " forward" , " reverse" ))
807+
808+ y <- get_guide_data(b2 , " y" )
809+ expect_equal(y $ y , rescale(seq(2.5 , 10 , by = 2.5 ), from = b1 $ y.range ))
810+
811+ })
0 commit comments