1

I have unique combinations of x, y and z data and wanted to create 3d scatter plot and a surface plot. The 3d scatter plot worked with this code:

s3d <-scatterplot3d(x,z,y, pch=16, highlight.3d=TRUE, type="h", main="3D Scatterplot")

But I fail to create a surface plot using the base graphics. persp(x, z, y) gave me the error message: “increasing 'x' and 'y' values expected .

I also tried the rgl package the rgl package:

require(rgl)  
surface3d(x, y, z)

which resulted in "Error in rgl.surface(x = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, : y length != x rows * z cols"

I have looked at previous posts here and also here, but most need some alteration or interpolation of the data. How can I create a 3d surface plot with the sample data without changing /much alteration of the content.

Sample data follows:

dput(df)
structure(list(x = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 
4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5), z = c(1, 2, 3, 
4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 
4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 
4, 5, 6, 7, 8, 9, 10), y = c(0.300000041723251, 0.400000065565109, 
0.499999970197678, 0.600000023841858, 0.700000047683716, 0.800000071525574, 
1.20000004768372, 1.59999990463257, 1.99999988079071, 2.39999985694885, 
1.40000009536743, 1.60000002384186, 1.79999983310699, 1.99999988079071, 
2.19999980926514, 2.39999985694885, 3, 3.59999990463257, 4.19999980926514, 
4.79999971389771, 2.49999976158142, 2.79999971389771, 3.09999990463257, 
3.39999961853027, 3.69999980926514, 4, 4.79999971389771, 5.59999990463257, 
6.39999961853027, 7.19999980926514, 3.59999990463257, 4, 4.39999961853027, 
4.79999971389771, 5.19999980926514, 5.59999990463257, 6.59999990463257, 
7.59999942779541, 8.60000038146973, 9.60000038146973, 4.69999980926514, 
5.19999980926514, 5.69999980926514, 6.19999980926514, 6.69999980926514, 
7.19999980926514, 8.40000057220459, 9.60000038146973, 10.8000011444092, 
12.0000009536743)), datalabel = "", time.stamp = "19 Dec 2013 17:54", .Names = c("x", 
"z", "y"), formats = c("%9.0g", "%9.0g", "%9.0g"), types = c(254L, 
254L, 254L), val.labels = c("", "", ""), var.labels = c("", "", 
""), version = 12L, row.names = c("1", "2", "3", "4", "5", "6", 
"7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", 
"18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", 
"29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", 
"40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50"
), class = "data.frame")
Community
  • 1
  • 1
Meso
  • 1,375
  • 5
  • 17
  • 36

1 Answers1

1

I'm assuming your y & z columns are inverted, but you can do something like this - basically you need x & y for the axes and a z-grid for the intersect values:

require(rgl)
require(reshape2)
df.x<-unique(df$x)
df.y<-unique(df$z)
df.z<-acast(df,x~z)
persp(df.x,df.y,df.z)
surface3d(df.x,df.y,df.z)

enter image description here

Troy
  • 8,581
  • 29
  • 32