Skip to content

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

Description

@Noskario

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

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