0
votes

The label colors on my plotly 3d scatter charts do not match the marker colors: enter image description here

All labels end up being the same color with the following minimal example:

plot_ly(
     data      = mtcars,
     type      = "scatter3d",
     mode      = "markers",
     colors    = c("blue","red"),
     text      = row.names(mtcars),
     hoverinfo = "text",
     color     = ~cyl,
     x         = ~disp,
     y         = ~hp,
     z         = ~qsec
)

With 2D scatter plots, the labels are colored appropriately: enter image description here enter image description here

Which is achieved using:

plot_ly(
     data      = mtcars,
     type      = "scatter",
     mode      = "markers",
     colors    = c("blue","red"),
     text      = row.names(mtcars),
     hoverinfo = "text",
     color     = ~cyl,
     x         = ~disp,
     y         = ~hp
)

I know a color range for the legend isn't appropriate in this example, but it's needed for the actual data I would use with this code.

1

1 Answers

0
votes

Well, it seems that if you set color = ~as.factor(cyl) it works correctly.

But I really don't have idea why.

The code:

plot_ly(
  data      = mtcars,
  type      = "scatter3d",
  mode      = "markers",
  colors    = ~c("blue","red"),
  text      = row.names(mtcars),
  hoverinfo = "text",
  color     = ~as.factor(cyl),
  x         = ~disp,
  y         = ~hp,
  z         = ~qsec
)

The output:

enter image description here