@@ -25,3 +25,96 @@ test_that("position_nudgestack draws correctly", {
2525 stock_marked
2626 )
2727})
28+
29+
30+ test_that(" nudging works in both dimensions simultaneously" , {
31+ # individual nudge value for continuous data
32+ set.seed(111 )
33+
34+ df <- data_frame(x = 1 : 3 )
35+
36+ p <- ggplot(df , aes(x , x , xmax = x , xmin = x , ymax = x , ymin = x )) +
37+ geom_col(position = position_nudgestack(x = 0.5 , y = 2 ))
38+
39+ data <- layer_data(p )
40+
41+ expect_equal(data $ x , 1.5 : 3.5 )
42+ expect_equal(data $ xmin , 1.05 : 3.05 )
43+ expect_equal(data $ xmax , 1.95 : 3.95 )
44+ expect_equal(data $ y , 3 : 5 )
45+ expect_equal(data $ ymin , c(2 ,2 ,2 ))
46+ expect_equal(data $ ymax , 3 : 5 )
47+
48+ })
49+
50+ test_that(" nudging works for discrete values correctly" , {
51+ set.seed(111 )
52+
53+ # x nudge value for discrete data
54+ series <- data.frame (
55+ time = factor (c(rep(1 , 4 ),rep(2 , 4 ), rep(3 , 4 ), rep(4 , 4 ))),
56+ type = rep(c(' a' , ' b' , ' c' , ' d' ), 4 ),
57+ value = rpois(16 , 10 )
58+ )
59+
60+ p <- ggplot(series , aes(time , value , group = type )) +
61+ geom_line(aes(colour = type ), position = position_nudgestack(x = 0.5 )) +
62+ geom_point(aes(colour = type ), position = position_nudgestack(x = 0.5 ))
63+
64+ data <- layer_data(p )
65+
66+ expect_equal(data $ x , c(rep(1.5 , 4 ),rep(2.5 , 4 ), rep(3.5 , 4 ), rep(4.5 , 4 )))
67+ expect_equal(data $ xmin , c(rep(1.5 , 4 ),rep(2.5 , 4 ), rep(3.5 , 4 ), rep(4.5 , 4 )))
68+ expect_equal(data $ xmax , c(rep(1.5 , 4 ),rep(2.5 , 4 ), rep(3.5 , 4 ), rep(4.5 , 4 )))
69+
70+ })
71+
72+
73+ test_that(" data is sorted prior to stacking" , {
74+ df <- data_frame(
75+ x = rep(c(1 : 10 ), 3 ),
76+ var = rep(c(" a" , " b" , " c" ), 10 ),
77+ y = round(runif(30 , 1 , 5 ))
78+ )
79+ p <- ggplot(df , aes(x = x , y = y , fill = var )) +
80+ geom_col(position = position_nudgestack(x = 0.5 ))
81+
82+ dat <- layer_data(p )
83+ expect_true(all(dat $ group == 3 : 1 ))
84+ })
85+
86+ test_that(" negative and positive values are handled separately" , {
87+ df <- data_frame(
88+ x = c(1 ,1 ,1 ,2 ,2 ),
89+ g = c(1 ,2 ,3 ,1 ,2 ),
90+ y = c(1 ,- 1 ,1 ,2 ,- 3 )
91+ )
92+ p <- ggplot(df , aes(x , y , fill = factor (g ))) +
93+ geom_col(position = position_nudgestack(x = 0.5 ))
94+ dat <- layer_data(p )
95+
96+ expect_equal(dat $ ymin [dat $ x == 1.5 ], c(0 , - 1 , 1 ))
97+ expect_equal(dat $ ymax [dat $ x == 1.5 ], c(1 , 0 , 2 ))
98+
99+ expect_equal(dat $ ymin [dat $ x == 2.5 ], c(0 , - 3 ))
100+ expect_equal(dat $ ymax [dat $ x == 2.5 ], c(2 , 0 ))
101+ })
102+
103+ test_that(" can request reverse stacking" , {
104+ df <- data_frame(
105+ y = c(- 2 , 2 , - 1 , 1 ),
106+ g = c(" a" , " a" , " b" , " b" )
107+ )
108+ p <- ggplot(df , aes(1 , y , fill = g )) +
109+ geom_col(position = position_nudgestack(x = 0.5 , reverse = TRUE ))
110+ dat <- layer_data(p )
111+ expect_equal(dat $ ymin , c(- 2 , 0 , - 3 , 2 ))
112+ })
113+
114+ test_that(" position_nudgestack() can stack correctly when ymax is NA" , {
115+ df <- data_frame(x = c(1 , 1 ), y = c(1 , 1 ))
116+ p <- ggplot(df , aes(x , y , ymax = NA_real_ )) +
117+ geom_point(position = position_nudgestack(x = 0.5 ))
118+ expect_equal(layer_data(p )$ y , c(1 , 2 ))
119+ })
120+
0 commit comments