R曲面,包含NA值

时间:2014-09-08 07:45:21

标签: r plot

我想在R中绘制一个3d表面。我的数据包含NA。

# Topographic Surface Plot wanted!
library(rgl)
x<-seq(from = 0,to = 4000,by = 500)
y<-seq(from = 10,to = 580,by = 60)
z<-matrix(data = rbinom(n = 9*10,size = 10,prob = 0.7),nrow = length(y))
# Everybody can plot this in 3d, but my real world data contains empty space NA values like here:
z[8:10,8:9]<-NA
# Also, the y-axis is not homogeneous, it looks more like this:
y<-y+rbinom(n = length(y),size = 15,prob = 0.4)

需要帮助:如何绘制xyz地形表面?

提前致谢!

2 个答案:

答案 0 :(得分:0)

除非您尝试生成3D热图,否则看起来您的值太多了。如果要生成曲面图,Z应该是数组,而不是矩阵。加上现在的情况,Z输出NA值。这是因为行z[8:10,8:9]<-NA

试试这里建议的内容: R: Plotting a 3D surface from x, y, z

答案 1 :(得分:0)

您可以尝试persp在3D透视图中绘制曲面 - 它还允许NA和不等分布的x和y值:

# theta and phi control view angle    
persp(x=y,y=x,z=z, theta=50, phi = 40, zlim=c(0,15))

# for fancy colours:
nr <- nrow(z)
nc <- ncol(z)

# Calculate value at center of each cell
zfacet <- (z[-1, -1] + z[-1, -nc] + z[-nr, -1] + z[-nr, -nc])/4

# Generate the desired colors
cols = heat.colors(10)

# Cut matrix values into 10 bins by manual breaks
zbinned <- cut(zfacet, breaks=10)

persp(x=y,y=x,z=z, theta=30, phi = 30, r=2, zlim=c(-15,15), col=cols[zbinned], ticktype='detailed')