plotly.R icon indicating copy to clipboard operation
plotly.R copied to clipboard

ggplotly() creates a wrong legend in some cases

Open aloboa opened this issue 11 months ago • 2 comments

Given

library(ggplot2)
library(plotly)
d1 <- data.frame(X=1:10, Y= (1:10)^2, b=paste0("B",1:10),d="d1")
d1$Y2 <- d1$Y+2
gg2 <- ggplot(data=d1, aes(label=b)) +
  geom_point(aes(x=X, y=Y, col="blue")) +
  geom_line(aes(x=X, y=Y2, col="orange")) +
    scale_color_manual(values=c("blue","orange"),
                       labels=c("Left","Center"),
                       name="") 

print(gg2) results in:

Image

But ggplotly(gg2)

results in:

Image

I know a more conventional code such as:

d1 <- data.frame(X=1:10, Y= (1:10)^2, b=paste0("B",1:10),position="left")
d2 <- data.frame(X=1:10, Y= (1:10)^2 + 2, b=paste0("B",1:10),position="center")
d <- rbind(d1,d2)
gg2 <- ggplot(data=d, aes(label=b)) +
  geom_point(data=d[d$position=="left",],aes(x=X, y=Y,col=position)) +
  geom_line(data=d[d$position=="center",],aes(x=X, y=Y,col=position)) +
  scale_color_manual(values=c("blue","orange","red"),
                     labels=c("Left","Center"),
                     name="") 
gg2
ggplotly(gg2)

results in similar legends, but I wonder if the 1st example is not uncovering a bug in ggplotly()

Image

aloboa avatar Feb 02 '25 09:02 aloboa

Your more conventional code block appears identical to the first block, except in the first one you refer to gg2 variable and print gg... is that a mistake on your end?

The label=b generates a warning "Ignoring unknown aesthetics: label".

The "conventional" code block works.

Can you revise your first code block and provide code that actually fails for you? It appears to me that you accidentally copied the functional code twice.

romanzenka avatar Feb 21 '25 17:02 romanzenka

Excuse me, I had made a wrong reproducible example. I have edited the initial example.

aloboa avatar Feb 25 '25 09:02 aloboa