3

I recently started using R language and now i am using R for most of my 2d plots. Now, I want to use R for generating 3d plots. I have x, y, z data coming from a tool, till now i was using splot in gnuplot to generate a surface plot. I want to use R for generating a surface plot similar to the one splot in gnuplot gives. Problem i see is, to generate a 3d plot R requires data to be in matrix format but my data is not in that format. My question is, if gnuplot is able to generate a plot from the data why R cant do it. I am sure i am missing something, please help me

Here is the plot from gnuplot

Surface plot from GNU plot

This is the data

17.46 537.74 0.8
18.36 537.74 1.6
19.26 537.74 1.3
19.395 537.74 1.7
21.015 537.74 1.9
35.46 475.26 1.2
36.36 475.26 0.8
37.395 475.26 0.9
39.96 475.26 0.6
43.56 475.26 1
SAN
  • 2,219
  • 4
  • 26
  • 32
  • It will help if you describe your current data format, ideally with a small reproducible example. Also, which R function are you using to plot your data? – Andrie Mar 07 '11 at 13:37
  • There are a number of functions for plotting surfaces. Edit your question, possibly with a link or an image of the kind of graph you'd like to produce. While you wait for files to upload, see if http://addictedtor.free.fr/graphiques/thumbs.php?sort=package has anything useful. – Roman Luštrik Mar 07 '11 at 14:01

3 Answers3

3

It's no secret that I'm a raster package fan. It comes with a plot method that uses rgl package. The images can be quite the eye-full.

This is is the example from ?raster::plot3D enter image description here

EDIT

Here's an example of how to plot a surface using a matrix with three columns. This example looks like stage death trap in Mortal Combat. If you're looking for kernel smoothing, then that merits its own question.

library(rgl)
library(raster)
x <- sample(-500:500, 1000, replace = TRUE)
y <- sample(-500:500, 1000, replace = TRUE)
z <- rnorm(500, 10, 20)
df <- cbind(x, y, z)

rst <- raster(ncols = 100, nrows = 100, xmn = -100, xmx = 100, ymn = -100, ymx = 100)
rst2 <- rasterize(x = df[,1:2], y = rst, df[, 3])
plot3D(rst2)

enter image description here

Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
  • This looks very good, hope it doesnt require data in matrix form. I will try this, i have some issue with rgl package (http://stackoverflow.com/questions/5166175/help-in-using-rgl-package), not sure if it works for me. Is there any other package for generating surface plots when the data is not in matrix format? – SAN Mar 07 '11 at 17:31
  • You can use `rgl::surface3d` directly. It accepts the kind of data you describe in your question (three column matrix). – Roman Luštrik Mar 07 '11 at 22:31
  • surface3d also requires a z matrix for each grid point, i am getting this error with the above data y length != x rows * z cols – SAN Mar 08 '11 at 01:05
  • I have some issue with the plot3d function :( stackoverflow.com/questions/5166175/help-in-using-rgl-package – SAN Mar 09 '11 at 15:35
1

You can use the outer function to generate the matrix from a function:

fn3d <- function(x,y) x^2-y^2
persp(outer(seq(-10,10,length=30),seq(-10,10,length=30),fn3d))

Look at ?persp, there are plenty of examples there. If you want interactive 3d plotting, consider installing the package rgl.

James
  • 65,548
  • 14
  • 155
  • 193
  • The data generator is a simluation tool, i dont have a function – SAN Mar 07 '11 at 16:18
  • @Aki You only have a few data points here. It seems the rest of the plot is filled with an interpolated plane in gnuplot. You might want to consider if its an appropriate way to represent the data. – James Mar 07 '11 at 17:00
  • Actual number of point is in millions, for example purpose i took few datapoints – SAN Mar 07 '11 at 17:27
0

The wireframe and levelplot functions in the lattice package use a data format like gnuplot does rather than requireing a matrix.

Greg Snow
  • 48,497
  • 6
  • 83
  • 110