Skip to content

Commit f9c671f

Browse files
committed
Merge pull request #503 from ropensci/fix/legendTitles
legend title annotation
2 parents 606d996 + 5c03f64 commit f9c671f

File tree

6 files changed

+30
-2
lines changed

6 files changed

+30
-2
lines changed

.Rbuildignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
CONDUCT.md
66
CONTRIBUTING.md
77
build_site.R
8+
inst/examples/flexdashboard

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.js'
3-
Version: 3.3.2
3+
Version: 3.4.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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
3.4.1 -- 13 Mar 2016
2+
3+
BUGFIX:
4+
5+
Legend titles are now supported.
6+
17
3.4.0 -- 12 Mar 2016
28

39
NEW FEATURES:

R/ggplotly.R

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,23 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
525525
}
526526
}
527527
traces <- c(traces, colorbar)
528+
529+
# legend title annotation - https://github.com/plotly/plotly.js/issues/276
530+
legendTitles <- compact(lapply(gdefs, function(g) if (inherits(g, "legend")) g$title else NULL))
531+
legendTitle <- paste(legendTitles, collapse = "<br>")
532+
titleAnnotation <- make_label(
533+
legendTitle,
534+
x = gglayout$legend$x %||% 1.02,
535+
y = gglayout$legend$y %||% 1,
536+
theme$legend.title,
537+
xanchor = "left",
538+
yanchor = "top"
539+
)
540+
gglayout$annotations <- c(gglayout$annotations, titleAnnotation)
541+
# adjust the height of the legend to accomodate for the title
542+
# this assumes the legend always appears below colorbars
543+
gglayout$legend$y <- (gglayout$legend$y %||% 1) -
544+
length(legendTitles) * unitConvert(theme$legend.title$size, "npc", "height")
528545
}
529546

530547
# geom_bar() hacks

tests/testthat/test-ggplot-density2d.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ 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)
54+
expect_true(L$layout$legend$y < 0.5)
5555
expect_true(L$layout$legend$yanchor == "top")
5656
expect_true(colorbar$marker$colorbar$y == 1)
5757
expect_true(colorbar$marker$colorbar$yanchor == "top")

tests/testthat/test-ggplot-legend.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ test_that("Discrete colour and shape get merged into one legend", {
2929
expect_identical(
3030
nms, paste0("(", d$vs, ",", d$cyl, ")")
3131
)
32+
a <- info$layout$annotations
33+
expect_match(a[[3]]$text, "^factor\\(vs\\)")
34+
expect_match(a[[3]]$text, "factor\\(cyl\\)$")
35+
expect_true(a[[3]]$y > info$layout$legend$y)
3236
})
3337

3438

0 commit comments

Comments
 (0)