Skip to content

Commit 18192a5

Browse files
committed
ignore alpha when comparing legend entries
1 parent 2e26cc0 commit 18192a5

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

R/ggplotly.R

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -760,13 +760,28 @@ gg2list <- function(p){
760760
merged.traces[[length(merged.traces)+1]] <- tr
761761
}
762762

763+
# -------------------------------
763764
# 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)
765+
# -------------------------------
766+
# remove alpha from a color entry
767+
rm_alpha <- function(x) {
768+
if (length(x) == 0) return(x)
769+
pat <- "^rgba\\("
770+
if (!grepl(pat, x)) return(x)
771+
sub(",\\s*[0]?[.]?[0-9]+\\)$", ")", sub(pat, "rgb(", x))
772+
}
773+
# convenient for extracting name/value of legend entries (ignoring alpha)
774+
entries <- function(x, y) {
775+
z <- try(x[[y]], silent = TRUE)
776+
if (inherits(e, "try-error")) {
777+
paste0(x$name, "-")
778+
} else {
779+
paste0(x$name, "-", rm_alpha(z))
780+
}
781+
}
782+
fill_set <- unlist(lapply(merged.traces, entries, "fillcolor"))
783+
line_set <- unlist(lapply(merged.traces, entries, c("line", "color")))
784+
mark_set <- unlist(lapply(merged.traces, entries, c("marker", "color")))
770785
legend_intersect <- function(x, y) {
771786
i <- intersect(x, y)
772787
# restrict intersection to valid legend entries

0 commit comments

Comments
 (0)