如何解决在曲面上找到轮廓的确切坐标并在 R 中手动绘制
我正在绘制曲面图并想使用 plotly
“手动”绘制轮廓线。在下面的代码中我:
- 模拟绘制曲面图的数据
- 使用
contoureR
包计算特定 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,) %>%
add_trace(
type = "scatter3d",x = r[[1]]$x,y = r[[1]]$y,z = z_level
)
我知道这些都是近似值,因此我还尝试将 x
生成的 y
和 contourLines()
坐标传递给用于创建上述 z1
的公式并使用相应的值绘制我的等高线(而不是使用 z_level = 5
,但我仍然没有获得所需的结果:
plot_ly(
x = x,type = "surface"
) %>%
add_trace(
type = "scatter3d",z = r[[1]]$x^0.2*r[[1]]$y^0.3
)
我也知道 plotly
使我能够绘制特定的轮廓线(请参阅我的问答:Add a permanent contour line to a surface plot in R plotly)。但是,我想自己绘制轮廓线(在获取它们的坐标后),以便它可以通过光标“拉”并在我将鼠标悬停在其上时向我显示工具提示信息。理想情况下,如果有一种方法可以获得由 plotly
本身计算的轮廓线坐标,那就太好了。
感谢您的帮助。
解决方法
我找到了两个解决这个问题的方法。
方案一:转置z1
矩阵
@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
)
解决方案 2:使用 isoband
包
第二种解决方案是用isoband::isolines()
函数计算等高线坐标:
library(plotly) # for interactive visualizations
library(isoband) # for find contour lines coordinates
# Simulate the data for plotting
x <- y <- seq(from = 0,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,) %>%
add_trace(
type = "scatter3d",z = z_level
)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。