在表面上找到轮廓的精确坐标并在 R 中手动绘制
Find the exact coordinates of a contour on a surface and plot it manually in R plotly
我正在绘制曲面图并想使用 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,
y = y,
z = z1,
) %>%
add_trace(
type = "scatter3d",
x = r[[1]]$x,
y = r[[1]]$y,
z = z_level
)
我知道这些都是近似值,所以我也尝试将contourLines()
产生的x
和y
坐标传递给用于创建[=17=的公式]上面并使用相应的值绘制我的等高线(而不是使用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
)
我也知道 plotly
使我能够绘制特定的轮廓线(请在此处查看我的问题和答案:)。但是,我想自己绘制轮廓线(在获得坐标后),以便它可以通过光标“拉动”并在我将鼠标悬停在它上面时向我显示工具提示信息。理想情况下,如果有一种方法可以获得 plotly
本身计算的等高线坐标,那就太好了。
感谢您的帮助。
我找到了解决这个问题的两个方法。
解决方案 1:转置 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, 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
)
我正在绘制曲面图并想使用 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,
y = y,
z = z1,
) %>%
add_trace(
type = "scatter3d",
x = r[[1]]$x,
y = r[[1]]$y,
z = z_level
)
我知道这些都是近似值,所以我也尝试将contourLines()
产生的x
和y
坐标传递给用于创建[=17=的公式]上面并使用相应的值绘制我的等高线(而不是使用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
)
我也知道 plotly
使我能够绘制特定的轮廓线(请在此处查看我的问题和答案:plotly
本身计算的等高线坐标,那就太好了。
感谢您的帮助。
我找到了解决这个问题的两个方法。
解决方案 1:转置 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, 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
)