6

I have a plot where I have first trace in gray that will be overplotted by other traces in colors. My issue is that in plotly-version 4.7.1. as well as in version 4.8.0. I can't adjust the color.

One year ago this code would work:

mysim=data.frame(x=rep(1:4,4),y=rbinom(16,10,0.5),id=rep(1:4,each=4))

my_colors<-c(             ## add the standard plotly colors
        '#1f77b4',  #// muted blue
         '#ff7f0e',  #// safety orange
         '#2ca02c',  #// cooked asparagus green
         '#d62728'  #// brick red
             ) 


plot_ly() %>%
 add_trace(x=1:4,y=rbinom(4,10,0.4),type='scatter',mode='lines',
            line=list(color='#CCCCCC',dash='dashed'),hoverinfo='skip',opacity=0.25) %>% 
  add_trace(data=mysim,x=~x,y=~y,type='scatter',mode='lines', split=~as.factor(id),
            line=list(color=my_colors),hoverinfo='skip',opacity=1) 

Sadly I do not have that machine anymore. But it seems that there were changes made to plotly since then. I also tried using the color argument instead of split and used colors instead of the line-list to specify the colors. It didn't have any impact. I still get this plot: enter image description here

What am I missing here? How can I make it work?

Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225
5th
  • 2,097
  • 3
  • 22
  • 41

1 Answers1

8

See this issue.

This works if you use color instead of split and if you set the colors in the plot_ly function at the beginning, with the argument colors:

plot_ly(colors=my_colors) %>%
  add_trace(x=1:4,y=rbinom(4,10,0.4),type='scatter',mode='lines', line=list(color='rgb(0,0,255)',dash='dashed'),hoverinfo='skip',opacity=0.25) %>% 
  add_trace(data=mysim,x=~x,y=~y,type='scatter',mode='lines', color=~as.factor(id),
            hoverinfo='skip',opacity=1) 
Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225
  • THIS!!! I cannot believe I only now saw this - it has fixed a problem I have had for over 2 years. In this way, the `symbol` can also be set in the following manner: `symbols = c("key1" = "symbol1", "key2" = "symbol2")`. The same can be done for colors such that `colors = c("key1" = "color1", "key2" = "color2")`. This is needed when the order of the colors changes in for loops with lots of `add_trace`s. More can be seen here: https://github.com/plotly/plotly.R/issues/790#issuecomment-931292330 – Martin Sep 30 '21 at 13:27