Skip to content

Commit bb8c174

Browse files
committed
Merge pull request #496 from ropensci/fix/tooManyLegendEntries
don't show duplicated legendgroups
2 parents 8bbb6f5 + a52383e commit bb8c174

File tree

7 files changed

+44
-17
lines changed

7 files changed

+44
-17
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: plotly
22
Title: Create Interactive Web Graphics via Plotly's JavaScript Graphing Library
3-
Version: 3.2.1
3+
Version: 3.3.1
44
Authors@R: c(person("Carson", "Sievert", role = c("aut", "cre"),
55
email = "cpsievert1@gmail.com"),
66
person("Chris", "Parmer", role = c("aut", "cph"),

NEWS

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
3.3.1 -- 10 Mar 2015
2+
3+
CHANGES:
4+
5+
* Changed the mapping argument name to tooltip (which seems like a better name).
6+
7+
BUGFIX:
8+
9+
* Redundant legend entries are no longer shown.
10+
111
3.2.1 -- 10 Mar 2015
212

313
BUGFIX:

R/ggplotly.R

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
#' @param p a ggplot object.
77
#' @param width Width of the plot in pixels (optional, defaults to automatic sizing).
88
#' @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
1010
#' in the tooltip. The default, "all", means show all the aesthetic mappings
1111
#' (including the unofficial "text" aesthetic). The order of variables here will
1212
#' 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
1414
#' colour last.
1515
#' @param source Only relevant for \link{event_data}.
1616
#' @seealso \link{signup}, \link{plot_ly}
@@ -31,22 +31,22 @@
3131
#' }
3232
#'
3333
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)
3636
hash_plot(p$data, l)
3737
}
3838

3939
#' Convert a ggplot to a list.
4040
#' @param p ggplot2 plot.
4141
#' @param width Width of the plot in pixels (optional, defaults to automatic sizing).
4242
#' @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
4545
#' (including the unofficial "text" aesthetic).
4646
#' @param source Only relevant for \link{event_data}.
4747
#' @return a 'built' plotly object (list with names "data" and "layout").
4848
#' @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") {
5050
# ------------------------------------------------------------------------
5151
# Our internal version of ggplot2::ggplot_build(). Modified from
5252
# 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
190190
# remove leading/trailing dots in "hidden" stat aes
191191
map <- sub("^\\.\\.", "", sub("\\.\\.$", "", map))
192192
# 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]
195195
}
196196
# tooltips for discrete positional scales are misleading
197197
if (scales$get_scales("x")$is_discrete()) {
@@ -240,6 +240,12 @@ gg2list <- function(p, width = NULL, height = NULL, mapping = "all", source = "A
240240
tr$hoverinfo <- tr$hoverinfo %||%"text"
241241
tr
242242
})
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))
243249

244250
# ------------------------------------------------------------------------
245251
# axis/facet/margin conversion
@@ -587,6 +593,9 @@ gg2list <- function(p, width = NULL, height = NULL, mapping = "all", source = "A
587593
if (all(modes[idx] %in% c("lines", "markers"))) {
588594
mergedTraces[[i]] <- Reduce(modifyList, traces[idx])
589595
mergedTraces[[i]]$mode <- "markers+lines"
596+
if (any(sapply(traces[idx], "[[", "showlegend"))) {
597+
mergedTraces[[i]]$showlegend <- TRUE
598+
}
590599
}
591600
}
592601
traces <- mergedTraces

man/gg2list.Rd

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/ggplotly.Rd

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-ggplot-facets.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ test_that("6 facets becomes 6 panels", {
77
theme_bw() +
88
theme(panel.margin = grid::unit(0, "cm"))
99
info <- save_outputs(gg, "barley")
10+
# two legend entries, but two groups
11+
expect_equal(sum(sapply(info$data, "[[", "showlegend")), 2)
12+
expect_identical(
13+
sort(unique(sapply(info$data, "[[", "legendgroup"))), c("1931", "1932")
14+
)
15+
expect_identical(
16+
sort(unique(sapply(info$data, "[[", "name"))), c("1931", "1932")
17+
)
1018
})
1119

1220
test_that("3 facets becomes 3 panels", {

tests/testthat/test-ggplot-violin.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ test_that("geom_violin with fill aes works", {
2424
})
2525

2626
test_that("can hide x values in tooltip", {
27-
p <- ggplotly(gg2, mapping = "y")
27+
p <- ggplotly(gg2, tooltip = "y")
2828
l <- plotly_build(p)
2929
expect_equal(sum(grepl("cyl", l$data[[1]]$text)), 0)
3030
})

0 commit comments

Comments
 (0)