簡體   English   中英

如何使用 plot_ly function 在 r 的輪廓 plot 中添加網格線?

[英]How to add gridlines in a contour plot in r using plot_ly function?

Using r, I'm trying to find (1) How do I add gridlines to this contour plot example using the Plot_ly function in r? (2) 如何控制網格線之間的刻度間隔數?

x1 <- -10:10
y1 <- -10:10
z1 <- sqrt(outer(x1 ^ 2, y1 ^ 2, "+"))
fig <- plot_ly(z = z1, x = x1, y = y1,  type = "contour", 
                line = list(width = 1.5, color = "black"),
               contours = list(showlabels = TRUE))
fig

輪廓 plot 示例

Plotly 總是將跡線放在網格線的頂部,所以當整個 plot 被填充時,你對網格線做什么並不重要。

或者,您可以使用shapes來模仿網格線。

這是 y 軸模擬網格線的示例。

hlines <- function(y = 0, color = "white") {
  list(type = "line",
       x0 = 0, x1 = 1, xref = "paper", y0 = y, y1 = y, 
       line = list(color = color, width = 1))
}
fig$x$layout$shapes <- lapply(-10:10, hlines)
fig

在此處輸入圖像描述

謝謝大家的幫助。 非常感激。 下面我記錄完整的答案。

帶有笛卡爾網格線的輪廓 plot 示例

rm(list=ls(all=TRUE))
library(plotly)
x1 <- -10:10
y1 <- -10:10
z1 <- sqrt(outer(x1 ^ 2, y1 ^ 2, "+"))
fig <- plot_ly(z = z1, x = x1, y = y1,  type = "contour", 
               line = list(width = 1.5, color = "black"),
               contours = list(showlabels = TRUE))
fig

# adding cartesian horizontal grid lines

hlines <- function(y = 0, color = "white") {
  list(type = "line",
       x0 = 0, x1 = 1, xref = "paper", y0 = y, y1 = y, 
       line = list(color = color, width = 0.5))
}
fig$x$layout$shapes <- lapply(-10:10, hlines)
fig


#Add vertical lines
vlines <- function(x = 0, color = "white") {
  list(type = "line",
       y0 = 0, y1 = 1, yref = "paper", x0 = x, x1 = x,
       line = list(color = color, width = 0.5))
}

fig$x$layout$shapes[22:42] <- lapply(-10:10, vlines)
fig

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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