有没有办法在 R 中的绘图下方创建带有轮廓的 3d 绘图? [英] Is there a way to create a 3d plot with contours beneath the plot in R?

查看:62
本文介绍了有没有办法在 R 中的绘图下方创建带有轮廓的 3d 绘图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 R 中创建一个 3d 图,在图下方有这样的轮廓?如果没有,有没有办法将我现有的等高线图放在 3d 透视图上?

Is there a way to create a 3d plot in R that has contours beneath the plot like so? If not, is there a way to take my existing contour plot and put it on top of the 3d persp plot?

我已经能够制作 3d 图以及函数和约束的等高线图,但无法将两者结合起来.这是我的 R 代码:

I have been able to make a 3d plot, and a contour plot of the function and constraint, but have not been able to combine the two. Here is my R code:

obj = function(x1,x2){
  value = -(cos((x1-.1)*x2))^2 - x1*sin(3*x1+x2)
  return(value)
}

con1 = function(x1,x2){
  t = atan2(x1,x2)
  value = x1^2 + x2^2 -((2*cos(t)-1/2*cos(2*t)-1/4*cos(3*t)-1/8*cos(4*t))^2) - ((2*sin(t))^2)
  return(value)
}


x1 = seq(-2.25,2.5,.015)
x2 = seq(-2.5,1.75,.015)

out = outer(x1,x2,obj)

infeasible = ifelse(c(outer(x1,x2,con1)<=0),1,0)
image(x1,x2,out,xlab=expression(x[1]),ylab=expression(x[2]),main="Modified Townsend Problem")

X = cbind(expand.grid(x1,x2),infeasible)
points(X[X[,3]==0,1],X[X[,3]==0,2],pch=19,col="lightgrey")

persp(x1,x2,out,phi=35,col="lightgrey")

非常感谢任何建议或包.

Any suggestions or packages that do it are greatly appreciated.

推荐答案

我认为这是可行的,通过 ?contourLines 和你的观点的一些转换:

I reckon this is doable via ?contourLines and some transformation of your points:

p <- persp(x1, x2, out, phi=35, col="#00000000", border=NA, box=FALSE)
cl <- contourLines(x1, x2, replace(out, outer(x1,x2,con1) > 0, NA))
levs <- factor(sapply(cl, `[[`, "level"))
Map(
  function(cl,col) lines(trans3d(cl$x, cl$y, min(out), pmat=p), col=col),
  cl,
  hcl.colors(nlevels(levs), "YlOrRd", rev = TRUE)[levs]
)
par(new=TRUE)
persp(x1, x2, out, phi=35, col="lightgrey")

这篇关于有没有办法在 R 中的绘图下方创建带有轮廓的 3d 绘图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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