簡體   English   中英

在 R plotly 中將永久輪廓線添加到曲面 plot

[英]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:

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

兩個圖的渲染包含等高線,它顯示了xy的組合,這些組合產生了一個恆定的z水平。 在 3D 表面 plot 的情況下,將鼠標懸停在其上會動態繪制一條等高線,其中鼠標在 3 維空間的一側具有等高線的投影。

我想做的是通過自己指定z的值(例如z = 5 )在兩個圖上手動繪制一條或多條等高線。

感謝@IRTFM 的評論,我能夠找到我的問題的答案。 答案更具體到等高線圖。 用戶需要將contours參數下的startend屬性設置為所需的z值。 下面,我 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()

等高線 z = 5

如果想要 plot 2 條特定的等高線,他們需要一個額外的參數: ncontours = 2 不這樣做可能會導致繪制超過 2 條等高線 - z值介於startend之間的線。

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

兩條等高線,z=5 和 z=7

最后,對於超過 2 條特定的等高線,我們需要使用add_contour() function。 讓我們 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM