r - 等高线图添加线

标签 r plotly

<分区>

我想在等高线图中添加一条位于 y=1 的水平线和一条位于 x=1 的垂直线,我该怎么做?

我的代码如下所示:

library(plotly)
library("mvtnorm")

cov=matrix(c(2,1,1,2),2,2)

x1=seq(-4,4,by=0.1)
x2=seq(-4,4,by=0.1)

d<-expand.grid(x1,x2)

z=dmvnorm(as.matrix(d),sigma=cov)

plot_ly(x=d[,1],y=d[,2],z=z,type="contour")

最佳答案

你可以使用

plot_ly(x = d[, 1], y = d[, 2], z = ~z, type = "contour") %>%
  add_segments(x = 1, xend = 1, y = -4, yend = 4, inherit = FALSE) %>%
  add_segments(x = -4, xend = 4, y = 1, yend = 1, inherit = FALSE) %>%
  layout(xaxis = list(range = c(-4, 4)),
         yaxis = list(range = c(-4, 4)))

enter image description here

我在其中添加了 inherit = FALSE 以避免警告,以及 layout 部分来修复 x 轴。

关于r - 等高线图添加线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54290296/

相关文章:

r - 条件面板和选择输入

python - 绘制甘特图

r - 获取该月的第二个和第四个星期六

r - 如何删除 knitr 输出中的前缀(索引指示器)[1]?

r - 用于显示和更新 data.table 的 gwidgets 或 View 的替代方案

Python plotly : Adding a horizontal line to a scatter plot that has multiple subplots

r - 如何在R中对列表中的元素进行排序?

r - 具有满足特定条件的列数的新列

javascript - 使绘图适合 div 的最大高度

python - Plotly:如何从两个数据框列中绘制两条线并从其他两列中分配悬停信息?