Skip to content

Commit 12c5adc

Browse files
committed
add hide_legend and hide_guides to complement hide_colorbar
1 parent c49bdb0 commit 12c5adc

File tree

9 files changed

+161
-7
lines changed

9 files changed

+161
-7
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ export(group_by_.plotly)
9191
export(groups)
9292
export(groups.plotly)
9393
export(hide_colorbar)
94+
export(hide_guides)
95+
export(hide_legend)
9496
export(knit_print.plotly_figure)
9597
export(last_plot)
9698
export(layout)

R/crosstalk.R

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# #' Control selection appearance
2+
# #'
3+
# #' Control the visual appearance of selections deriving from a given
4+
# #' selection set.
5+
# #'
6+
# #' @param dynamic should UI controls for managing selection aesthetics be
7+
# #' included in the output?
8+
# #' @param persistant should selections persist (i.e., accumulate)?
9+
# #' @param color color(s) to use for highlighting selections.
10+
# #' If \code{NULL} (the default), the color of selections are not altered.
11+
# #' If not \code{NULL}
12+
# #' a valid color code,
13+
# #' If not \code{dynamic}, this argument should be length 1,
14+
# #' If \code{dynamic},
15+
# #' this argument accepts a character
16+
# #' @param opacityDim a number between 0 and 1 used to reduce the
17+
# #' opacity of non-selected traces (by multiplying with the existing opacity).
18+
# #' @param showInLegend show a legend entry for additional "selection traces"?
19+
# #' @export
20+
# #'
21+
#
22+
# ct_opts <- function(dynamic = FALSE, persitant = dynamic, color = NULL,
23+
# opacityDim = 0.2, showInLegend = FALSE) {
24+
# if (opacityDim < 0 || 1 < opacityDim) {
25+
# stop("opacityDim must be between 0 and 1", call. = FALSE)
26+
# }
27+
# if (dynamic && length(color) < 2) {
28+
# message("Adding more colors to the selection color palette")
29+
# color <- c(color, RColorBrewer::brewer.pal(8, "Set2"))
30+
# }
31+
# if (!dynamic) {
32+
# color <- color[1]
33+
# }
34+
# list(
35+
# color = toRGB(color),
36+
# dynamic = dynamic,
37+
# persitant = persitant,
38+
# opacityDim = opacityDim,
39+
# showInLegend = showInLegend
40+
# )
41+
# }

R/ggplotly.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,9 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all",
550550
y = gglayout$legend$y %||% 1,
551551
theme$legend.title,
552552
xanchor = "left",
553-
yanchor = "top"
553+
yanchor = "top",
554+
# just so the R client knows this is a title
555+
legendTitle = TRUE
554556
)
555557
gglayout$annotations <- c(gglayout$annotations, titleAnnotation)
556558
# adjust the height of the legend to accomodate for the title

R/helpers.R

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
1-
#' Hide a color bar
1+
#' Hide guides (legends and colorbars)
2+
#'
3+
#' @param p a plotly object.
4+
#' @export
5+
#' @seealso \link{hide_legend}, \link{hide_colorbar}
6+
#'
7+
8+
hide_guides <- function(p) {
9+
hide_legend(hide_colorbar(p))
10+
}
11+
12+
13+
#' Hide color bar(s)
214
#'
315
#' @param p a plotly object.
416
#' @export
5-
#' @examples
17+
#' @seealso \link{hide_legend}
18+
#' @examples
619
#'
720
#' plot_ly(economics, x = ~date, y = ~unemploy / pop, color = ~pop) %>%
821
#' add_markers() %>%
@@ -21,6 +34,30 @@ hide_colorbar <- function(p) {
2134
p
2235
}
2336

37+
#' Hide legend
38+
#'
39+
#' @param p a plotly object.
40+
#' @export
41+
#' @seealso \link{hide_legend}
42+
#' @examples
43+
#'
44+
#' plot_ly(economics, x = ~date, y = ~unemploy / pop, color = ~pop) %>%
45+
#' add_markers() %>%
46+
#' hide_colorbar()
47+
48+
hide_legend <- function(p) {
49+
p <- plotly_build(p)
50+
# annotations have to be an array of objects, so this should be a list of lists
51+
ann <- p$x$layout$annotations
52+
for (i in seq_along(ann)) {
53+
if (isTRUE(ann[[i]]$legendTitle)) {
54+
p$x$layout$annotations[[i]] <- NULL
55+
}
56+
}
57+
p$x$layout$showlegend <- FALSE
58+
p
59+
}
60+
2461
#' Convert trace types to WebGL
2562
#'
2663
#' @param p a plotly or ggplot object.

man/hide_colorbar.Rd

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

man/hide_legend.Rd

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

tests/testthat/test-ggplot-legend.R

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,26 @@ test_that("very long legend items", {
7070
)
7171
p_long_items <- ggplot(long_items, aes(cat1, fill = cat2)) +
7272
geom_bar(position = "dodge")
73-
info <- expect_traces(p_long_items, 3, "very long legend items")
73+
info <- expect_traces(p_long_items, 3, "very-long-legend-items")
7474
})
7575

76+
iris$All <- "All species"
77+
p <- qplot(data = iris, x = Sepal.Length, y = Sepal.Width, color = All)
78+
79+
test_that("legend is created with discrete mapping regardless of unique values", {
80+
info <- expect_traces(p, 1, "one-entry")
81+
expect_true(info$data[[1]]$showlegend)
82+
expect_true(info$layout$showlegend)
83+
expect_equal(length(info$layout$annotations), 1)
84+
})
85+
86+
test_that("can hide legend", {
87+
info <- expect_traces(hide_legend(p), 1, "hide-legend")
88+
expect_false(info$layout$showlegend)
89+
expect_null(info$layout$annotations)
90+
})
91+
92+
7693
# test of legend position
7794
test_that("many legend items", {
7895
p <- ggplot(midwest, aes(category, fill = category)) + geom_bar()

tests/testthat/test-ggplot-text.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ gg <- ggplot(mtcars, aes(x = wt, y = mpg, label = rownames(mtcars))) +
55
info <- save_outputs(gg, "text")
66

77
test_that("label is translated correctly", {
8-
expect_identical(as.character(info$data[[1]]$text), rownames(mtcars))
8+
greps <- Map(function(x, y) grepl(x, y), rownames(mtcars), info$data[[1]]$text)
9+
expect_true(all(unlist(greps)))
910
})
1011

1112
test_that("position is translated correctly", {

tests/testthat/test-ggplot-tooltip.R

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,30 @@ test_that("group domain is included in hovertext", {
6868
pattern <- paste(unique(txhousing$city), collapse = "|")
6969
expect_true(all(grepl(pattern, txt)))
7070
})
71+
72+
73+
labelDF <- data.frame(
74+
label = paste0(("label"), c(1:10)),
75+
x = runif(10, 1, 10),
76+
y = runif(10, 1, 10)
77+
)
78+
# Create data frame for 10 edges
79+
edgeDF <- data.frame(
80+
x = runif(10, 1, 10),
81+
y = runif(10, 1, 10),
82+
xend = runif(10, 1, 10),
83+
yend = runif(10, 1, 10)
84+
)
85+
86+
myPlot <- ggplot(data = labelDF, aes(x = x, y = y)) +
87+
geom_segment(data = edgeDF, aes(x = x, y = y, xend = xend, yend = yend),
88+
colour = "pink") +
89+
geom_text(data = labelDF, aes(x = x, y = y, label = label), size = 10)
90+
91+
test_that("Hoverinfo is only displayed if no tooltip variables are present", {
92+
L <- save_outputs(p, "hovertext-display")
93+
L <- plotly_build(ggplotly(myPlot, tooltip = "label"))[["x"]]
94+
expect_equal(length(L$data), 2)
95+
expect_equal(sum(nchar(L$data[[1]]$text)), 0)
96+
expect_true(all(grepl("^label:", L$data[[2]]$text)))
97+
})

0 commit comments

Comments
 (0)