@@ -53,12 +53,12 @@ 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
55
56
- expect_equal(p $ labels $ colour [1 ], " colour" )
57
- expect_true(attr(p $ labels $ colour , " fallback" ))
56
+ labels <- get_labs(p )
57
+ expect_equal(labels $ colour [1 ], " colour" )
58
+ expect_true(attr(labels $ colour , " fallback" ))
58
59
59
- p <- p + geom_smooth(aes(color = drv ))
60
-
61
- expect_equal(p $ labels $ colour , " drv" )
60
+ p <- p + geom_smooth(aes(color = drv ), method = " lm" , formula = y ~ x )
61
+ expect_equal(get_labs(p )$ colour , " drv" )
62
62
})
63
63
64
64
test_that(" alt text is returned" , {
@@ -97,24 +97,25 @@ test_that("position axis label hierarchy works as intended", {
97
97
geom_point(size = 5 )
98
98
99
99
p <- ggplot_build(p )
100
+ resolve_label <- function (x ) p $ layout $ resolve_label(x , p $ plot $ labels )
100
101
101
102
# In absence of explicit title, get title from mapping
102
103
expect_identical(
103
- p $ layout $ resolve_label(p $ layout $ panel_scales_x [[1 ]], p $ plot $ labels ),
104
+ resolve_label(p $ layout $ panel_scales_x [[1 ]]),
104
105
list (secondary = NULL , primary = " foo" )
105
106
)
106
107
expect_identical(
107
- p $ layout $ resolve_label(p $ layout $ panel_scales_y [[1 ]], p $ plot $ labels ),
108
+ resolve_label(p $ layout $ panel_scales_y [[1 ]]),
108
109
list (primary = " bar" , secondary = NULL )
109
110
)
110
111
111
112
# Scale name overrules mapping label
112
113
expect_identical(
113
- p $ layout $ resolve_label(scale_x_continuous(" Baz" ), p $ plot $ labels ),
114
+ resolve_label(scale_x_continuous(" Baz" )),
114
115
list (secondary = NULL , primary = " Baz" )
115
116
)
116
117
expect_identical(
117
- p $ layout $ resolve_label(scale_y_continuous(" Qux" ), p $ plot $ labels ),
118
+ resolve_label(scale_y_continuous(" Qux" )),
118
119
list (primary = " Qux" , secondary = NULL )
119
120
)
120
121
@@ -124,23 +125,23 @@ test_that("position axis label hierarchy works as intended", {
124
125
p $ plot $ layers
125
126
)
126
127
expect_identical(
127
- p $ layout $ resolve_label(scale_x_continuous(" Baz" ), p $ plot $ labels ),
128
+ resolve_label(scale_x_continuous(" Baz" )),
128
129
list (secondary = NULL , primary = " quuX" )
129
130
)
130
131
expect_identical(
131
- p $ layout $ resolve_label(scale_y_continuous(" Qux" ), p $ plot $ labels ),
132
+ resolve_label(scale_y_continuous(" Qux" )),
132
133
list (primary = " corgE" , secondary = NULL )
133
134
)
134
135
135
136
# Secondary axis names work
136
137
xsec <- scale_x_continuous(" Baz" , sec.axis = dup_axis(name = " grault" ))
137
138
expect_identical(
138
- p $ layout $ resolve_label(xsec , p $ plot $ labels ),
139
+ resolve_label(xsec ),
139
140
list (secondary = " grault" , primary = " quuX" )
140
141
)
141
142
ysec <- scale_y_continuous(" Qux" , sec.axis = dup_axis(name = " garply" ))
142
143
expect_identical(
143
- p $ layout $ resolve_label(ysec , p $ plot $ labels ),
144
+ resolve_label(ysec ),
144
145
list (primary = " corgE" , secondary = " garply" )
145
146
)
146
147
@@ -151,12 +152,12 @@ test_that("position axis label hierarchy works as intended", {
151
152
p $ plot $ layers
152
153
)
153
154
expect_identical(
154
- p $ layout $ resolve_label(xsec , p $ plot $ labels ),
155
+ resolve_label(xsec ),
155
156
list (secondary = " waldo" , primary = " quuX" )
156
157
)
157
158
ysec <- scale_y_continuous(" Qux" , sec.axis = dup_axis(name = " garply" ))
158
159
expect_identical(
159
- p $ layout $ resolve_label(ysec , p $ plot $ labels ),
160
+ resolve_label(ysec ),
160
161
list (primary = " corgE" , secondary = " fred" )
161
162
)
162
163
})
@@ -177,31 +178,20 @@ test_that("moving guide positions lets titles follow", {
177
178
),
178
179
p $ plot $ layers
179
180
)
180
- expect_identical(
181
- p $ layout $ resolve_label(p $ layout $ panel_scales_x [[1 ]], p $ plot $ labels ),
182
- list (secondary = NULL , primary = " baz" )
183
- )
184
- expect_identical(
185
- p $ layout $ resolve_label(p $ layout $ panel_scales_y [[1 ]], p $ plot $ labels ),
186
- list (primary = " qux" , secondary = NULL )
187
- )
181
+ labs <- get_labs(p )
182
+ expect <- list (x = " baz" , x.sec = NULL , y = " qux" , y.sec = NULL )
183
+ expect_identical(labs [names(expect )], expect )
188
184
189
- # Guides at secondary positions (changes order of primary/secondary)
185
+ # Guides at secondary positions
190
186
p $ layout $ setup_panel_guides(
191
187
guides_list(
192
188
list (x = guide_axis(" baz" , position = " top" ),
193
189
y = guide_axis(" qux" , position = " right" ))
194
190
),
195
191
p $ plot $ layers
196
192
)
197
- expect_identical(
198
- p $ layout $ resolve_label(p $ layout $ panel_scales_x [[1 ]], p $ plot $ labels ),
199
- list (primary = " baz" , secondary = NULL )
200
- )
201
- expect_identical(
202
- p $ layout $ resolve_label(p $ layout $ panel_scales_y [[1 ]], p $ plot $ labels ),
203
- list (secondary = NULL , primary = " qux" )
204
- )
193
+ labs <- get_labs(p )
194
+ expect_identical(labs [names(expect )], expect )
205
195
206
196
# Primary guides at secondary positions with
207
197
# secondary guides at primary positions
@@ -214,14 +204,9 @@ test_that("moving guide positions lets titles follow", {
214
204
),
215
205
p $ plot $ layers
216
206
)
217
- expect_identical(
218
- p $ layout $ resolve_label(p $ layout $ panel_scales_x [[1 ]], p $ plot $ labels ),
219
- list (primary = " baz" , secondary = " quux" )
220
- )
221
- expect_identical(
222
- p $ layout $ resolve_label(p $ layout $ panel_scales_y [[1 ]], p $ plot $ labels ),
223
- list (secondary = " corge" , primary = " qux" )
224
- )
207
+ labs <- get_labs(p )
208
+ expect [c(" x.sec" , " y.sec" )] <- list (" quux" , " corge" )
209
+ expect_identical(labs [names(expect )], expect )
225
210
})
226
211
227
212
# Visual tests ------------------------------------------------------------
0 commit comments