0

I want to plot a 3d surface in R. My data contains NA's.

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

Help needed: How can the xyz topographic surface be plotted?

Thanks in advance!

Impulsleistung
  • 177
  • 3
  • 13

2 Answers2

0

Unless you are trying to generate a 3D heat map, it looks like you have too many values. If you want to generate a surface plot, Z should be an array, not a matrix. Plus as it stands now, Z is outputting with NA values. This is because of the line z[8:10,8:9]<-NA.

Try out what is suggested here: R: Plotting a 3D surface from x, y, z

Community
  • 1
  • 1
cranberry
  • 294
  • 5
  • 17
0

You can try persp to plot a surface in 3D perspective -- it also allows NAs and unequally distributed x and y values:

# 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')
koekenbakker
  • 3,524
  • 2
  • 21
  • 30