11context(" bar" )
22
3- expect_traces <- function (gg , n.traces , name ){
3+ expect_traces <- function (gg , n.traces , name ) {
44 stopifnot(is.ggplot(gg ))
55 stopifnot(is.numeric(n.traces ))
66 save_outputs(gg , paste0(" bar-" , name ))
77 L <- gg2list(gg )
8- is.trace <- names(L ) == " "
9- all.traces <- L [is.trace ]
8+ all.traces <- L $ data
109 no.data <- sapply(all.traces , function (tr ) {
1110 is.null(tr [[" x" ]]) && is.null(tr [[" y" ]])
1211 })
1312 has.data <- all.traces [! no.data ]
1413 expect_equal(length(has.data ), n.traces )
15- list (traces = has.data , kwargs = L $ kwargs )
14+ list (traces = has.data , layout = L $ layout )
1615}
1716
1817researchers <-
@@ -29,7 +28,7 @@ test_that("position_dodge is translated to barmode=group", {
2928 trs <- info $ traces
3029 trace.names <- sapply(trs [1 : 2 ], " [[" , " name" )
3130 expect_true(all(c(" Math" , " Bio" ) %in% trace.names ))
32- expect_identical(info $ kwargs $ layout $ barmode , " group" )
31+ expect_identical(info $ layout $ barmode , " group" )
3332 # Check x values
3433 expect_identical(as.character(trs [[1 ]]$ x ), c(" Canada" , " Germany" ))
3534 expect_identical(as.character(trs [[2 ]]$ x ), c(" Canada" , " USA" ))
@@ -41,7 +40,7 @@ test_that("position_stack is translated to barmode=stack", {
4140 trs <- info $ traces
4241 trace.names <- sapply(trs [1 : 2 ], " [[" , " name" )
4342 expect_true(all(c(" Math" , " Bio" ) %in% trace.names ))
44- expect_identical(info $ kwargs $ layout $ barmode , " stack" )
43+ expect_identical(info $ layout $ barmode , " stack" )
4544})
4645
4746test_that(" position_identity is translated to barmode=stack" , {
@@ -50,7 +49,7 @@ test_that("position_identity is translated to barmode=stack", {
5049 trs <- info $ traces
5150 trace.names <- sapply(trs [1 : 2 ], " [[" , " name" )
5251 expect_true(all(c(" Math" , " Bio" ) %in% trace.names ))
53- expect_identical(info $ kwargs $ layout $ barmode , " stack" )
52+ expect_identical(info $ layout $ barmode , " stack" )
5453})
5554
5655test_that(" dates work well with bar charts" , {
@@ -60,7 +59,7 @@ test_that("dates work well with bar charts", {
6059 geom_bar(stat = " identity" )
6160 info <- expect_traces(gd , 2 , " dates" )
6261 trs <- info $ traces
63- expect_identical(info $ kwargs $ layout $ xaxis $ type , " date" )
62+ expect_identical(info $ layout $ xaxis $ type , " date" )
6463 # plotly likes time in milliseconds
6564 t <- as.numeric(unique(researchers $ month )) * 24 * 60 * 60 * 1000
6665 expect_identical(trs [[1 ]]$ x , t )
@@ -79,8 +78,8 @@ test_that("Very basic bar graph", {
7978 expect_null(tr $ marker $ line $ color )
8079 expect_null(tr $ marker $ line $ width )
8180 }
82- expect_null(info $ kwargs $ layout $ annotations )
83- expect_false(info $ kwargs $ layout $ showlegend )
81+ expect_null(info $ layout $ annotations )
82+ expect_false(info $ layout $ showlegend )
8483})
8584
8685test_that(" Map the time of day to different fill colors" , {
@@ -93,8 +92,8 @@ test_that("Map the time of day to different fill colors", {
9392 expect_null(tr $ marker $ line $ width )
9493 expect_true(tr $ showlegend )
9594 }
96- expect_match(info $ kwargs $ layout $ annotations [[1 ]]$ text , " time" )
97- expect_true(info $ kwargs $ layout $ showlegend )
95+ expect_match(info $ layout $ annotations [[1 ]]$ text , " time" )
96+ expect_true(info $ layout $ showlegend )
9897})
9998
10099test_that(" Add a black outline" , {
@@ -107,8 +106,8 @@ test_that("Add a black outline", {
107106 expect_equal(tr $ marker $ line $ width , 1 )
108107 expect_true(tr $ showlegend )
109108 }
110- expect_match(info $ kwargs $ layout $ annotations [[1 ]]$ text , " time" )
111- expect_true(info $ kwargs $ layout $ showlegend )
109+ expect_match(info $ layout $ annotations [[1 ]]$ text , " time" )
110+ expect_true(info $ layout $ showlegend )
112111})
113112
114113test_that(" guides(fill=FALSE) hides fill legend" , {
@@ -121,8 +120,8 @@ test_that("guides(fill=FALSE) hides fill legend", {
121120 expect_identical(tr $ marker $ line $ color , toRGB(" black" ))
122121 expect_equal(tr $ marker $ line $ width , 1 )
123122 }
124- expect_null(info $ kwargs $ layout $ annotations )
125- expect_false(info $ kwargs $ layout $ showlegend )
123+ expect_null(info $ layout $ annotations )
124+ expect_false(info $ layout $ showlegend )
126125})
127126
128127test_that(' guides(fill="none") hides fill legend' , {
@@ -135,8 +134,8 @@ test_that('guides(fill="none") hides fill legend', {
135134 expect_identical(tr $ marker $ line $ color , toRGB(" black" ))
136135 expect_equal(tr $ marker $ line $ width , 1 )
137136 }
138- expect_null(info $ kwargs $ layout $ annotations )
139- expect_false(info $ kwargs $ layout $ showlegend )
137+ expect_null(info $ layout $ annotations )
138+ expect_false(info $ layout $ showlegend )
140139})
141140
142141test_that(' guides(colour="none") does not affect fill legend' , {
@@ -150,8 +149,8 @@ test_that('guides(colour="none") does not affect fill legend', {
150149 expect_equal(tr $ marker $ line $ width , 1 )
151150 expect_true(tr $ showlegend )
152151 }
153- expect_match(info $ kwargs $ layout $ annotations [[1 ]]$ text , " time" )
154- expect_true(info $ kwargs $ layout $ showlegend )
152+ expect_match(info $ layout $ annotations [[1 ]]$ text , " time" )
153+ expect_true(info $ layout $ showlegend )
155154})
156155
157156test_that(" guides(fill=FALSE) does not affect colour legend" , {
@@ -165,16 +164,16 @@ test_that("guides(fill=FALSE) does not affect colour legend", {
165164 expect_equal(tr $ marker $ line $ width , 1 )
166165 expect_true(tr $ showlegend )
167166 }
168- expect_match(info $ kwargs $ layout $ annotations [[1 ]]$ text , " time" )
169- expect_true(info $ kwargs $ layout $ showlegend )
167+ expect_match(info $ layout $ annotations [[1 ]]$ text , " time" )
168+ expect_true(info $ layout $ showlegend )
170169})
171170
172171
173172base <- ggplot(mtcars , aes(factor (vs ), fill = factor (cyl )))
174173
175174test_that(" geom_bar() stacks counts" , {
176175 info <- expect_traces(base + geom_bar(), 3 , " position-stack" )
177- expect_identical(info $ kwargs $ layout $ barmode , " stack" )
176+ expect_identical(info $ layout $ barmode , " stack" )
178177 trs <- info $ traces
179178 # sum of y values for each trace
180179 test <- as.numeric(sort(sapply(trs , function (x ) sum(x $ y ))))
@@ -184,7 +183,7 @@ test_that("geom_bar() stacks counts", {
184183
185184test_that(" geom_bar(position = 'fill') stacks proportions" , {
186185 info <- expect_traces(base + geom_bar(position = " fill" ), 3 , " position-fill" )
187- expect_identical(info $ kwargs $ layout $ barmode , " stack" )
186+ expect_identical(info $ layout $ barmode , " stack" )
188187 trs <- info $ traces
189188 # sum of y-values *conditioned* on a x-value
190189 prop <- sum(sapply(sapply(trs , " [[" , " y" ), " [" , 1 ))
0 commit comments