Mastering Development

Separate colour mappings in R plotly

I’ve got two layers of markers for which I’d like to use separate colour mappings. The problem is that the second add_marker inherits the palette from the first one. Is there anything like ggnewscale::new_scale_color() in native plotly?


df = data.frame(x = runif(10),
                y = runif(10),
                m = runif(10),
                b = round(runif(10)))

plot_ly(data = df,
        x = ~x,
        y = ~y) %>%
  add_markers(color = ~m,
              colors = viridisLite::viridis(10),
              size = I(100)) %>%
  add_markers(color = ~factor(b),
              colors = c(`0` = "black",
                         `1` = "red"))

Leave a Reply

Your email address will not be published. Required fields are marked *