5

In R, how do I set the default color palette for all plotly plots? I know in plot_ly() you can set colors=palette, but that works only for scatter plots and not for line plots. For line plots you have to set the colors for each trace individually.

5th
  • 2,097
  • 3
  • 22
  • 41
Geyer Bisschoff
  • 221
  • 2
  • 9

1 Answers1

2

Apparently in the current plotly-version (4.8.0) this feature exists. You can set a color palette by using the colors-argument inside the plot_ly()-wrapper. However this only works if you use the color-argument in each add_trace.

my_palette=c('#0099FF','#00FF99') # create my palette
plot_ly(colors=my_palette) %>%    ## set the palette
  add_trace(x=1:4,y=rbinom(4,10,0.4),type='scatter',mode='lines',hoverinfo='skip',color=as.factor('my first trace')) %>% 
  add_trace(x=1:4,y=rbinom(4,10,0.5),type='scatter',mode='lines',hoverinfo='skip',color=as.factor('the second'))

Since the color-functionality is fairly new in plotly I am optimistic that this functionality will continue to exist in future package versions (>4.8.0.).

I only found your question over my own question and it turns out that both our problems have a similar solution

5th
  • 2,097
  • 3
  • 22
  • 41