[英]Find the exact coordinates of a contour on a surface and plot it manually in R plotly
I am drawing a surface plot and would like to "manually" draw a contour line using plotly
.我正在绘制表面 plot 并想“手动”使用plotly
绘制轮廓线。 In the code below I:在下面的代码中我:
contoureR
package使用contoureR
仪 package 计算特定 z 水平的等高线坐标# Load packages
library(plotly) # for interactive visualizations
library(contoureR) # for calculating contour coordinates
# Simulate the data for plotting
x <- y <- seq(from = 0, to = 100, by = 1)
z1 <- outer(X = x, Y = y, FUN = function(x, y) x^0.2 * y^0.3) # data for surface plot
# Obtain coordinates of contour for z = 5
z_level <- 5
r <- contourLines(x = x, y = y, z = z1, levels = z_level)
plot_ly(
type = "surface",
x = x,
y = y,
z = z1,
) %>%
add_trace(
type = "scatter3d",
x = r[[1]]$x,
y = r[[1]]$y,
z = z_level
)
I am aware that these are all approximations, so I also tried to pass the x
and y
coordinates produced by contourLines()
to the formula used to create z1
above and use the corresponding values to plot my contour line (instead of using z_level = 5
, but I still do not obtain the desired result:我知道这些都是近似值,因此我还尝试将由contourLines()
生成的x
和y
坐标传递给用于在上面创建z1
的公式,并将相应的值用于 plot 我的等高线(而不是使用z_level = 5
,但我仍然没有得到想要的结果:
plot_ly(
x = x,
y = y,
z = z1,
type = "surface"
) %>%
add_trace(
type = "scatter3d",
x = r[[1]]$x,
y = r[[1]]$y,
z = r[[1]]$x^0.2*r[[1]]$y^0.3
)
I alo know that plotly
enables me to draw specific contour lines (see my question and answer here: Add a permanent contour line to a surface plot in R plotly ). I alo know that plotly
enables me to draw specific contour lines (see my question and answer here: Add a permanent contour line to a surface plot in R plotly ). However, I would like to draw my contour line myself (after getting their coordinates) so it can "pull" by cursor and show me the tooltip info whenever I hover over it.但是,我想自己绘制我的轮廓线(在获得他们的坐标之后),这样它就可以通过 cursor “拉”,并在我 hover 超过它时向我显示工具提示信息。 Ideally, if there was a way to obtain the contour lines coordinates as computed by plotly
itself, that would be great.理想情况下,如果有办法获得由plotly
本身计算的等高线坐标,那就太好了。
Thank you for your help.谢谢您的帮助。
I was able to find two solutions to this problem.我能够找到解决此问题的两种方法。
z1
matrix解决方案1:转置z1
矩阵The first solution was given me by @nirgrahamuk and it consists in transposing the z1
matrix: @nirgrahamuk 给了我第一个解决方案,它包括转置z1
矩阵:
library(plotly) # for interactive visualizations
# Simulate the data for plotting
x <- y <- seq(from = 0, to = 100, by = 1)
z1 <- outer(X = x, Y = y, FUN = function(x, y) x^0.2 * y^0.3) # data for surface plot
# Obtain coordinates of contour for z = 5
z_level <- 6
r <- contourLines(x = x,
y = y,
z = z1,
levels = z_level)
plot_ly(
type = "surface",
z = t(z1), # *** WE TRANSPOSE THE MATRIX HERE! ***
) %>%
add_trace(
type = "scatter3d",
x = r[[1]]$x,
y = r[[1]]$y,
z = z_level
)
isoband
package解决方案2:使用isoband
package The second solution is to compute the contour lines coordinates with the isoband::isolines()
function:第二种解决方案是使用isoband::isolines()
function 计算等高线坐标:
library(plotly) # for interactive visualizations
library(isoband) # for find contour lines coordinates
# Simulate the data for plotting
x <- y <- seq(from = 0, to = 100, by = 1)
z1 <- outer(X = x, Y = y, FUN = function(x, y) x^0.2 * y^0.3) # data for surface plot
# Obtain coordinates of contour for z = 5
z_level <- 6
r <- isolines(x = x, # *** WE USE THE isolines() FUNCTION HERE ***
y = y,
z = z1,
levels = z_level)
plot_ly(
type = "surface",
z = z1,
) %>%
add_trace(
type = "scatter3d",
x = r[[1]]$x,
y = r[[1]]$y,
z = z_level
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.