SAN
SAN

Reputation: 2247

Surface plot in R similar to the one from gnuplot

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

Upvotes: 3

Views: 3721

Answers (3)

Roman Luštrik
Roman Luštrik

Reputation: 70643

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

Upvotes: 3

Greg Snow
Greg Snow

Reputation: 49640

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

Upvotes: 0

James
James

Reputation: 66834

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.

Upvotes: 1

Related Questions