如何在R中的交互式图形中以对数标度绘制轮廓? [英] How to plot contours with a log scale in an interactive graph in R?

查看:122
本文介绍了如何在R中的交互式图形中以对数标度绘制轮廓?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用plotly显示的等高线图:

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

这是结果:

问题:如何为z颜色使用对数刻度?还是如何手动控制轮廓级别?

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

我尝试了layout(p, zaxis = list(type = "log")),但是它不起作用.

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

注释#1:我不想绘制log(z),这是显而易见的解决方案,但要具有对数色标.

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

注释2::如果使用plotly无法实现,什么交互式图形库可以做到?我需要一个将图形导出到html文件的库.

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.

注释3:我尝试使用ggplot以对数比例绘制,然后使用ggplotly命令进行绘制,但最后,仅绘制log(z).

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

注释4:我尝试过使用热图,按照指示手动设置颜色比例

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

推荐答案

这是我最后想出的解决方法:

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

因此,想法是在ggplot2中手动设置色阶,然后使用ggplotly命令将其转换.

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

这是结果:

唯一剩下的问题是色标,它不是对数的.我可以轻松更改休息时间,但是我不知道如何更改颜色.我猜这是

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.

我尝试使用提供的解决方案此处,但是交互式图形不会在鼠标悬停时指示z的确切值,而是指示范围(例如"[0.3,1)"),这无济于事,并使其失去了对交互性的兴趣.

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!

这篇关于如何在R中的交互式图形中以对数标度绘制轮廓?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆