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 - 将字符串直接转换为 IDateTime

r - 如何从R中的princomp获取 "proportion of variance"向量

r - 通过跨多列测试逻辑条件进行过滤

python - 下拉更新后如何删除 plotly dash 中的撤消按钮

python - 如何在 plotly 时间序列图表中添加和定义多条线?

r - R中添加水印

r - 在R中使用lm和nls进行正弦曲线拟合

python - 动态更改pyqt5中的数据破折号

python - 将 Plotly 与 pycharm 一起使用

r - 如何更改x轴的字体以及如何加粗