@@ -174,7 +174,19 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all",
174
174
175
175
# Compute aesthetics to produce data with generalised variable names
176
176
data <- by_layer(function (l , d ) l $ compute_aesthetics(d , plot ))
177
-
177
+
178
+ # The computed aesthetic codes the groups as integers
179
+ # Here we build a map each of the integer values to the group label
180
+ group_maps <- Map(function (x , y ) {
181
+ tryCatch({
182
+ x_group <- x [[" group" ]]
183
+ names(x_group ) <- y
184
+ x_group <- x_group [! duplicated(x_group )]
185
+ x_group
186
+ }, error = function (e ) NULL
187
+ )
188
+ }, data , groupDomains )
189
+
178
190
# Transform all scales
179
191
data <- lapply(data , ggfun(" scales_transform_df" ), scales = scales )
180
192
@@ -205,7 +217,17 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all",
205
217
206
218
# Reparameterise geoms from (e.g.) y and width to ymin and ymax
207
219
data <- by_layer(function (l , d ) l $ compute_geom_1(d ))
208
-
220
+
221
+ # compute_geom_1 can reorder the rows from `data`, making groupDomains
222
+ # invalid. We rebuild groupDomains based on the current `data` and the
223
+ # group map we built before.
224
+ groupDomains <- Map(function (x , y ) {
225
+ tryCatch({
226
+ names(y )[match(x $ group , y )]
227
+ }, error = function (e ) NULL
228
+ )
229
+ }, data , group_maps )
230
+
209
231
# Apply position adjustments
210
232
data <- by_layer(function (l , d ) l $ compute_position(d , layout ))
211
233
0 commit comments