[英]Add a permanent contour line to a surface plot in R plotly
I am using the plotly
package in R to draw a surface plot and a contour plot: I am using the plotly
package in R to draw a surface plot and a contour plot:
# 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()
The rendering of the two plots contains contour lines, which show the combinations of x
and y
that yield a constant z
level.两个图的渲染包含等高线,它显示了x
和y
的组合,这些组合产生了一个恒定的z
水平。 In the case of the 3D surface plot, hovering the mouse over it dynamically draws a contour line where the mouse is with a projection of the contour line on one of the sides of the 3-dimensional space.在 3D 表面 plot 的情况下,将鼠标悬停在其上会动态绘制一条等高线,其中鼠标在 3 维空间的一侧具有等高线的投影。
What I would like to do is to manually draw one or multiple contour lines on the two plots by specifying a value for z
myself (eg z = 5
).我想做的是通过自己指定z
的值(例如z = 5
)在两个图上手动绘制一条或多条等高线。
Thanks to @IRTFM's comment, I was able to find an answer to my question.感谢@IRTFM 的评论,我能够找到我的问题的答案。 The answer is more specific to contour plots.答案更具体到等高线图。 The user needs to set the start
and end
attributes under the contours
argument to the desired z
value.用户需要将contours
参数下的start
和end
属性设置为所需的z
值。 Below, I plot the contour line for z = 5
:下面,我 plot 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()
In case one wants to plot 2 specific contour lines, they need an additional argument: ncontours = 2
.如果想要 plot 2 条特定的等高线,他们需要一个额外的参数: ncontours = 2
。 Not doing so will likely lead to the plotting of more than 2 contour lines - the lines for which the z
value is between start
and end
.不这样做可能会导致绘制超过 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()
Finally, for more than 2 specific contour lines, we need to use the add_contour()
function.最后,对于超过 2 条特定的等高线,我们需要使用add_contour()
function。 Let's plot 4 specific contour plots:让我们 plot 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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.