11context(" geom_jitter" )
22
33# Expect trace function
4- expect_traces <- function (gg , n_traces , name , seed ) {
4+ expect_traces <- function (gg , n_traces , name ) {
55 stopifnot(is.ggplot(gg ))
66 stopifnot(is.numeric(n_traces ))
7- save_outputs(gg , paste0(" coord_fixed-" , name ))
8- set.seed(seed )
7+ save_outputs(gg , paste0(" jitter-" , name ))
98 L <- gg2list(gg )
109 all_traces <- L $ data
1110 no_data <- sapply(all_traces , function (tr ) {
@@ -16,27 +15,13 @@ expect_traces <- function(gg, n_traces, name, seed) {
1615 list (traces = has_data , layout = L $ layout )
1716}
1817
19- # head(L$data[[1]]$x)
20-
21- # get data from mpg dataset
22- p <- ggplot(mpg , aes(displ , hwy ))
23-
24- # Test 1
25- # set up the data
2618set.seed(1001 )
27- p1 <- ggplot() + geom_jitter(data = mpg , aes(displ , hwy ), width = 1 )
28- head(ggplot_build2(p1 )$ data [[1 ]]$ x )
29- # test
19+ p <- ggplot(mpg , aes(cyl , hwy )) + geom_jitter()
20+
3021test_that(" geom_jitter is working" , {
31- info <- expect_traces(p1 , 1 , " geom_jitter " , 1001 )
22+ info <- expect_traces(p1 , 1 , " basic " )
3223 tr <- info $ traces [[1 ]]
33- la <- info $ layout
3424 expect_identical(tr $ type , " scatter" )
35- set.seed(1001 )
36- built <- ggplot_build2(p1 )
37- print(head(tr $ x )) # from gg2list
38- print(head(built $ data [[1 ]]$ x )) # from ggplot_build2
39- expect_identical(tr $ x , built $ data [[1 ]]$ x )
40- expect_identical(tr $ y , built $ data [[1 ]]$ y )
25+ # default jitter is 40% of the resolution of the data.
26+ expect_true(all(0 < abs(mpg $ cyl - tr $ x ) < 0.4 ))
4127})
42-
0 commit comments