3d plot in R - patch

I have the following data in a data frame:

**x** in (0,1) **y** in [0,1] **z** in [0,1] 

For instance:

 X,Y,Z 0.1, 0.2, 0.56 0.1, 0.3, 0.57 ... 

I would like to build them on this type of chart: A 3d plot

I tried on R, but all I could get was a not-so-fantastic 3D scattered image . I also read about the 3d wireframe lattice , but I could not lower it around.

What should I do to get Matlab as a wireframe in R? What data conversions are involved?

This is sample code from the documentation:

 x <- seq(-pi, pi, len = 20) y <- seq(-pi, pi, len = 20) g <- expand.grid(x = x, y = y) g$z <- sin(sqrt(g$x^2 + g$y^2)) wireframe(z ~ x * y, g, drape = TRUE, aspect = c(3,1), colorkey = TRUE) 

I do not think this is particularly clear.

EDIT : The persp3d function works fine, and I managed to create a 3D plot with a single color. How to set the color scale relative to the value of z?

Thanks for any tips, Mulone

+4
source share
1 answer

Use outer to create z values, and then use persp to build:

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

persp

There are options for painting and adjusting the viewing angle, see ?persp . See Matlab Style Four Coloring Example.

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

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

Edit

To add color, there is a slight difference from the method in persp , since the color refers to the vertex, not to the center of the facet, but 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]) 

persp3d

The help file recommends adding the parameter smooth=FALSE , but this applies to personal preferences.

+11
source

All Articles