surface3d in r and rgl package

10
Surface3d in R and rgl package: Prepared by Volkan OBAN Code: ibrary(rgl); data(volcano) dim(volcano) peak.height <- volcano; ppm.index <- (1:nrow(volcano)); sample.index <- (1:ncol(volcano)); zlim <- range(peak.height) zlen <- zlim[2] - zlim[1] + 1

Upload: volkan-oban

Post on 24-Jan-2017

55 views

Category:

Data & Analytics


2 download

TRANSCRIPT

Page 1: Surface3d in R and rgl package

Surface3d in R and rgl package:

Prepared by Volkan OBAN

Code:

ibrary(rgl);data(volcano)dim(volcano)

peak.height <- volcano;ppm.index <- (1:nrow(volcano));sample.index <- (1:ncol(volcano));

zlim <- range(peak.height)zlen <- zlim[2] - zlim[1] + 1colorlut <- terrain.colors(zlen) # height color lookup tablecol <- colorlut[(peak.height-zlim[1]+1)] # assign colors to heights for each point

Page 2: Surface3d in R and rgl package

open3d()

ppm.index1 <- ppm.index*zlim[2]/max(ppm.index);sample.index1 <- sample.index*zlim[2]/max(sample.index)

title.name <- paste("plot3d ", "volcano", sep = "");surface3d(ppm.index1, sample.index1, peak.height, color=col, back="lines", main = title.name);grid3d(c("x", "y+", "z"), n =20)

sample.name <- paste("col.", 1:ncol(volcano), sep="");sample.label <- as.integer(seq(1, length(sample.name), length = 5));

axis3d('y+',at = sample.index1[sample.label], sample.name[sample.label], cex = 0.3);axis3d('y',at = sample.index1[sample.label], sample.name[sample.label], cex = 0.3)axis3d('z',pos=c(0, 0, NA))

ppm.label <- as.integer(seq(1, length(ppm.index), length = 10));axes3d('x', at=c(ppm.index1[ppm.label], 0, 0), abs(round(ppm.index[ppm.label], 2)), cex = 0.3);

title3d(main = title.name, sub = "test", xlab = "ppm", ylab = "samples", zlab = "peak")rgl.bringtotop();

Page 3: Surface3d in R and rgl package
Page 4: Surface3d in R and rgl package
Page 5: Surface3d in R and rgl package
Page 6: Surface3d in R and rgl package

Codes:x=seq(-pi,pi,len=50)> y=x;> f=outer(x,y,function(x,y)cos(y)/(1+x^2));> f[1:5, 1:5] [,1] [,2] [,3] [,4] [,5][1,] -0.09199967 -0.09124435 -0.08899081 -0.08527603 -0.08016103[2,] -0.09920276 -0.09838830 -0.09595832 -0.09195270 -0.08643722[3,] -0.10724999 -0.10636947 -0.10374236 -0.09941181 -0.09344892[4,] -0.11627176 -0.11531717 -0.11246907 -0.10777424 -0.10130976[5,] -0.12642295 -0.12538502 -0.12228827 -0.11718355 -0.11015468> > # The contour() function produces a contour map and is used for three> # dimensional data (like mountains). You feed it three inputs. The first> # is a vector of the x values, the second a vector of the y values and> # the third is a matrix with each element corresponding to the Z value> # (the third dimension) for each pair of (x,y) coordinates. Just like> # plot there are many other things you can feed it to. See the help file.> > contour(x,y,f)

Page 7: Surface3d in R and rgl package

> contour(x,y,f,nlevels=15)> > # persp() works the same as image() and contour() but it actually> # produces a 3d plot. theta an phi control the various angles you can look> # at the plot from.> persp(x,y,f)> persp(x,y,f,theta=30)> persp(x,y,f,theta=30,phi=20)

>

Page 8: Surface3d in R and rgl package

Codes:

# (1) The Obligatory Mathematical surface.# Rotated sinc function.

x <- seq(-10, 10, length= 30)y <- xf <- function(x,y) { r <- sqrt(x^2+y^2); 10 * sin(r)/r }z <- outer(x, y, f)z[is.na(z)] <- 1open3d()bg3d("white")material3d(col="black")persp3d(x, y, z, aspect=c(1, 1, 0.5), col = "lightblue", xlab = "X", ylab = "Y", zlab = "Sinc( r )")

# (2) Add to existing persp plot:

xE <- c(-10,10); xy <- expand.grid(xE, xE)points3d(xy[,1], xy[,2], 6, col = "red")lines3d(x, y=10, z= 6 + sin(x), col = "green")

phi <- seq(0, 2*pi, len = 201)r1 <- 7.725 # radius of 2nd maximumxr <- r1 * cos(phi)yr <- r1 * sin(phi)lines3d(xr,yr, f(xr,yr), col = "pink", lwd = 2)

# (3) Visualizing a simple DEM model

Page 9: Surface3d in R and rgl package

z <- 2 * volcano # Exaggerate the reliefx <- 10 * (1:nrow(z)) # 10 meter spacing (S to N)y <- 10 * (1:ncol(z)) # 10 meter spacing (E to W)

open3d()bg3d("slategray")material3d(col="black")persp3d(x, y, z, col = "green3", aspect="iso", axes = FALSE, box = FALSE)

# (4) A cylindrical plot

z <- matrix(seq(0, 1, len=50), 50, 50)theta <- t(z)r <- 1 + exp( -pmin( (z - theta)^2, (z - theta - 1)^2, (z - theta + 1)^2 )/0.01 )x <- r*cos(theta*2*pi)y <- r*sin(theta*2*pi)

open3d()persp3d(x, y, z, col="red")

# (5) A globe

lat <- matrix(seq(90,-90, len=50)*pi/180, 50, 50, byrow=TRUE)long <- matrix(seq(-180, 180, len=50)*pi/180, 50, 50)

r <- 6378.1 # radius of Earth in kmx <- r*cos(lat)*cos(long)y <- r*cos(lat)*sin(long)z <- r*sin(lat)

open3d()persp3d(x, y, z, col="white", texture=system.file("textures/worldsmall.png",package="rgl"), specular="black", axes=FALSE, box=FALSE, xlab="", ylab="", zlab="", normal_x=x, normal_y=y, normal_z=z)play3d(spin3d(axis=c(0,0,1), rpm=8), duration=5)

## Not run: # This looks much better, but is slow because the texture is very bigpersp3d(x, y, z, col="white", texture=system.file("textures/world.png",package="rgl"), specular="black", axes=FALSE, box=FALSE, xlab="", ylab="", zlab="", normal_x=x, normal_y=y, normal_z=z)## End(Not run)

Reference:http://svitsrv25.epfl.ch/R-doc/library/rgl/html/persp3d.html