Skip to content

Commit dec99f1

Browse files
committed
Merge 8136ed6 into 140e3ca
2 parents 140e3ca + 8136ed6 commit dec99f1

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

R/ggplotly.R

+8-7
Original file line numberDiff line numberDiff line change
@@ -662,9 +662,11 @@ gg2list <- function(p) {
662662
if (exists("increase_margin_r")) {
663663
layout$margin$r <- 60
664664
}
665-
layout$legend <- list(bordercolor="transparent",
666-
x=1.05, y=1/2,
667-
xanchor="center", yanchor="top")
665+
layout$legend <- list(bordercolor = "transparent",
666+
x = 1.01,
667+
y = 0.075 * 0.5* length(trace.list) + 0.45,
668+
xref="paper", yref="paper",
669+
xanchor = "left", yanchor = "top")
668670

669671
## Legend hiding when guides(fill="none").
670672
legends.present <- unique(unlist(layer.legends))
@@ -709,15 +711,14 @@ gg2list <- function(p) {
709711
nann <- 1
710712
}
711713
annotations[[nann]] <- list(text=legend.title,
712-
x=layout$legend$x,
713-
y=layout$legend$y * 1.04,
714+
x = layout$legend$x * 1.0154,
715+
y = 0.075 * 0.5* length(trace.list) + 0.55,
714716
showarrow=FALSE,
715717
xref="paper", yref="paper",
716-
xanchor="center",
718+
xanchor="left", yanchor = "top",
717719
textangle=0)
718720
layout$annotations <- annotations
719721
}
720-
721722
# Family font for text
722723
if (!is.null(theme.pars$text$family)) {
723724
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)