@@ -52,24 +52,22 @@ test_that("setting guide labels works", {
52
52
test_that(" Labels from default stat mapping are overwritten by default labels" , {
53
53
p <- ggplot(mpg , aes(displ , hwy )) +
54
54
geom_density2d()
55
- labels <- ggplot_build(p )$ plot $ labels
56
55
56
+ labels <- get_labs(p )
57
57
expect_equal(labels $ colour [1 ], " colour" )
58
58
expect_true(attr(labels $ colour , " fallback" ))
59
59
60
60
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" )
64
62
})
65
63
66
64
test_that(" Labels can be extracted from attributes" , {
67
65
df <- mtcars
68
66
attr(df $ mpg , " label" ) <- " Miles per gallon"
69
67
70
68
p <- ggplot(df , aes(mpg , disp )) + geom_point()
71
- labels <- ggplot_build(p )$ plot $ labels
72
69
70
+ labels <- get_labs(p )
73
71
expect_equal(labels $ x , " Miles per gallon" )
74
72
expect_equal(labels $ y , " disp" )
75
73
})
@@ -79,14 +77,10 @@ test_that("Labels from static aesthetics are ignored (#6003)", {
79
77
df <- data.frame (x = 1 , y = 1 , f = 1 )
80
78
81
79
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" )
85
81
86
82
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 )
90
84
})
91
85
92
86
test_that(" alt text is returned" , {
@@ -140,24 +134,25 @@ test_that("position axis label hierarchy works as intended", {
140
134
geom_point(size = 5 )
141
135
142
136
p <- ggplot_build(p )
137
+ resolve_label <- function (x ) p $ layout $ resolve_label(x , p $ plot $ labels )
143
138
144
139
# In absence of explicit title, get title from mapping
145
140
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 ]]),
147
142
list (secondary = NULL , primary = " foo" )
148
143
)
149
144
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 ]]),
151
146
list (primary = " bar" , secondary = NULL )
152
147
)
153
148
154
149
# Scale name overrules mapping label
155
150
expect_identical(
156
- p $ layout $ resolve_label(scale_x_continuous(" Baz" ), p $ plot $ labels ),
151
+ resolve_label(scale_x_continuous(" Baz" )),
157
152
list (secondary = NULL , primary = " Baz" )
158
153
)
159
154
expect_identical(
160
- p $ layout $ resolve_label(scale_y_continuous(" Qux" ), p $ plot $ labels ),
155
+ resolve_label(scale_y_continuous(" Qux" )),
161
156
list (primary = " Qux" , secondary = NULL )
162
157
)
163
158
@@ -167,23 +162,23 @@ test_that("position axis label hierarchy works as intended", {
167
162
p $ plot $ layers
168
163
)
169
164
expect_identical(
170
- p $ layout $ resolve_label(scale_x_continuous(" Baz" ), p $ plot $ labels ),
165
+ resolve_label(scale_x_continuous(" Baz" )),
171
166
list (secondary = NULL , primary = " quuX" )
172
167
)
173
168
expect_identical(
174
- p $ layout $ resolve_label(scale_y_continuous(" Qux" ), p $ plot $ labels ),
169
+ resolve_label(scale_y_continuous(" Qux" )),
175
170
list (primary = " corgE" , secondary = NULL )
176
171
)
177
172
178
173
# Secondary axis names work
179
174
xsec <- scale_x_continuous(" Baz" , sec.axis = dup_axis(name = " grault" ))
180
175
expect_identical(
181
- p $ layout $ resolve_label(xsec , p $ plot $ labels ),
176
+ resolve_label(xsec ),
182
177
list (secondary = " grault" , primary = " quuX" )
183
178
)
184
179
ysec <- scale_y_continuous(" Qux" , sec.axis = dup_axis(name = " garply" ))
185
180
expect_identical(
186
- p $ layout $ resolve_label(ysec , p $ plot $ labels ),
181
+ resolve_label(ysec ),
187
182
list (primary = " corgE" , secondary = " garply" )
188
183
)
189
184
@@ -194,12 +189,12 @@ test_that("position axis label hierarchy works as intended", {
194
189
p $ plot $ layers
195
190
)
196
191
expect_identical(
197
- p $ layout $ resolve_label(xsec , p $ plot $ labels ),
192
+ resolve_label(xsec ),
198
193
list (secondary = " waldo" , primary = " quuX" )
199
194
)
200
195
ysec <- scale_y_continuous(" Qux" , sec.axis = dup_axis(name = " garply" ))
201
196
expect_identical(
202
- p $ layout $ resolve_label(ysec , p $ plot $ labels ),
197
+ resolve_label(ysec ),
203
198
list (primary = " corgE" , secondary = " fred" )
204
199
)
205
200
})
@@ -220,31 +215,20 @@ test_that("moving guide positions lets titles follow", {
220
215
),
221
216
p $ plot $ layers
222
217
)
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 )
231
221
232
- # Guides at secondary positions (changes order of primary/secondary)
222
+ # Guides at secondary positions
233
223
p $ layout $ setup_panel_guides(
234
224
guides_list(
235
225
list (x = guide_axis(" baz" , position = " top" ),
236
226
y = guide_axis(" qux" , position = " right" ))
237
227
),
238
228
p $ plot $ layers
239
229
)
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 )
248
232
249
233
# Primary guides at secondary positions with
250
234
# secondary guides at primary positions
@@ -257,14 +241,9 @@ test_that("moving guide positions lets titles follow", {
257
241
),
258
242
p $ plot $ layers
259
243
)
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 )
268
247
})
269
248
270
249
# Visual tests ------------------------------------------------------------
0 commit comments