Skip to content

Commit e883b0b

Browse files
committed
Merge pull request #241 from ropensci/baobao-legend_position
Fixing Legend Position
2 parents 43b7daa + cd0e47f commit e883b0b

File tree

4 files changed

+47
-8
lines changed

4 files changed

+47
-8
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: plotly
22
Title: Create interactive web-based graphs via plotly's API
3-
Version: 1.0.2
3+
Version: 1.0.3
44
Authors@R: c(person("Chris", "Parmer", role = c("aut", "cph"),
55
email = "[email protected]"),
66
person("Scott", "Chamberlain", role = "aut",

NEWS

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
1.0.3 -- 7 Aug 2015
2+
3+
Improved legend positioning. See #241
4+
15
1.0.2 -- 2 Aug 2015
26

37
* last_plot() will now look for the last plotly object; if not found, it will try to find the last ggplot object.

R/ggplotly.R

+8-7
Original file line numberDiff line numberDiff line change
@@ -676,9 +676,11 @@ gg2list <- function(p) {
676676
if (exists("increase_margin_r")) {
677677
layout$margin$r <- 60
678678
}
679-
layout$legend <- list(bordercolor="transparent",
680-
x=1.05, y=1/2,
681-
xanchor="center", yanchor="top")
679+
layout$legend <- list(bordercolor = "transparent",
680+
x = 1.01,
681+
y = 0.075 * 0.5* length(trace.list) + 0.45,
682+
xref="paper", yref="paper",
683+
xanchor = "left", yanchor = "top")
682684

683685
## Legend hiding when guides(fill="none").
684686
legends.present <- unique(unlist(layer.legends))
@@ -723,15 +725,14 @@ gg2list <- function(p) {
723725
nann <- 1
724726
}
725727
annotations[[nann]] <- list(text=legend.title,
726-
x=layout$legend$x,
727-
y=layout$legend$y * 1.04,
728+
x = layout$legend$x * 1.0154,
729+
y = 0.075 * 0.5* length(trace.list) + 0.55,
728730
showarrow=FALSE,
729731
xref="paper", yref="paper",
730-
xanchor="center",
732+
xanchor="left", yanchor = "top",
731733
textangle=0)
732734
layout$annotations <- annotations
733735
}
734-
735736
# Family font for text
736737
if (!is.null(theme.pars$text$family)) {
737738
layout$titlefont$family <- theme.pars$text$family

tests/testthat/test-ggplot-legend.R

+34
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,37 @@ test_that("0 breaks -> 3 traces with showlegend=FALSE", {
8383
computed.showlegend <- sapply(info$traces, "[[", "showlegend")
8484
expect_identical(as.logical(computed.showlegend), expected.showlegend)
8585
})
86+
87+
# test of legend position
88+
test_that("very long legend items", {
89+
long_items <- data.frame(cat1 = sample(x = LETTERS[1:10],
90+
size = 100, replace = TRUE),
91+
cat2 = sample(x = c("AAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
92+
"BBBBBBBBBBBBBBBBBBBBBBBBBBBBB",
93+
"CCCCCCCCCCCCCCCCCCCCCCCCCCCCC"),
94+
size = 100, replace = TRUE))
95+
p_long_items <- ggplot(long_items, aes(cat1, fill=cat2)) +
96+
geom_bar(position="dodge")
97+
info <- expect_traces(p_long_items, 3, "very long legend items")
98+
expect_equal(length(info$layout$annotations), 1)
99+
expected.names <- levels(long_items$cat2)
100+
expect_identical(info$layout$annotations[[1]]$y -
101+
info$layout$legend$y > 0, TRUE)
102+
})
103+
104+
# test of legend position
105+
test_that("many legend items", {
106+
many_items <- data.frame(cat1 = sample(x = paste0("Group ", LETTERS[1:12]),
107+
size = 100, replace = TRUE),
108+
cat2 = sample(x = c("foo", "bar", "baz"),
109+
size = 100, replace = TRUE))
110+
p_many_items <- ggplot(many_items, aes(cat2, fill=cat1)) +
111+
geom_bar(position="dodge")
112+
info <- expect_traces(p_many_items, 12, "many legend items")
113+
expect_equal(length(info$layout$annotations), 1)
114+
expected.names <- levels(many_items$cat2)
115+
expect_identical(info$layout$annotations[[1]]$y > 0.5, TRUE)
116+
expect_identical(info$layout$annotations[[1]]$y -
117+
info$layout$legend$y > 0, TRUE)
118+
})
119+

0 commit comments

Comments
 (0)