向 R 中的曲面图添加永久等高线 plotly
Add a permanent contour line to a surface plot in R plotly
我正在使用 R 中的 plotly
包绘制曲面图和等高线图:
# Load package
library(plotly)
# 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
# Draw surface plot (3D)
plotly::plot_ly(z = z1) %>%
plotly::add_surface(
contours = list(
z = list(
show = TRUE,
usecolormap = TRUE,
highlightcolor = "#ff0000",
project = list(z = TRUE)
)
)
)
# Draw contour plot (2D)
plot_ly(z = z1) %>%
add_contour()
两个图的渲染包含等高线,显示了 x
和 y
的组合,产生了一个恒定的 z
水平。在 3D 曲面图的情况下,将鼠标悬停在其上会动态绘制等高线,其中鼠标在 3 维 space.[=17= 的一侧投影等高线。 ]
我想做的是通过自己指定 z
的值(例如 z = 5
)在两个地块上手动绘制一条或多条等高线。
感谢@IRTFM 的评论,我能够找到问题的答案。答案更具体到等高线图。用户需要将 contours
参数下的 start
和 end
属性设置为所需的 z
值。下面,我绘制了 z = 5
:
的等高线
# Load package
library(plotly)
# 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
plot_ly(
z = z1,
type = "contour",
autocontour = FALSE,
colorscale = "RdBu",
contours = list(
start = 5,
end = 5,
showlabels = TRUE,
showlines = TRUE,
coloring = "lines"
)
) %>%
hide_guides()
如果想要绘制 2 条特定的等高线,他们需要一个额外的参数:ncontours = 2
。不这样做可能会导致绘制超过 2 条等高线 - z
值介于 start
和 end
之间的线。
plot_ly(
z = z1,
type = "contour",
autocontour = FALSE,
ncontours = 2,
colorscale = "RdBu",
contours = list(
start = 5,
end = 7,
showlabels = TRUE,
showlines = TRUE,
coloring = "lines"
)
) %>%
hide_guides()
最后,对于2条以上的特定轮廓线,我们需要使用add_contour()
函数。让我们绘制 4 个特定的等高线图:
plot_ly(
z = z1,
type = "contour",
autocontour = FALSE,
ncontours = 2,
colorscale = "RdBu",
contours = list(
start = 5,
end = 7,
showlabels = TRUE,
showlines = TRUE,
coloring = "lines"
)
) %>%
add_contour(
ncontours = 2,
contours = list(
start = 4,
end = 6,
showlabels = TRUE,
showlines = TRUE,
coloring = "lines"
)
) %>%
hide_guides()
我正在使用 R 中的 plotly
包绘制曲面图和等高线图:
# Load package
library(plotly)
# 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
# Draw surface plot (3D)
plotly::plot_ly(z = z1) %>%
plotly::add_surface(
contours = list(
z = list(
show = TRUE,
usecolormap = TRUE,
highlightcolor = "#ff0000",
project = list(z = TRUE)
)
)
)
# Draw contour plot (2D)
plot_ly(z = z1) %>%
add_contour()
两个图的渲染包含等高线,显示了 x
和 y
的组合,产生了一个恒定的 z
水平。在 3D 曲面图的情况下,将鼠标悬停在其上会动态绘制等高线,其中鼠标在 3 维 space.[=17= 的一侧投影等高线。 ]
我想做的是通过自己指定 z
的值(例如 z = 5
)在两个地块上手动绘制一条或多条等高线。
感谢@IRTFM 的评论,我能够找到问题的答案。答案更具体到等高线图。用户需要将 contours
参数下的 start
和 end
属性设置为所需的 z
值。下面,我绘制了 z = 5
:
# Load package
library(plotly)
# 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
plot_ly(
z = z1,
type = "contour",
autocontour = FALSE,
colorscale = "RdBu",
contours = list(
start = 5,
end = 5,
showlabels = TRUE,
showlines = TRUE,
coloring = "lines"
)
) %>%
hide_guides()
如果想要绘制 2 条特定的等高线,他们需要一个额外的参数:ncontours = 2
。不这样做可能会导致绘制超过 2 条等高线 - z
值介于 start
和 end
之间的线。
plot_ly(
z = z1,
type = "contour",
autocontour = FALSE,
ncontours = 2,
colorscale = "RdBu",
contours = list(
start = 5,
end = 7,
showlabels = TRUE,
showlines = TRUE,
coloring = "lines"
)
) %>%
hide_guides()
最后,对于2条以上的特定轮廓线,我们需要使用add_contour()
函数。让我们绘制 4 个特定的等高线图:
plot_ly(
z = z1,
type = "contour",
autocontour = FALSE,
ncontours = 2,
colorscale = "RdBu",
contours = list(
start = 5,
end = 7,
showlabels = TRUE,
showlines = TRUE,
coloring = "lines"
)
) %>%
add_contour(
ncontours = 2,
contours = list(
start = 4,
end = 6,
showlabels = TRUE,
showlines = TRUE,
coloring = "lines"
)
) %>%
hide_guides()