Skip to content

Commit 2e26cc0

Browse files
committed
Avoid redunant legends entries AFTER merging traces
1 parent aaf302e commit 2e26cc0

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

R/ggplotly.R

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -599,29 +599,6 @@ gg2list <- function(p){
599599
}
600600
}
601601

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-
625602
# Only show a legend title if there is at least 1 trace with
626603
# showlegend=TRUE.
627604
trace.showlegend <- sapply(trace.list, "[[", "showlegend")
@@ -652,8 +629,6 @@ gg2list <- function(p){
652629
layout$annotations <- annotations
653630
}
654631

655-
656-
657632
# Family font for text
658633
if (!is.null(theme.pars$text$family)) {
659634
layout$titlefont$family <- theme.pars$text$family
@@ -785,6 +760,31 @@ gg2list <- function(p){
785760
merged.traces[[length(merged.traces)+1]] <- tr
786761
}
787762

763+
# avoid redundant legends entries
764+
fills <- lapply(merged.traces, function(x) paste0(x$name, "-", x$fillcolor))
765+
linez <- lapply(merged.traces, function(x) paste0(x$name, "-", x$line$color))
766+
marks <- lapply(merged.traces, function(x) paste0(x$name, "-",x$marker$color))
767+
fill_set <- unlist(fills)
768+
line_set <- unlist(linez)
769+
mark_set <- unlist(marks)
770+
legend_intersect <- function(x, y) {
771+
i <- intersect(x, y)
772+
# restrict intersection to valid legend entries
773+
i[grepl("-rgb[a]?\\(", i)]
774+
}
775+
# if there is a mark & line legend, get rid of line
776+
t1 <- line_set %in% legend_intersect(mark_set, line_set)
777+
# that is, unless the mode is 'lines+markers'...
778+
t1 <- t1 & !(unlist(lapply(merged.traces, "[[", "mode")) %in% "lines+markers")
779+
# if there is a mark & fill legend, get rid of fill
780+
t2 <- fill_set %in% legend_intersect(mark_set, fill_set)
781+
# if there is a line & fill legend, get rid of fill
782+
t3 <- fill_set %in% legend_intersect(line_set, fill_set)
783+
t <- t1 | t2 | t3
784+
for (m in seq_along(merged.traces))
785+
if (isTRUE(merged.traces[[m]]$showlegend && t[m]))
786+
merged.traces[[m]]$showlegend <- FALSE
787+
788788
# Put the traces in correct order, according to any manually
789789
# specified scales. This seems to be repetitive with the trace$rank
790790
# attribute in layer2traces (which is useful for sorting traces that

0 commit comments

Comments
 (0)