plotly / plotly/plotly.R

Data gets dropped in add_trace and consequently points have the wrong color if NA is present

Open
#2,497 3 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

Have a look at the following code:

library(plotly)
x = c(NA, 2, 3, 4, 5)
color = c(0, 0, 0.5, 1, 1)
# If you try to plot it this way, you get mismatched colors. The NA seems to have been silently dropped and then the wrong indices aligned. The colors of points 2 and 3 are now blue, of 4 is purple and of 5 is green.
plotly::plot_ly()|>
    plotly::add_trace(
      type = "scatter",
      mode = "markers",
      x = x,
      y = 0,
      marker = list(
        color = color,
        cmin = 0,
        cmax = 1,
        colorscale = list(list(0, "blue"), list(0.5, "purple"), list(1, "green"))
      )
    ) |>
    plotly::layout(
      showlegend = FALSE
    ) |>
    plotly::hide_colorbar() 

It produces a plot with wrong colors:

Image

I think this is unacceptable behavior, especially since this is not what happens if you do it in the what I consider the classical way:

data.frame(x=x, color=color) |> 
  plot_ly(x=~x, color=~color, y=0, colors = c("blue", "purple", "green")) |> 
  hide_colorbar()
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

Run the supplied R example through plotly::add_trace and compare its marker colors with the data.frame example. Trace how add_trace handles NA values in x and aligns the marker color vector, then add regression coverage showing that non-NA points retain their corresponding colors.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
data-visualization
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
72/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.