3d plot in R - Patch
Asked Answered
K

1

7

I have the following data in a data frame:

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

For example:

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

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

I tried on R, but all I could get was a not-so-fancy 3d scatterplot. I also read about the lattice 3d wireframe, but I couldn't get my head around it.

What am I supposed to do to get a Matlab like wireframe in R? What data transforms are involved?

This is the 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 don't find it particularly clear.

EDIT: the persp3d function works fine, and I was able to generate a 3d plot with one colour. How can I set a colour scale relative to the z value?

Thanks for any hints, Mulone

Karate answered 27/9, 2011 at 11:29 Comment(4)
It is true that it takes a little practice to convert (x,y,z) data into a matrix of z-values for wireframe or other 3-d grid plotting tools. It's worth the effort to learn to convert, since once you know how, you never forget :-) . You might like the scatterplot3d package, which does allow wiregrid plots.Painting
This is untested so it's going as a comment rather than an answer. I would recommend with(mydata,persp(x=sort(unique(X)),y=sort(unique(Y)),z=matrix(Z,ncol=length(unique(x))))) -- I'm not quite sure I got the matrix organized in the right order (maybe byrow=TRUE etc.)Paragraphia
See example(wireframe) and example(persp) and check if it fits your needs.Loam
I never quite found the time to get my head around this, but look at the code here: addictedtor.free.fr/graphiques/graphcode.php?graph=23Triangulate
M
15

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)

persp

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])

persp3d

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

Misha answered 27/9, 2011 at 11:48 Comment(2)
The problem w/ this answer is it assumes z = f(x,y). The OP said he had a set of ordered triples (x,y,z), in which case he'd need to generate a fitting function. Of course that will not plot the exact original data .Painting
@Karate I've included the example of adding height-based colouring to the persp3d plot.Misha

© 2022 - 2024 — McMap. All rights reserved.