Dries Van Rompaey
Dries Van Rompaey

Reputation: 21

How can I plot this data as a surface plot in R?

I'm trying to plot the following data as a surface plot. The mesh library or persp3D seems to do what I want, but it seems I cannot find the correct way to input my data. I pasted some sample data below.

  V1   V2   V3
1 1.01 1.30 -113.7410   
2 1.01 1.25 -113.7540   
3 1.01 1.22 -113.7589    
4 1.01 1.20 -113.7605  
5 1.03 1.30 -113.7458  
6 1.03 1.25 -113.7590   
7 1.01 1.20 -113.7605

Thanks!

Upvotes: 2

Views: 54

Answers (1)

chappers
chappers

Reputation: 2415

Your sample data doesn't make sense, you basically need to make a grid of points (equally spaced!) on your x, y axis and a single z coordinate. I suppose you could use interpolation to get some kind of surface plot based on your data.

In the example below I've used interp from akima to interpolate the values based on the mean, and it will produce a grid of points in the format that you're after.

library(rgl)
library(akima)

dat<-data.frame(V1=c(1.01,1.01,1.01,1.01,1.03,1.03,1.01),
              V2=c(1.30,1.25,1.22,1.20,1.30,1.25,1.20),
              V3=c(-113.7410,-113.7540,-113.7589,-113.7605,-113.7458,-113.7590,-113.7605))

s = interp(dat$V1, dat$V2, dat$V3, duplicate="mean")
persp3d(s$x, s$y, s$z)

Output:

enter image description here

Upvotes: 1

Related Questions