Custom color range in r plot_ly scatter

Viewed 314

I am struggling with setting a custom range / cutoff for the colour values in a plot_ly scatter plot.

data(mtcars)

plot_tmp <- plot_ly(data = mtcars, 
                    marker = list(size=10, colorbar = "Hot"),
                    hovertext = ~mtcars[,3],
                    type = "scatter",
                    x = ~mtcars[,1], 
                    y = ~mtcars[,2], 
                    color = ~mtcars[,3]) 

plot_tmp

Plot example

I tried using colorbar(limits = c(3,5)), but this only excludes everything from beeing coloured.

plot_tmp %>% colorbar(limits = c(250,350))

Plot using "limits"

The desired output would be to set everything e.g. over the limit to the same value.

1 Answers

After a lot of reading in the manual I was able to come up with a working solution:

plot_tmp <- plot_ly(
  data = mtcars,
  x = ~ mtcars[, 1],
  y = ~ mtcars[, 2],
  mode = "markers",
  type = "scatter",
  marker = list(
    size = "3em",
    color = ~ mtcars[, 3],
    cauto = FALSE,
    colorbar = list(title = "Scaled Expression"),
    cmin = 250,
    cmax = 280
  ),
  hovertext = ~ mtcars[, 3]
)
plot_tmp

The important thing is specifying the color, cmin and cmax argument inside of the list of "marker". If you don't specify the color inside, the scale bar will not be updated according to your min and max values

Related