plotly / plotly/plotly.R

ggplotly() creates a wrong legend in some cases

Open
#2,420 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
R
Stars
2.7k
Forks
641
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the reproducible R example from the issue and compare the ggplot2 and ggplotly legends. Investigate the ggplotly conversion of manual colour scales and verify that the converted legend matches the declared Left and Center labels without extra entries.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
data-visualization
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.