Last active
March 21, 2019 15:30
-
-
Save arraytools/ef955d017cb6b9ef0690a4fe79f809f9 to your computer and use it in GitHub Desktop.
3 D example using persp()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ### Random pattern | |
| # Create matrix with random values with dimension of final grid | |
| rand <- rnorm(441, mean=0.3, sd=0.1) | |
| mat.rand <- matrix(rand, nrow=21) | |
| # Create another matrix for the colors. Start by making all cells green | |
| fill <- matrix("green3", nr = 21, nc = 21) | |
| # Change colors in each cell based on corresponding mat.rand value | |
| fcol <- fill | |
| fcol[] <- terrain.colors(40)[cut(mat.rand, | |
| stats::quantile(mat.rand, seq(0,1, len = 41), | |
| na.rm=T), include.lowest = TRUE)] | |
| # Create concave surface using expontential function | |
| x <- -10:10 | |
| y <- x^2 | |
| y <- as.matrix(y) | |
| y1 <- y | |
| for(i in 1:20){tmp <- cbind(y,y1); y1 <- tmp[,1]; y <- tmp;} | |
| mat <- tmp[1:21, 1:21] | |
| # Plot it up! | |
| persp(1:21, 1:21, t(mat)/10, theta = 90, phi = 35,col=fcol, | |
| scale = FALSE, axes = FALSE, box = FALSE) | |
| ### Organized pattern | |
| # Same as before | |
| rand <- rnorm(441, mean=0.3, sd=0.1) | |
| # Create concave surface using expontential function | |
| x <- -10:10 | |
| y <- x^2 | |
| y <- as.matrix(y) | |
| for(i in 1:20){tmp <- cbind(y,y); y1 <- tmp[,1]; y <- tmp;} | |
| mat <- tmp[1:21, 1:21] | |
| ###Organize rand by y and put into matrix form | |
| o <- order(rand,as.vector(mat)) | |
| o.tmp <- cbind(rand[o], rev(sort(as.vector(mat)))) | |
| mat.org <- matrix(o.tmp[,1], nrow=21) | |
| half.1 <- mat.org[,seq(1,21,2)] | |
| half.2 <- mat.org[,rev(seq(2,20,2))] | |
| full <- cbind(half.1, half.2) | |
| full <- t(full) | |
| # Again, create color matrix and populate using rand values | |
| zi <- full[-1, -1] + full[-1, -21] + full[-21,-1] + full[-21, -21] | |
| fill <- matrix("green3", nr = 20, nc = 20) | |
| fcol <- fill | |
| fcol[] <- terrain.colors(40)[cut(zi, | |
| stats::quantile(zi, seq(0,1, len = 41), na.rm=T), | |
| include.lowest = TRUE)] | |
| # Plot it up! | |
| persp(1:21, 1:21, t(mat)/10, theta = 90, phi = 35,col=t(fcol), | |
| scale = FALSE, axes = FALSE, box = FALSE) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment