|
Dear List, I am trying to plot a similar graph to attached from minitab manual in R. I have a response Y and three components which systematically vary in their proportions. I have found in R methods/packages to plot ternary plots (eg. plotrix) but nothing which can extend it to response surface in 3-D. Any help appreciated, Tim Carnus ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
|
On 5/12/2009 8:43 AM, Tim Carnus wrote:
> Dear List, > I am trying to plot a similar graph to attached from minitab manual in R. > I have a response Y and three components which systematically vary in their > proportions. I have found in R methods/packages to plot ternary plots (eg. > plotrix) but nothing which can extend it to response surface in 3-D. > Any help appreciated, I'm not aware of anyone who has done this. The way to do the surface in rgl would be to construct a mesh of triangles using tmesh3d, and set the color of each vertex as part of the material argument. It's a little tricky to get the colors right when they vary by vertex, but the code below gives an example. I would construct the mesh by starting with one triangle and calling subdivision3d, but you may want more control over them. For example: library(rgl) # First create a flat triangle and subdivide it triangle <- c(0,0,0,1, 1,0,0,1, 0.5, sqrt(3)/2, 0, 1) mesh <- tmesh3d( triangle, 1:3, homogeneous=TRUE) mesh <- subdivision3d(mesh, 4, deform=FALSE, normalize=TRUE) # Now get the x and y coordinates and compute the surface height x <- with(mesh, vb[1,]) y <- with(mesh, vb[2,]) z <- x^2 + y^2 mesh$vb[3,] <- z # Now assign colors according to the height; remember that the # colors need to be in the order of mesh$it, not vertex order. vcolors <- rainbow(100)[99*z+1] tricolors <- vcolors[mesh$it] mesh$material = list(color=tricolors) # Now draw the surface, and a rudimentary frame behind it. shade3d(mesh) triangles3d(matrix(triangle, byrow=TRUE, ncol=4), col="white") quads3d(matrix(c(1,0.5,0.5,1, 0,sqrt(3)/2, sqrt(3)/2,0, 0,0,1,1), ncol=3), col="white") bg3d("gray") Duncan Murdoch ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
|
In reply to this post by Tim Carnus
hello,
I have an code for similar plot in 2D. HTH Cleber Borges ------------------------------------------------------ ######################################################### ######################################################### ######################################################### trimage <- function(f){ x = y = seq( 1, 0, l=181 ) t1 = length(x) im = aux = numeric(0) for( i in seq( 1, t1, by = 2 ) ){ #idx = seq( t1**2, i*t1, by = -t1 ) - ((t1 - i):0) idx = seq( i*t1, t1**2, by = t1 ) - (i-1) im = c(im, aux, idx, aux ) aux = c(aux, NA) } z = outer(X=x, Y=y, FUN=f) return( matrix(z[im],nr=t1) ) } ### EXAMPLE ternary_func <- function(x1, x2) { x3=1-x1-x2 -100*x1 + 0*x2 + 100*x3 + 200*x1*x2 } zmat1 <- trimage(ternary_func) windows(w=4.5, h=4.5, restoreConsole = TRUE ) par(mar=c(5,5,5,5), pty='s', xaxt='n', yaxt='n', bty='n' ) image(z=zmat1, ylab='', xlab='' ) contour(z=zmat1, add=T, nlevels=10 ) ### tips.: use tim.colors in package 'fields' library(fields) windows(w=4.5, h=4.5, restoreConsole = TRUE ) par(mar=c(5,5,5,5), pty='s', xaxt='n', yaxt='n', bty='n' ) image(z=zmat1, ylab='', xlab='', col=tim.colors(256) ) contour(z=zmat1, add=T, nlevels=10 ) ######################################################### ######################################################### ######################################################### ------------------------------------------------------ Tim Carnus escreveu: > Dear List, > I am trying to plot a similar graph to attached from minitab manual in R. > I have a response Y and three components which systematically vary in their > proportions. I have found in R methods/packages to plot ternary plots (eg. > plotrix) but nothing which can extend it to response surface in 3-D. > Any help appreciated, > Tim Carnus > > > ------------------------------------------------------------------------ > > ------------------------------------------------------------------------ > > ______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
|
In reply to this post by Duncan Murdoch
HI,
thank you for that. I had come across a while ago a presentation outlining the ideas for such a function but can't remember who or where. Thanks again, Tim ----- Original Message ----- From: Duncan Murdoch <[hidden email]> Date: Tuesday, May 12, 2009 4:26 pm Subject: Re: [R] Response surface plot To: Tim Carnus <[hidden email]> Cc: [hidden email] > On 5/12/2009 8:43 AM, Tim Carnus wrote: > > Dear List, > > I am trying to plot a similar graph to > attached from minitab manual in R. > > I have a response Y and three components > which systematically vary in their > > proportions. I have found in R > methods/packages to plot ternary plots (eg. > > plotrix) but nothing which can extend it to > response surface in 3-D. > > Any help appreciated, > > I'm not aware of anyone who has done this. The way to do > the surface in > rgl would be to construct a mesh of triangles using tmesh3d, and > set the > color of each vertex as part of the material argument. It's a > little > tricky to get the colors right when they vary by vertex, but the > code > below gives an example. > > I would construct the mesh by starting with one triangle and > calling > subdivision3d, but you may want more control over them. > > For example: > > library(rgl) > > # First create a flat triangle and subdivide it > triangle <- c(0,0,0,1, 1,0,0,1, 0.5, sqrt(3)/2, 0, 1) > mesh <- tmesh3d( triangle, 1:3, homogeneous=TRUE) > mesh <- subdivision3d(mesh, 4, deform=FALSE, normalize=TRUE) > > # Now get the x and y coordinates and compute the surface height > x <- with(mesh, vb[1,]) > y <- with(mesh, vb[2,]) > z <- x^2 + y^2 > mesh$vb[3,] <- z > > # Now assign colors according to the height; remember that the > # colors need to be in the order of mesh$it, not vertex order. > > vcolors <- rainbow(100)[99*z+1] > tricolors <- vcolors[mesh$it] > mesh$material = list(color=tricolors) > > # Now draw the surface, and a rudimentary frame behind it. > > shade3d(mesh) > triangles3d(matrix(triangle, byrow=TRUE, ncol=4), col="white") > quads3d(matrix(c(1,0.5,0.5,1, 0,sqrt(3)/2, sqrt(3)/2,0, > 0,0,1,1), > ncol=3), col="white") > bg3d("gray") > > Duncan Murdoch ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
| Powered by Nabble | See how NAML generates this page |
