Colors mode doesn't work with a second added trace

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.

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

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

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
    )
  )

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.

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
    )
  )
2 Likes

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

1 Like

I’m having difficulty getting this to work with an sf (polygons) and scatter (points) map combined:

Code

 fig = plot_mapbox(
    ) %>% add_sf(
      data=districts,
      color=~agent.population,
      alpha=0.7,
      showlegend=TRUE
    ) %>% add_markers(
    # ) %>% add_trace(
      type='scatter',
      data=facilities,
      x=~longitude,
      y=~latitude, 
      color=~agent.calculatedprevalence,
      alpha=0.7,
      showlegend=TRUE
    ) %>% layout(
      mapbox=list(
        zoom=4,
        style='open-street-map'))

Warnings

Warning: line.color doesn't (yet) support data arrays
Warning: Only one fillcolor per trace allowed
Warning: line.color doesn't (yet) support data arrays
Warning: Only one fillcolor per trace allowed

Result

Interpretation

The legend looks correct to me. And actually even though the polygons are all 1 color (purple), that is actually what I expect. I just haven’t fixed it. This is occurring because all of the polygons are actually in the low thousands, which should yield the same color. I’ll fix that.

However, I was expecting the scatterplot map markers to have a legend and a coloration to reflect, but this is not happening.

Thanks for update and quick reply. I’ll be sure to keep an eye on this thread. Looking for the same issue.

www.rapidfs.com login