3d plot in R - Patch

时光总嘲笑我的痴心妄想 提交于 2019-11-27 13:49:50

Use outer to create the z values and then use persp to plot:

z <- outer(x,y, function(x,y) sin(sqrt(x^2+y^2)))
persp(x,y,z)

There are options for colouring and setting the viewing angle, see ?persp. See the fourth example for Matlab style colouring.

For an interactive plot, consider using persp3d in the rgl package:

require(rgl)
persp3d(x,y,z,col="blue")

Edit

To add colour, there is a slight difference from the method in persp, since the colour relates to the vertex rather than the centre of the facet, but it makes it easier.

jet.colors <- colorRampPalette( c("blue", "green") ) 
pal <- jet.colors(100)
col.ind <- cut(z,100) # colour indices of each point
persp3d(x,y,z,col=pal[col.ind])

The help file recommends adding the parameter smooth=FALSE, but that's down to personal preference.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!