1

I have a contour plot displayed with plotly:

x <- rep(seq(0.01, 0.1, length.out = 50), 50)
y <- rep(seq(0.01, 0.1, length.out = 50), each = 50)
z <- 1 / (x^2 + y^2)

my.data <- data.frame(x, y, z)

p <- plot_ly(data = my.data, x = x, y = y, z = z, type = "contour")

Here is the result:

No log scale

Question: How to use a log scale for the z colors? Or how to manually control the contour levels?

I tried layout(p, zaxis = list(type = "log")) but it does not work.

Remark #1: I don't want to plot log(z), which is the obvious solution, but to have a log color scale.

Remark #2: if it's not possible with plotly, what interactive graph library can do it? I need a library that exports my graph to an html file.

Remark #3: I tried plotting with ggplot in log scale and then using the ggplotly command, but in the end, plotly just plots log(z).

Remark #4: I tried with heat maps, manually setting the colour scale as indicated here, but could not make it work.

Ben
  • 6,321
  • 9
  • 40
  • 76

1 Answers1

1

Here is the workaround I finally came up with:

ggplot(my.data, aes(x = x, y = y, fill = z)) +
  geom_tile() +
  scale_fill_gradientn(colours = c("blue",
                                   "cyan",
                                   "green",
                                   "yellow",
                                   "orange",
                                   "red"),
                       values = rescale(c(0.3, 1, 2, 5, 10, 20, 35))) +
  scale_x_continuous(expand = c(0, 0)) +
  scale_y_continuous(expand = c(0, 0))

ggplotly()

So the idea was to manually set the color scale in ggplot2 and then convert it with ggplotly command.

Here is the result:

Better

The only remaining problem is the color scale, which is not logarithmic. I can easily change the breaks, but I don't know how to change the colors. I guess this is the subject of another question...

Another critic of this answer is that I don't have the discrete colors on the graph that I had with plotly, but rather a continuous gradient.

I tried using the solution provided here, but then the interactive graph does not indicate the exact value of z on mouse hover, but the range (e.g. "[0.3, 1)"), which is not helping and makes it lose the interest of the interactivity.

Alternate solutions are thus welcome!

Community
  • 1
  • 1
Ben
  • 6,321
  • 9
  • 40
  • 76