Skip to content

Commit b50a969

Browse files
committed
naive approach to avoiding redundant legends
1 parent 39b2546 commit b50a969

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

R/ggplotly.R

+27-2
Original file line numberDiff line numberDiff line change
@@ -598,13 +598,36 @@ gg2list <- function(p){
598598
layout$showlegend <- FALSE
599599
}
600600
}
601-
601+
602+
# avoid redundant legends
603+
fills <- lapply(trace.list, function(x) paste0(x$name, "-", x$fillcolor))
604+
linez <- lapply(trace.list, function(x) paste0(x$name, "-", x$line$color))
605+
marks <- lapply(trace.list, function(x) paste0(x$name, "-", x$marker$color))
606+
fill_set <- unlist(fills)
607+
line_set <- unlist(linez)
608+
mark_set <- unlist(marks)
609+
legend_intersect <- function(x, y) {
610+
i <- intersect(x, y)
611+
# restrict intersection to valid legend entries
612+
i[grepl("-rgb[a]?\\(", i)]
613+
}
614+
# if there is a mark & line legend, get rid of line
615+
t1 <- line_set %in% legend_intersect(mark_set, line_set)
616+
# if there is a mark & fill legend, get rid of fill
617+
t2 <- fill_set %in% legend_intersect(mark_set, fill_set)
618+
# if there is a line & fill legend, get rid of fill
619+
t3 <- fill_set %in% legend_intersect(line_set, fill_set)
620+
t <- t1 | t2 | t3
621+
for (m in seq_along(trace.list))
622+
if (trace.list[[m]]$showlegend && t[m])
623+
trace.list[[m]]$showlegend <- FALSE
624+
602625
# Only show a legend title if there is at least 1 trace with
603626
# showlegend=TRUE.
604627
trace.showlegend <- sapply(trace.list, "[[", "showlegend")
605628
if (any(trace.showlegend) && layout$showlegend && length(p$data)) {
606629
# Retrieve legend title
607-
legend.elements <- sapply(traces, "[[", "name")
630+
legend.elements <- unlist(sapply(traces, "[[", "name"))
608631
legend.title <- ""
609632
for (i in 1:ncol(p$data)) {
610633
if (all(legend.elements %in% unique(p$data[, i])))
@@ -629,6 +652,8 @@ gg2list <- function(p){
629652
layout$annotations <- annotations
630653
}
631654

655+
656+
632657
# Family font for text
633658
if (!is.null(theme.pars$text$family)) {
634659
layout$titlefont$family <- theme.pars$text$family

tests/testthat/test-ggplot-smooth.R

+26-2
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,37 @@ test_that("geom_smooth() respects group aesthetic", {
3636
})
3737

3838
p4 <- qplot(carat, price, colour = cut, data = d) + geom_smooth()
39+
p5 <- qplot(carat, price, data = d) + geom_smooth(aes(colour = cut))
3940

4041
test_that("geom_smooth() respects colour aesthetic", {
4142
info <- expect_traces(p4, 11, "colour")
43+
# number of showlegends should equal the number of factor levels
44+
n <- sum(unlist(sapply(info$traces, "[[", "showlegend")))
45+
expect_equal(n, nlevels(d$cut))
46+
info <- expect_traces(p5, 11, "colour2")
47+
n <- sum(unlist(sapply(info$traces, "[[", "showlegend")))
48+
expect_equal(n, nlevels(d$cut))
4249
})
4350

44-
p5 <- qplot(carat, price, fill = cut, data = d) + geom_smooth()
51+
# why are 5 traces for point being created here??
52+
#p6 <- qplot(carat, price, fill = cut, data = d) + geom_smooth()
53+
p7 <- qplot(carat, price, data = d) + geom_smooth(aes(fill = cut))
4554

4655
test_that("geom_smooth() respects fill aesthetic", {
47-
info <- expect_traces(p5, 11, "fill")
56+
# info <- expect_traces(p6, 11, "fill")
57+
# n <- sum(unlist(sapply(info$traces, "[[", "showlegend")))
58+
# expect_equal(n, nlevels(d$cut))
59+
info <- expect_traces(p7, 11, "fill2")
60+
n <- sum(unlist(sapply(info$traces, "[[", "showlegend")))
61+
expect_equal(n, nlevels(d$cut))
4862
})
63+
64+
# ensure legend is drawn when needed
65+
p8 <- qplot(carat, price, data = d) + facet_wrap(~cut) +
66+
geom_smooth(aes(colour = cut, fill = cut))
67+
68+
test_that("geom_smooth() works with facets", {
69+
# 3 traces for each panel
70+
info <- expect_traces(p8, 15, "fill2")
71+
})
72+

0 commit comments

Comments
 (0)