Skip to content

ggplotly() creates a wrong legend in some cases #2420

Open
@aloboa

Description

@aloboa

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions