@@ -52,24 +52,22 @@ test_that("setting guide labels works", {
5252test_that(" Labels from default stat mapping are overwritten by default labels" , {
5353 p <- ggplot(mpg , aes(displ , hwy )) +
5454 geom_density2d()
55- labels <- ggplot_build(p )$ plot $ labels
5655
56+ labels <- get_labs(p )
5757 expect_equal(labels $ colour [1 ], " colour" )
5858 expect_true(attr(labels $ colour , " fallback" ))
5959
6060 p <- p + geom_smooth(aes(color = drv ), method = " lm" , formula = y ~ x )
61- labels <- ggplot_build(p )$ plot $ labels
62-
63- expect_equal(labels $ colour , " drv" )
61+ expect_equal(get_labs(p )$ colour , " drv" )
6462})
6563
6664test_that(" Labels can be extracted from attributes" , {
6765 df <- mtcars
6866 attr(df $ mpg , " label" ) <- " Miles per gallon"
6967
7068 p <- ggplot(df , aes(mpg , disp )) + geom_point()
71- labels <- ggplot_build(p )$ plot $ labels
7269
70+ labels <- get_labs(p )
7371 expect_equal(labels $ x , " Miles per gallon" )
7472 expect_equal(labels $ y , " disp" )
7573})
@@ -79,14 +77,10 @@ test_that("Labels from static aesthetics are ignored (#6003)", {
7977 df <- data.frame (x = 1 , y = 1 , f = 1 )
8078
8179 p <- ggplot(df , aes(x , y , colour = f )) + geom_point()
82- labels <- ggplot_build(p )$ plot $ labels
83-
84- expect_equal(labels $ colour , " f" )
80+ expect_equal(get_labs(p )$ colour , " f" )
8581
8682 p <- ggplot(df , aes(x , y , colour = f )) + geom_point(colour = " blue" )
87- labels <- ggplot_build(p )$ plot $ labels
88-
89- expect_null(labels $ colour )
83+ expect_null(get_labs(p )$ colour )
9084})
9185
9286test_that(" alt text is returned" , {
@@ -140,24 +134,25 @@ test_that("position axis label hierarchy works as intended", {
140134 geom_point(size = 5 )
141135
142136 p <- ggplot_build(p )
137+ resolve_label <- function (x ) p $ layout $ resolve_label(x , p $ plot $ labels )
143138
144139 # In absence of explicit title, get title from mapping
145140 expect_identical(
146- p $ layout $ resolve_label(p $ layout $ panel_scales_x [[1 ]], p $ plot $ labels ),
141+ resolve_label(p $ layout $ panel_scales_x [[1 ]]),
147142 list (secondary = NULL , primary = " foo" )
148143 )
149144 expect_identical(
150- p $ layout $ resolve_label(p $ layout $ panel_scales_y [[1 ]], p $ plot $ labels ),
145+ resolve_label(p $ layout $ panel_scales_y [[1 ]]),
151146 list (primary = " bar" , secondary = NULL )
152147 )
153148
154149 # Scale name overrules mapping label
155150 expect_identical(
156- p $ layout $ resolve_label(scale_x_continuous(" Baz" ), p $ plot $ labels ),
151+ resolve_label(scale_x_continuous(" Baz" )),
157152 list (secondary = NULL , primary = " Baz" )
158153 )
159154 expect_identical(
160- p $ layout $ resolve_label(scale_y_continuous(" Qux" ), p $ plot $ labels ),
155+ resolve_label(scale_y_continuous(" Qux" )),
161156 list (primary = " Qux" , secondary = NULL )
162157 )
163158
@@ -167,23 +162,23 @@ test_that("position axis label hierarchy works as intended", {
167162 p $ plot $ layers
168163 )
169164 expect_identical(
170- p $ layout $ resolve_label(scale_x_continuous(" Baz" ), p $ plot $ labels ),
165+ resolve_label(scale_x_continuous(" Baz" )),
171166 list (secondary = NULL , primary = " quuX" )
172167 )
173168 expect_identical(
174- p $ layout $ resolve_label(scale_y_continuous(" Qux" ), p $ plot $ labels ),
169+ resolve_label(scale_y_continuous(" Qux" )),
175170 list (primary = " corgE" , secondary = NULL )
176171 )
177172
178173 # Secondary axis names work
179174 xsec <- scale_x_continuous(" Baz" , sec.axis = dup_axis(name = " grault" ))
180175 expect_identical(
181- p $ layout $ resolve_label(xsec , p $ plot $ labels ),
176+ resolve_label(xsec ),
182177 list (secondary = " grault" , primary = " quuX" )
183178 )
184179 ysec <- scale_y_continuous(" Qux" , sec.axis = dup_axis(name = " garply" ))
185180 expect_identical(
186- p $ layout $ resolve_label(ysec , p $ plot $ labels ),
181+ resolve_label(ysec ),
187182 list (primary = " corgE" , secondary = " garply" )
188183 )
189184
@@ -194,12 +189,12 @@ test_that("position axis label hierarchy works as intended", {
194189 p $ plot $ layers
195190 )
196191 expect_identical(
197- p $ layout $ resolve_label(xsec , p $ plot $ labels ),
192+ resolve_label(xsec ),
198193 list (secondary = " waldo" , primary = " quuX" )
199194 )
200195 ysec <- scale_y_continuous(" Qux" , sec.axis = dup_axis(name = " garply" ))
201196 expect_identical(
202- p $ layout $ resolve_label(ysec , p $ plot $ labels ),
197+ resolve_label(ysec ),
203198 list (primary = " corgE" , secondary = " fred" )
204199 )
205200})
@@ -220,31 +215,20 @@ test_that("moving guide positions lets titles follow", {
220215 ),
221216 p $ plot $ layers
222217 )
223- expect_identical(
224- p $ layout $ resolve_label(p $ layout $ panel_scales_x [[1 ]], p $ plot $ labels ),
225- list (secondary = NULL , primary = " baz" )
226- )
227- expect_identical(
228- p $ layout $ resolve_label(p $ layout $ panel_scales_y [[1 ]], p $ plot $ labels ),
229- list (primary = " qux" , secondary = NULL )
230- )
218+ labs <- get_labs(p )
219+ expect <- list (x = " baz" , x.sec = NULL , y = " qux" , y.sec = NULL )
220+ expect_identical(labs [names(expect )], expect )
231221
232- # Guides at secondary positions (changes order of primary/secondary)
222+ # Guides at secondary positions
233223 p $ layout $ setup_panel_guides(
234224 guides_list(
235225 list (x = guide_axis(" baz" , position = " top" ),
236226 y = guide_axis(" qux" , position = " right" ))
237227 ),
238228 p $ plot $ layers
239229 )
240- expect_identical(
241- p $ layout $ resolve_label(p $ layout $ panel_scales_x [[1 ]], p $ plot $ labels ),
242- list (primary = " baz" , secondary = NULL )
243- )
244- expect_identical(
245- p $ layout $ resolve_label(p $ layout $ panel_scales_y [[1 ]], p $ plot $ labels ),
246- list (secondary = NULL , primary = " qux" )
247- )
230+ labs <- get_labs(p )
231+ expect_identical(labs [names(expect )], expect )
248232
249233 # Primary guides at secondary positions with
250234 # secondary guides at primary positions
@@ -257,14 +241,9 @@ test_that("moving guide positions lets titles follow", {
257241 ),
258242 p $ plot $ layers
259243 )
260- expect_identical(
261- p $ layout $ resolve_label(p $ layout $ panel_scales_x [[1 ]], p $ plot $ labels ),
262- list (primary = " baz" , secondary = " quux" )
263- )
264- expect_identical(
265- p $ layout $ resolve_label(p $ layout $ panel_scales_y [[1 ]], p $ plot $ labels ),
266- list (secondary = " corge" , primary = " qux" )
267- )
244+ labs <- get_labs(p )
245+ expect [c(" x.sec" , " y.sec" )] <- list (" quux" , " corge" )
246+ expect_identical(labs [names(expect )], expect )
268247})
269248
270249# Visual tests ------------------------------------------------------------
0 commit comments