@@ -123,12 +123,16 @@ plotly_build.plotly <- function(p) {
123123 builtData $ .plotlyTraceIndex <- Reduce(paste2 , builtData [isSplit ])
124124 }
125125 # Build the index used to determine grouping (later on, NAs are inserted
126- # via group2NA() to create the groups). This is done in 2 parts:
127- # 1. Translate missing values on positional scales to a grouping variable.
126+ # via group2NA() to create the groups). This is done in 4 parts:
127+ # 1. Sort data by the trace index since groups are nested within traces.
128+ # 2. Translate missing values on positional scales to a grouping variable.
128129 # If grouping isn't relevant for this trace, a warning is thrown since
129130 # NAs are removed.
130- # 2 . The grouping from (1 ) and any groups detected via dplyr::groups()
131+ # 3 . The grouping from (2 ) and any groups detected via dplyr::groups()
131132 # are combined into a single grouping variable, .plotlyGroupIndex
133+ builtData <- arrange_safe(
134+ builtData , c(" .plotlyTraceIndex" , if (inherits(trace , " plotly_line" )) " x" )
135+ )
132136 isComplete <- complete.cases(builtData [names(builtData ) %in% c(" x" , " y" , " z" )])
133137 # is grouping relevant for this geometry? (e.g., grouping doesn't effect a scatterplot)
134138 hasGrp <- inherits(trace , paste0(" plotly_" , c(" segment" , " path" , " line" , " polygon" ))) ||
@@ -154,17 +158,14 @@ plotly_build.plotly <- function(p) {
154158 interaction(dat [isComplete , grps , drop = FALSE ]),
155159 builtData $ .plotlyGroupIndex %|| % " "
156160 )
161+ builtData <- arrange_safe(
162+ builtData , c(" .plotlyTraceIndex" , " .plotlyGroupIndex" , if (inherits(trace , " plotly_line" )) " x" )
163+ )
157164 }
165+
158166 builtData <- train_data(builtData , trace )
159167 trace $ .plotlyVariableMapping <- names(builtData )
160- # arrange the built data
161- arrangeVars <- c(
162- " .plotlyTraceIndex" , " group" , if (inherits(trace , " plotly_line" )) " x"
163- )
164- arrangeVars <- arrangeVars [arrangeVars %in% names(builtData )]
165- if (length(arrangeVars )) {
166- builtData <- dplyr :: arrange_(builtData , arrangeVars )
167- }
168+
168169 # copy over to the trace data
169170 for (i in names(builtData )) {
170171 trace [[i ]] <- builtData [[i ]]
0 commit comments