将永久轮廓线添加到R中的曲面图上 [英] Add a permanent contour line to a surface plot in R plotly

查看:71
本文介绍了将永久轮廓线添加到R中的曲面图上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在R中使用 plotly 包来绘制表面图和轮廓图:

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()

两个图的渲染包含轮廓线,轮廓线显示了 x y 的组合,产生了恒定的 z 水平.如果是3D表面图,则将鼠标悬停在其上会动态绘制轮廓线,其中鼠标所在的位置是轮廓线在3维空间的一侧上的投影.

The rendering of the two plots contains contour lines, which show the combinations of x and y that yield a constant z level. 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.

我想做的是通过为我自己指定 z 的值(例如, z = 5 )在两条曲线上手动绘制一条或多条轮廓线.

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 (e.g. z = 5).

推荐答案

由于@IRTFM的评论,我得以找到问题的答案.答案是更具体地针对等高线图.用户需要将 contours 参数下的 start end 属性设置为所需的 z 值.下面,我绘制了 z = 5 的轮廓线:

Thanks to @IRTFM's comment, I was able to find an answer to my question. 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. Below, I plot the contour line for 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 之间.

In case one wants to plot 2 specific contour lines, they need an additional argument: 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.

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个特定的等高线图:

Finally, for more than 2 specific contour lines, we need to use the add_contour() function. Let's plot 4 specific contour plots:

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中的曲面图上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆