|
6 | 6 | #' @param p a ggplot object. |
7 | 7 | #' @param width Width of the plot in pixels (optional, defaults to automatic sizing). |
8 | 8 | #' @param height Height of the plot in pixels (optional, defaults to automatic sizing). |
9 | | -#' @param mapping a character vector specifying which aesthetic mappings to show |
| 9 | +#' @param tooltip a character vector specifying which aesthetic mappings to show |
10 | 10 | #' in the tooltip. The default, "all", means show all the aesthetic mappings |
11 | 11 | #' (including the unofficial "text" aesthetic). The order of variables here will |
12 | 12 | #' also control the order they appear. For example, use |
13 | | -#' \code{mapping = c("y", "x", "colour")} if you want y first, x second, and |
| 13 | +#' \code{tooltip = c("y", "x", "colour")} if you want y first, x second, and |
14 | 14 | #' colour last. |
15 | 15 | #' @param source Only relevant for \link{event_data}. |
16 | 16 | #' @seealso \link{signup}, \link{plot_ly} |
|
31 | 31 | #' } |
32 | 32 | #' |
33 | 33 | ggplotly <- function(p = ggplot2::last_plot(), width = NULL, height = NULL, |
34 | | - mapping = "all", source = "A") { |
35 | | - l <- gg2list(p, width = width, height = height, mapping = mapping, source = source) |
| 34 | + tooltip = "all", source = "A") { |
| 35 | + l <- gg2list(p, width = width, height = height, tooltip = tooltip, source = source) |
36 | 36 | hash_plot(p$data, l) |
37 | 37 | } |
38 | 38 |
|
39 | 39 | #' Convert a ggplot to a list. |
40 | 40 | #' @param p ggplot2 plot. |
41 | 41 | #' @param width Width of the plot in pixels (optional, defaults to automatic sizing). |
42 | 42 | #' @param height Height of the plot in pixels (optional, defaults to automatic sizing). |
43 | | -#' @param mapping a character vector specifying which aesthetic mappings to show in the |
44 | | -#' tooltip. The default, "all", means show all the aesthetic mappings |
| 43 | +#' @param tooltip a character vector specifying which aesthetic tooltips to show in the |
| 44 | +#' tooltip. The default, "all", means show all the aesthetic tooltips |
45 | 45 | #' (including the unofficial "text" aesthetic). |
46 | 46 | #' @param source Only relevant for \link{event_data}. |
47 | 47 | #' @return a 'built' plotly object (list with names "data" and "layout"). |
48 | 48 | #' @export |
49 | | -gg2list <- function(p, width = NULL, height = NULL, mapping = "all", source = "A") { |
| 49 | +gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A") { |
50 | 50 | # ------------------------------------------------------------------------ |
51 | 51 | # Our internal version of ggplot2::ggplot_build(). Modified from |
52 | 52 | # https://github.com/hadley/ggplot2/blob/0cd0ba/R/plot-build.r#L18-L92 |
@@ -190,8 +190,8 @@ gg2list <- function(p, width = NULL, height = NULL, mapping = "all", source = "A |
190 | 190 | # remove leading/trailing dots in "hidden" stat aes |
191 | 191 | map <- sub("^\\.\\.", "", sub("\\.\\.$", "", map)) |
192 | 192 | # TODO: allow users to specify a _list_ of mappings? |
193 | | - if (!identical(mapping, "all")) { |
194 | | - map <- map[names(map) %in% mapping] |
| 193 | + if (!identical(tooltip, "all")) { |
| 194 | + map <- map[names(map) %in% tooltip] |
195 | 195 | } |
196 | 196 | # tooltips for discrete positional scales are misleading |
197 | 197 | if (scales$get_scales("x")$is_discrete()) { |
@@ -240,6 +240,12 @@ gg2list <- function(p, width = NULL, height = NULL, mapping = "all", source = "A |
240 | 240 | tr$hoverinfo <- tr$hoverinfo %||%"text" |
241 | 241 | tr |
242 | 242 | }) |
| 243 | + # show only one legend entry per legendgroup |
| 244 | + grps <- sapply(traces, "[[", "legendgroup") |
| 245 | + traces <- Map(function(x, y) { |
| 246 | + x$showlegend <- isTRUE(x$showlegend) && y |
| 247 | + x |
| 248 | + }, traces, !duplicated(grps)) |
243 | 249 |
|
244 | 250 | # ------------------------------------------------------------------------ |
245 | 251 | # axis/facet/margin conversion |
@@ -587,6 +593,9 @@ gg2list <- function(p, width = NULL, height = NULL, mapping = "all", source = "A |
587 | 593 | if (all(modes[idx] %in% c("lines", "markers"))) { |
588 | 594 | mergedTraces[[i]] <- Reduce(modifyList, traces[idx]) |
589 | 595 | mergedTraces[[i]]$mode <- "markers+lines" |
| 596 | + if (any(sapply(traces[idx], "[[", "showlegend"))) { |
| 597 | + mergedTraces[[i]]$showlegend <- TRUE |
| 598 | + } |
590 | 599 | } |
591 | 600 | } |
592 | 601 | traces <- mergedTraces |
|
0 commit comments