@@ -755,3 +755,32 @@ test_that("discrete scales work with NAs in arbitrary positions", {
755755 expect_equal(test , output )
756756
757757})
758+
759+ test_that(" discrete scales can map to 2D structures" , {
760+
761+ p <- ggplot(mtcars , aes(disp , mpg , colour = factor (cyl ))) +
762+ geom_point()
763+
764+ # Test it can map to a vctrs rcrd class
765+ rcrd <- new_rcrd(list (a = LETTERS [1 : 3 ], b = 3 : 1 ))
766+
767+ ld <- layer_data(p + scale_colour_manual(values = rcrd , na.value = NA ))
768+ expect_s3_class(ld $ colour , " vctrs_rcrd" )
769+ expect_length(ld $ colour , nrow(mtcars ))
770+
771+ # Test it can map to data.frames
772+ df <- data_frame0(a = LETTERS [1 : 3 ], b = 3 : 1 )
773+ my_pal <- function (n ) vec_slice(df , seq_len(n ))
774+
775+ ld <- layer_data(p + discrete_scale(" colour" , palette = my_pal ))
776+ expect_s3_class(ld $ colour , " data.frame" )
777+ expect_equal(dim(ld $ colour ), c(nrow(mtcars ), ncol(df )))
778+
779+ # Test it can map to matrices
780+ mtx <- cbind(a = LETTERS [1 : 3 ], b = LETTERS [4 : 6 ])
781+ my_pal <- function (n ) vec_slice(mtx , seq_len(n ))
782+
783+ ld <- layer_data(p + discrete_scale(" colour" , palette = my_pal ))
784+ expect_true(is.matrix(ld $ colour ))
785+ expect_equal(dim(ld $ colour ), c(nrow(mtcars ), ncol(df )))
786+ })
0 commit comments