Skip to content

Commit c4e5088

Browse files
committed
since groups are nested within traces, arrange data by trace index _before_ computing groups
fixes #716
1 parent e93e19e commit c4e5088

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

R/plotly_build.R

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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]]

R/utils.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ as_df <- function(x) {
6767
}
6868
}
6969

70+
# arrange data if the vars exist, don't throw error if they don't
71+
arrange_safe <- function(data, vars) {
72+
vars <- vars[vars %in% names(data)]
73+
if (length(vars)) dplyr::arrange_(data, vars) else data
74+
}
75+
7076
# make sure plot attributes adhere to the plotly.js schema
7177
verify_attr_names <- function(p) {
7278
# some layout attributes (e.g., [x-y]axis can have trailing numbers)

0 commit comments

Comments
 (0)