plotly / plotly/plotly.R

`colors` argument to `add_trace` is only respected if the trace type *isn't* specified in the `plot_ly` call

Open
#2,474 1 comment 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

I'd expect these two lines of code to produce the same output, but only the first does what I expect (uses the colors argument to pick a palette for the points):

plot_ly(data = iris) %>%
  add_trace(x = ~Sepal.Length, y = ~Sepal.Width, color = ~Species, colors = "Blues", mode = "markers")
Image
plot_ly(data = iris, type="scatter") %>%
  add_trace(x = ~Sepal.Length, y = ~Sepal.Width, color = ~Species, colors = "Blues", mode = "markers")
Image

This seems backwards since I added the type="scatter" to avoid the warning about guessing the trace type, only to have my palette disappear.

Curiously, this behavior isn't present if everything is specified in the same call:

plot_ly(data = iris, x = ~Sepal.Length, y = ~Sepal.Width, color = ~Species, colors = "Blues")
plot_ly(data = iris, x = ~Sepal.Length, y = ~Sepal.Width, color = ~Species, colors = "Blues", type="scatter")

both return

Image

Is colors the correct way to pass a palette to a specific layer? In other places it seems like it's actually colorspace? I know scatter doesn't have a colors argument in schema() (e.g. #1699) but clearly it's working when the type is inferred and I would like to provide a palette...

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

Reproduce the two plot_ly/add_trace examples with iris and compare the generated palettes. Trace color handling through the plot_ly and add_trace entry points, especially the explicit type="scatter" path. Done means both call forms honor colors="Blues" consistently, with regression coverage for the differing examples.

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
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.