Skip to content

Commit cef36dd

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 da66b99 commit cef36dd

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
@@ -334,14 +334,29 @@ plotly_build.plotly_hash <- function(l = last_plot()) {
334334
# define the dat traces
335335
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
336336
tracify_by_color(dat) %>%
337-
tracify_by_column(dat, "symbol", force_numeric=TRUE)
338-
trace_key_cols <- setdiff(colnames(points_df), "dat_index")
339-
points_df <- dplyr::arrange_(points_df, .dots = c(trace_key_cols, "dat_index")) %>%
340-
dplyr::group_by_(.dots = trace_key_cols)
337+
tracify_by_column(dat, "symbol", force_numeric=TRUE) %>%
338+
tracify_by_column(dat, "group", force_numeric=TRUE)
339+
subtrace_key_cols <- setdiff(colnames(points_df), "dat_index")
340+
trace_key_cols <- setdiff(subtrace_key_cols, "group_index")
341+
points_df <- dplyr::arrange_(points_df, .dots = c(subtrace_key_cols, "dat_index")) %>%
342+
dplyr::group_by_(.dots = subtrace_key_cols)
343+
points_df$subtrace_index <- dplyr::group_indices(points_df)
344+
points_df <- dplyr::group_by_(points_df, .dots = trace_key_cols)
341345
points_df$trace_index <- dplyr::group_indices(points_df)
342346
points_df <- dplyr::ungroup(points_df)
343347
points_df$point_order <- seq_len(nrow(points_df))
344348

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

0 commit comments

Comments
 (0)