如何创建3D曲面图

时间:2013-12-20 10:18:18

标签: r 3d plot

我有x,y和z数据的独特组合,想要创建三维散点图和曲面图。 三维散点图使用此代码:

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

但我无法使用基本图形创建表面图。  persp(x, z, y)给了我错误消息:“预计会增加'x'和'y'值。

我还尝试了rgl包中的rgl包:

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

导致“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“

我查看了之前的帖子here以及here,但大多数都需要对数据进行一些更改或插值。如何使用样本数据创建三维表面图而不更改/更改内容。

示例数据如下:

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")

1 个答案:

答案 0 :(得分:1)

我假设你的y&amp; z列是倒置的,但你可以做这样的事情 - 基本上你需要x&amp; y表示轴,z表示交叉值:

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

相关问题