|
13 | 13 | #' @export
|
14 | 14 | #' @examples
|
15 | 15 | #'
|
16 |
| -#' p <- qplot(data = mtcars, wt, mpg, geom = c("point", "smooth")) |
17 |
| -#' # keep the hover info for points, but remove it for the line/ribbon |
| 16 | +#' # style() is especially useful in conjunction with ggplotly() |
| 17 | +#' # It allows you to leverage the underlying plotly.js library to change |
| 18 | +#' # the return result of ggplotly() |
| 19 | +#' (p <- ggplotly(qplot(data = mtcars, wt, mpg, geom = c("point", "smooth")))) |
| 20 | +#' |
| 21 | +#' # removes hoverinfo for the line/ribbon traces (use `plotly_json()` to verify!) |
18 | 22 | #' style(p, hoverinfo = "none", traces = c(2, 3))
|
19 | 23 | #'
|
| 24 | +#' # another example with plot_ly() instead of ggplotly() |
| 25 | +#' marker <- list( |
| 26 | +#' color = "red", |
| 27 | +#' line = list( |
| 28 | +#' width = 20, |
| 29 | +#' color = "black" |
| 30 | +#' ) |
| 31 | +#' ) |
| 32 | +#' (p <- plot_ly(x = 1:10, y = 1:10, marker = marker)) |
| 33 | +#' |
| 34 | +#' # note how the entire (marker) object is replaced if a list is provided |
| 35 | +#' style(p, marker = list(line = list(color = "blue"))) |
| 36 | +#' |
| 37 | +#' # similar to plotly.js, you can update a particular attribute like so |
| 38 | +#' # https://github.com/plotly/plotly.js/issues/1866#issuecomment-314115744 |
| 39 | +#' style(p, marker.line.color = "blue") |
| 40 | +#' # this clobbers the previously supplied marker.line.color |
| 41 | +#' style(p, marker.line = list(width = 2.5), marker.size = 10) |
| 42 | +#' |
20 | 43 | style <- function(p, ..., traces = NULL) {
|
21 | 44 | p <- plotly_build(p)
|
22 |
| - nTraces <- length(p$x$data) |
23 |
| - traces <- traces %||% seq_len(nTraces) |
24 |
| - idx <- traces > nTraces |
25 |
| - traces <- traces[!idx] |
26 |
| - if (any(idx)) warning("You've referenced non-existent traces", call. = FALSE) |
27 |
| - argz <- list(...) |
28 |
| - for (i in traces) { |
29 |
| - for (j in names(argz)) { |
30 |
| - p$x$data[[i]][[j]] <- argz[[j]] |
31 |
| - } |
| 45 | + n_traces <- length(p$x$data) |
| 46 | + trace_idx <- traces %||% seq_len(n_traces) |
| 47 | + if (any(trace_idx > n_traces)) { |
| 48 | + warning("You've referenced non-existent traces", call. = FALSE) |
32 | 49 | }
|
| 50 | + |
| 51 | + values <- list(...) |
| 52 | + paths <- strsplit(names(values), "\\.") |
| 53 | + |
| 54 | + p$x$data[trace_idx] <- lapply(p$x$data[trace_idx], function(trace) { |
| 55 | + for (i in seq_along(paths)) { |
| 56 | + trace <- trace_replace(trace, paths[[i]], values[[i]]) |
| 57 | + } |
| 58 | + trace |
| 59 | + }) |
| 60 | + |
33 | 61 | p
|
34 | 62 | }
|
| 63 | + |
| 64 | +#' @param trace a single plotly trace |
| 65 | +#' @param path character vector of path elements pointing to a trace property: c("marker", "line", "size") |
| 66 | +#' @param value a value to assign to that trace property |
| 67 | +trace_replace <- function(trace, path, value) { |
| 68 | + if (length(path) == 0) return(trace) |
| 69 | + if (length(path) == 1) { |
| 70 | + trace[[path]] <- value |
| 71 | + return(trace) |
| 72 | + } |
| 73 | + trace[[path[1]]] <- trace[[path[1]]] %||% setNames(list(NULL), path[2]) |
| 74 | + trace[[path[1]]] <- trace_replace(trace[[path[1]]], path[-1], value) |
| 75 | + trace |
| 76 | +} |
0 commit comments