Skip to content

Commit e46d268

Browse files
committed
add fixes and unit tests
1 parent e047312 commit e46d268

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

R/ggplotly.R

+7-7
Original file line numberDiff line numberDiff line change
@@ -629,8 +629,10 @@ gg2list <- function(p) {
629629
layout$margin$r <- 60
630630
}
631631
layout$legend <- list(bordercolor = "transparent",
632-
x = 1.01, y = 0.5,
633-
xanchor = "left", yanchor = "center")
632+
x = 1.01,
633+
y = 0.075 * 0.5* length(trace.list) + 0.45,
634+
xref="paper", yref="paper",
635+
xanchor = "left", yanchor = "top")
634636

635637
## Legend hiding when guides(fill="none").
636638
legends.present <- unique(unlist(layer.legends))
@@ -675,16 +677,14 @@ gg2list <- function(p) {
675677
nann <- 1
676678
}
677679
annotations[[nann]] <- list(text=legend.title,
678-
x= layout$legend$x * 1.0154,
679-
y= layout$legend$y +
680-
0.5 * length(trace.list) * 0.10,
680+
x = layout$legend$x * 1.0154,
681+
y = 0.075 * 0.5* length(trace.list) + 0.55,
681682
showarrow=FALSE,
682683
xref="paper", yref="paper",
683-
xanchor="left", yanchor = "center",
684+
xanchor="left", yanchor = "top",
684685
textangle=0)
685686
layout$annotations <- annotations
686687
}
687-
688688
# Family font for text
689689
if (!is.null(theme.pars$text$family)) {
690690
layout$titlefont$family <- theme.pars$text$family

tests/testthat/test-ggplot-legend.R

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

0 commit comments

Comments
 (0)