Black Lives Matter. Please consider donating to Black Girls Code today.

Custom colorbar range

How to set custom colorbar range from -40 to 40, even though the values are in smaller range?

if someone will need, just put
colorscale = list(c(0, “rgb(255, 0, 0)”), list(1, “rgb(0, 255, 0)”)),
cauto = F,
cmin = -40,
cmax = 40

1 Like

colorscale = list(c(0, “rgb(255, 0, 0)”), list(1, “rgb(0, 255, 0)”)) dosn’t change colors, why?

attach your code, otherwise it is hard to say

Dear Alex, here it is:

plot_ly(z = volcano,
surfacecolor = matrix(nrow = volcano, ncol = volcano, runif(80)),
type = “surface”,
showscale = T,
colorscale = list(c(0, “rgb(255, 0, 0)”), list(1, “rgb(0, 255, 0)”)),
cauto = F,
cmin = 0,
cmax = 1)

Thank you in advance!

For me it works like a charm. What exactly doesn’t work? Try to update plotly library. If it doesn’t work, send your session info and snapshot of resulting surface

Here it is… Thank you for advice, I’ll try with the library update…

I’ve uninstalled R and RStudio, installed R 3.3.1, RStudio 0.99.903 and only plotly package (install_github(“ropensci/plotly”)) but the problem remains… I use Windows 7.

Actually, if I change colorcale to any of default, such as ‘Blues’ or ‘Earth’, it displays only Bluered:

plot_ly(z = volcano,
surfacecolor = matrix(nrow = volcano, ncol = volcano, runif(80)),
type = “surface”,
showscale = T,
colorscale = “Blues”,
# colorscale = list(c(0, “rgb(255, 0, 0”), list(1, “rgb(0, 255, 0)”)),
cauto = F,
cmin = 0,
cmax = 1
)

May you then run

p = plot_ly(z = volcano,
surfacecolor = matrix(nrow = volcano, ncol = volcano, runif(80)),
type = “surface”,
showscale = T,
colorscale = list(c(0, “rgb(255, 0, 0)”), list(1, “rgb(0, 255, 0)”)),
cauto = F,
cmin = 0,
cmax = 1)

bld = plotly_build§

str(bld)

And show the output of last command( “str(bld)” )

List of 8
$ x :List of 5
…$ layout :List of 1
… …$ margin:List of 4
… … …$ b: num 40
… … …$ l: num 60
… … …$ t: num 25
… … …$ r: num 10
…$ config :List of 1
… …$ modeBarButtonsToRemove:Class ‘AsIs’ chr “sendDataToCloud”
…$ base_url: chr “https://plot.ly
…$ source : chr “A”
…$ data :List of 1
… …$ :List of 9
… … …$ colorbar :List of 2
… … … …$ title : chr “”
… … … …$ ticklen: num 2
… … …$ colorscale :‘data.frame’: 2 obs. of 3 variables:
… … … …$ : Factor w/ 2 levels “0”,“rgb(255, 0, 0)”: 1 2
… … … …$ : num [1:2] 1 1
… … … …$ : Factor w/ 1 level “rgb(0, 255, 0)”: 1 1
… … …$ showscale : logi TRUE
… … …$ z : num [1:87, 1:61] 100 101 102 103 104 105 105 106 107 108 …
… … …$ surfacecolor: num [1:100, 1:100] 0.789 0.934 0.617 0.832 0.34 …
… … …$ cauto : logi FALSE
… … …$ cmin : num 0
… … …$ cmax : num 1
… … …$ type : chr “surface”
…- attr(*, “TOJSON_FUNC”)=function (x, …)
$ width : NULL
$ height : NULL
$ sizingPolicy :List of 6
…$ defaultWidth : chr “100%”
…$ defaultHeight: num 400
…$ padding : NULL
…$ viewer :List of 6
… …$ defaultWidth : NULL
… …$ defaultHeight: NULL
… …$ padding : NULL
… …$ fill : logi TRUE
… …$ suppress : logi FALSE
… …$ paneHeight : NULL
…$ browser :List of 4
… …$ defaultWidth : NULL
… …$ defaultHeight: NULL
… …$ padding : NULL
… …$ fill : logi TRUE
…$ knitr :List of 3
… …$ defaultWidth : NULL
… …$ defaultHeight: NULL
… …$ figure : logi TRUE
$ dependencies : NULL
$ elementId : NULL
$ preRenderHook:function §
$ jsHooks : list()

  • attr(*, “class”)= chr [1:2] “plotly” “htmlwidget”
  • attr(*, “package”)= chr “plotly”

Ok, then what I would do. Remove both plotly and htmlwidgets packages. and reinstall them again. However, use plotly 3.4.13 from https://cran.r-project.org/src/contrib/Archive/plotly/ and htmlwidgets_0.6 from https://cran.r-project.org/src/contrib/Archive/htmlwidgets/

1 Like

Dear Alex, that’s it! :slight_smile: Thank you, very, very much! All the best!