Skip to content

Commit aaa95c2

Browse files
committed
support "group" property to break lines
"group" property doesn't create any new trace, but if the trace mode is "lines", NA points are inserted at the "group" boundaries within the same trace. That is interpreted as the polyline break by plotlyjs.
1 parent 50f515c commit aaa95c2

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

R/plotly.R

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,14 +339,29 @@ plotly_build.plotly_hash <- function(l = last_plot()) {
339339
# define the dat traces
340340
points_df <- data.frame(dat_index = seq_along(dat[["x"]] %||% dat[["y"]] %||% dat[["z"]])) %>% # indices of the original data elements used in the trace FIXME properly define data length
341341
tracify_by_color(dat) %>%
342-
tracify_by_column(dat, "symbol", force_numeric=TRUE)
343-
trace_key_cols <- setdiff(colnames(points_df), "dat_index")
344-
points_df <- dplyr::arrange_(points_df, .dots = c(trace_key_cols, "dat_index")) %>%
345-
dplyr::group_by_(.dots = trace_key_cols)
342+
tracify_by_column(dat, "symbol", force_numeric=TRUE) %>%
343+
tracify_by_column(dat, "group", force_numeric=TRUE)
344+
subtrace_key_cols <- setdiff(colnames(points_df), "dat_index")
345+
trace_key_cols <- setdiff(subtrace_key_cols, "group_index")
346+
points_df <- dplyr::arrange_(points_df, .dots = c(subtrace_key_cols, "dat_index")) %>%
347+
dplyr::group_by_(.dots = subtrace_key_cols)
348+
points_df$subtrace_index <- dplyr::group_indices(points_df)
349+
points_df <- dplyr::group_by_(points_df, .dots = trace_key_cols)
346350
points_df$trace_index <- dplyr::group_indices(points_df)
347351
points_df <- dplyr::ungroup(points_df)
348352
points_df$point_order <- seq_len(nrow(points_df))
349353

354+
# polylines should be further disrupted at 'group' boundaries by inserting NAs
355+
if (grepl("lines", dat[["mode"]] %||% "markers+lines") && "group_index" %in% subtrace_key_cols) {
356+
subtrace_bound <- points_df$trace_index[-1] == points_df$trace_index[-nrow(points_df)] &
357+
points_df$subtrace_index[-1] != points_df$subtrace_index[-nrow(points_df)]
358+
if (any(subtrace_bound)) {
359+
points_df <- rbind(points_df, points_df[subtrace_bound,]) %>% dplyr::arrange(point_order)
360+
points_df$dat_index[c(FALSE, points_df$point_order[-1] == points_df$point_order[-nrow(points_df)])] <- NA
361+
points_df$point_order <- seq_len(nrow(points_df)) # order wrt added points
362+
}
363+
}
364+
350365
trace_point_indices <- attr(dplyr::group_by(points_df, trace_index), "indices")
351366
if (length(trace_point_indices) > 0) {
352367
trace_point_indices <- lapply(trace_point_indices, function(ixs) ixs+1L)

0 commit comments

Comments
 (0)