3
votes

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
3
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
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 addictedtor.free.fr/graphiques/thumbs.php?sort=package has anything useful. - Roman Luštrik

3 Answers

3
votes

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

1
votes

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.

0
votes

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