Colors mode doesn't work with a second added trace


#1

I have a script like this:

colori_BPS <- c("#1289d7", "#00008b")
colori_BPS <- setNames(colori_BPS, levels(dataframe_Brewer_alternativo_3$tipo_BPS))
colori_O3Brewer <- c("#ff0000", "#ffa500")
colori_O3Brewer <- setNames(colori_BPS, levels(dataframe_Brewer_alternativo_3$tipo_O3Brewer))

O3_Brewer_BPS_periodi_plot_2 <- plot_ly(data = dataframe_Brewer_alternativo_3,
                                      x = ~Date,
                                      y = ~O3_BPS,
                                      color = ~tipo_BPS,
                                      colors = colori_BPS,
                                      type = "scatter",
                                      mode="markers",
                                      marker=list(size = 5
                                              ),
                                      #name ="BPS ('Good')",
                                      hoverinfo = "text",
                                      text = paste("Date = ", dataframe_Brewer_alternativo_3$Date, "O3_BPS(DU) = ", dataframe_Brewer_alternativo_3$O3_BPS)
)

O3_Brewer_BPS_periodi_plot_2 <- add_trace(O3_Brewer_BPS_periodi_plot_2,
                                        x = ~Date,
                                        y = ~O3,
                                        type = "scatter",
                                        color = ~tipo_O3Brewer,
                                        colors = colori_O3Brewer,
                                        mode = "markers",
                                        marker = list(size = 4
                                        ),
                                        #name ="BPS ('Anomalous')",
                                        hoverinfo = "text",
                                        text = paste("Date = ", dataframe_Brewer_alternativo_3$Date, "O3_Brewer(DU) = ", dataframe_Brewer_alternativo_3$O3)
)

for both the traces I used the mode “color” and “colors” to costumize the colors of the sub-traces but when I call the plot it doesn’t work and it gives me this warning:

In colScale(as.character(color[[i]])) :
  Some values were outside the color scale and will be treated as NA

if I comment the lines with the “colors” command the plot is done but the software chooses the colors.

Is there any way to costumize the colors of the sub-traces?
thanks.


#2

Hey @frank_ska

Hard to say without a reproducible example. But perhaps try inherit = F in the 2nd trace. Alternatively, you can inspect the JSON to see what is going on, plotly_json(). Furthermore, you can make changes via plotly_build(), something like:

p <- plotly_build(p)

p$x$data[[1]]$marker$color <- new_color

#3

thanks for the answer bcd, you’re right and I send you a reproducible example:

library(plotly)

a <- c(1,2,3,4,5,6,7,8,9,10)
b <- c(rep("mode_1", 5), rep("mode_2", 5))
b <- as.factor(b)
c <- c(10,9,8,7,6,5,4,3,2,1)
d <- c(rep("mode_3", 3), rep("mode_4", 7))
d <- as.factor(d)

dataf <- data.frame(a,b,c,d)

color_a <- c("#1289d7", "#00008b")
color_a <- setNames(color_a, levels(dataf$b))
color_b <- c("#ff0000", "#ffa500")
color_b <- setNames(color_b, levels(dataf$d))

p <- plot_ly(data = dataf,
              x = ~a,
              y = ~a,
              color = ~b,
              colors = color_a,
              type = "scatter",
              mode="markers",
              marker=list(size = 5
              )
)

p <- add_trace(p,
             x = ~a,
             y = ~c,
             color = ~d,
             colors = color_b,
             type = "scatter",
             mode="markers",
             marker=list(size = 5
             )
)

Unfortunately the “inherit” method doesn’t work and before trying the “plotly_build()” one I’d prefere to know if there is a more intuitive way to set the colors.
Thanks


#4

try placing it outside of the plot_ly() function:

p <- dataf %>% 
  plot_ly(type = 'scatter') %>%
  add_markers(
    x = ~a,
    y = ~a,
    color = ~b,
    colors = color_a,
    marker=list(
      size = 5
    )
  ) %>%
  add_markers(
    x = ~a,
    y = ~c,
    color = ~d,
    colors = color_b,
    marker=list(
      size = 5
    )
  )

#5

With this method the plot is correctly done but the colors shown are not the ones choosen in “color_a” and “color_b”. they are automatically selected.


#6

Ahh I see what you mean. Try calling the colors in the plot_ly() function:

pal <- c("#1289d7", "#00008b", "#ff0000", "#ffa500")

p <- dataf %>% 
  plot_ly(type = 'scatter', colors = pal) %>%
  add_markers(
    x = ~a,
    y = ~a,
    color = ~b,
    marker=list(
      size = 5
    )
  ) %>%
  add_markers(
    x = ~a,
    y = ~c,
    color = ~d,
    marker=list(
      size = 5
    )
  )

#7

Perfect. so the point is to create a unique “colors” vector for both the traces. Thank you.