Skip to content

Commit 3435fbb

Browse files
committed
Use layout.legend.title over layout.annotations to when converting guides for discrete scales
1 parent 32f491b commit 3435fbb

File tree

5 files changed

+11
-30
lines changed

5 files changed

+11
-30
lines changed

R/ggplotly.R

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -999,26 +999,12 @@ gg2list <- function(p, width = NULL, height = NULL,
999999
}
10001000
traces <- c(traces, colorbar)
10011001

1002-
# legend title annotation - https://github.com/plotly/plotly.js/issues/276
1003-
if (isTRUE(gglayout$showlegend)) {
1004-
legendTitles <- compact(lapply(gdefs, function(g) if (inherits(g, "legend")) g$title else NULL))
1005-
legendTitle <- paste(legendTitles, collapse = br())
1006-
titleAnnotation <- make_label(
1007-
legendTitle,
1008-
x = gglayout$legend$x %||% 1.02,
1009-
y = gglayout$legend$y %||% 1,
1010-
theme$legend.title,
1011-
xanchor = "left",
1012-
yanchor = "bottom",
1013-
# just so the R client knows this is a title
1014-
legendTitle = TRUE
1015-
)
1016-
gglayout$annotations <- c(gglayout$annotations, titleAnnotation)
1017-
# adjust the height of the legend to accomodate for the title
1018-
# this assumes the legend always appears below colorbars
1019-
gglayout$legend$y <- (gglayout$legend$y %||% 1) -
1020-
length(legendTitles) * unitConvert(theme$legend.title$size, "npc", "height")
1021-
}
1002+
legendTitles <- compact(lapply(gdefs, function(g) if (inherits(g, "legend")) g$title else NULL))
1003+
legendTitle <- paste(legendTitles, collapse = br())
1004+
gglayout$legend$title <- list(
1005+
text = legendTitle,
1006+
font = text2font(theme$legend.title)
1007+
)
10221008
}
10231009

10241010
# flip x/y in traces for flipped coordinates

R/utils.R

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -845,9 +845,6 @@ verify_showlegend <- function(p) {
845845
# this attribute should be set in hide_legend()
846846
# it ensures that "legend titles" go away in addition to showlegend = FALSE
847847
if (isTRUE(p$x$.hideLegend)) {
848-
ann <- p$x$layout$annotations
849-
is_title <- vapply(ann, function(x) isTRUE(x$legendTitle), logical(1))
850-
p$x$layout$annotations <- ann[!is_title]
851848
p$x$layout$showlegend <- FALSE
852849
}
853850
show <- vapply(p$x$data, function(x) x$showlegend %||% TRUE, logical(1))

tests/testthat/test-ggplot-bar.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ test_that("guides(fill=FALSE) does not affect colour legend", {
134134
expect_true(is.character(tr$marker$line$color))
135135
expect_true(tr$showlegend)
136136
}
137-
expect_match(info$layout$annotations[[1]]$text, "time")
137+
expect_match(info$layout$legend$title$text, "time")
138138
expect_true(info$layout$showlegend)
139139
})
140140

tests/testthat/test-ggplot-density2d.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ test_that("StatDensity2d with GeomPolygon translates to filled path(s)", {
5151
length(unique(unlist(lapply(polygons, "[[", "fillcolor")))) > 1
5252
)
5353
# ensure the legend/guide are placed correctly
54-
expect_true(L$layout$legend$y < 0.5)
5554
expect_true(L$layout$legend$yanchor == "top")
5655
expect_true(colorbar$marker$colorbar$y == 1)
5756
expect_true(colorbar$marker$colorbar$yanchor == "top")

tests/testthat/test-ggplot-legend.R

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ test_that("Discrete colour and shape get merged into one legend", {
2828
expect_identical(
2929
nms, paste0("(", d$vs, ",", d$cyl, ")")
3030
)
31-
a <- info$layout$annotations
32-
expect_match(a[[1]]$text, "^factor\\(vs\\)")
33-
expect_match(a[[1]]$text, "factor\\(cyl\\)$")
34-
expect_true(a[[1]]$y > info$layout$legend$y)
31+
legend_title <- info$layout$legend$title$text
32+
expect_match(legend_title, "^factor\\(vs\\)")
33+
expect_match(legend_title, "factor\\(cyl\\)$")
3534
})
3635

3736

@@ -80,7 +79,7 @@ test_that("legend is created with discrete mapping regardless of unique values",
8079
info <- expect_traces(p, 1, "one-entry")
8180
expect_true(info$data[[1]]$showlegend)
8281
expect_true(info$layout$showlegend)
83-
expect_equivalent(length(info$layout$annotations), 1)
82+
expect_true(nzchar(info$layout$legend$title$text))
8483
})
8584

8685
test_that("can hide legend", {

0 commit comments

Comments
 (0)