# Tutorial on rgl Graphics Classic List Threaded 10 messages Open this post in threaded view
|

## Tutorial on rgl Graphics

 Can anyone point me towards a tutorial on using the rgl graphics package? Something with lots of examples would be nice. Thanks. Tom
Open this post in threaded view
|

## Re: Tutorial on rgl Graphics

 On 25/07/2008 6:29 AM, Tom La Bone wrote: > Can anyone point me towards a tutorial on using the rgl graphics package? > Something with lots of examples would be nice. Thanks. The manual has lots of examples, and there are some papers about it, but I don't know of a tutorial. I think the most relevant papers are http://rgl.neoscientists.org/arc/doc/RGL_DSC03.pdf  - From the DSC 2003 conference http://user2007.org/program/presentations/murdoch.pdf  - From useR 2007. Duncan Murdoch ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Tutorial on rgl Graphics

 After looking around a bit more I found the example I was looking for -- plotlm3d, which I found on the R wiki      http://wiki.r-project.org/rwiki/doku.php?id=graph_gallery:new-graphicsThe original author was John Fox, and it was modified by Jose Claudio Faria and Duncan Murdoch. Below is a simplified version of the function and two examples. One example was presented as a test of the function and it works fine. The second example is the plot I want to make and I can't seem to get the scale on the x and y axes correct. Being unfamiliar with rgl, can someone provide a hint on how to get the scales right? Thanks for the help. Tom plotlm3d <- function (x, y, z,                       surface        = T,                       model          = 'z ~ x + y',                       simple.axes    = T,                       box            = F,                       xlab           = deparse(substitute(x)),                       ylab           = deparse(substitute(y)),                       zlab           = deparse(substitute(z)),                       surface.col    = c('blue', 'orange', 'red', 'green',                                          'magenta', 'cyan', 'yellow', 'gray', 'brown'),                       point.col      = 'yellow',                       grid.col       = material3d("color"),                       grid           = T,                       grid.lines     = 26,                       sphere.factor  = 1,                       threshold      = 0.01) {   require(rgl)   require(mgcv)   xlab; ylab; zlab   size <- max(c(x,y,z))/100 * sphere.factor   if (size > threshold)     spheres3d(x, y, z, color = point.col, radius = size)   else     points3d(x, y, z, color = point.col)   aspect3d(c(1, 1, 1))   if (surface) {     xvals <- seq(min(x), max(x), length = grid.lines)     yvals <- seq(min(y), max(y), length = grid.lines)     dat  <- expand.grid(x = xvals, y = yvals)     for (i in 1:length(model)) {       mod <- lm(formula(model[i]))       zhat <- matrix(predict(mod, newdata = dat), grid.lines, grid.lines)       surface3d(xvals, yvals, zhat, color = surface.col[i], alpha = 0.5, lit = F)       if (grid)         surface3d(xvals, yvals, zhat, color = grid.col, alpha = 0.5,         lit = F, front = 'lines', back = 'lines') }}   if(simple.axes) {     axes3d(c('x', 'y', 'z'))     title3d(xlab = xlab, ylab = ylab, zlab = zlab)   }   else     decorate3d(xlab = xlab, ylab = ylab, zlab = zlab, box = box) } #This is an example of a 3D scatterplot that works fine x <- c( 274,  180,  375,  205,   86,  265,   98,  330,  195,   53,        430,  372,  236,  157,  370) y <- c(2450, 3254, 3802, 2838, 2347, 3782, 3008, 2450, 2137, 2560,       4020, 4427, 2660, 2088, 2605) z <- c( 162,  120,  223,  131,   67,  169,   81,  192,  116,   55,        252,  232,  144,  103,  212) open3d() plotlm3d(x, y, z,          surface = T,          model   = 'z ~ x + y',          xlab    = 'x',          ylab    = 'y',          zlab    = 'z') #This is the plot I am trying to make - scales on x and y axes are wrong x <- c(0.3405,0.1220,0.1028,0.08451,0.05668,0.0345,0.003788,0.002121) y <- c(0.3460,0.1227,0.1097,0.09666,0.07677,0.06278,0.02168,0.01303) z <- c(2720,1150,1010,790,482,358,78,35) open3d() plotlm3d(x, y, z,          surface = T,          model   = 'z ~ x + y - 1',          xlab    = 'x',          ylab    = 'y',          zlab    = 'z')
Open this post in threaded view
|

## Re: Tutorial on rgl Graphics

 On 7/25/2008 11:01 AM, Tom La Bone wrote: > After looking around a bit more I found the example I was looking for -- > plotlm3d, which I found on the R wiki > >      http://wiki.r-project.org/rwiki/doku.php?id=graph_gallery:new-graphics> > The original author was John Fox, and it was modified by Jose Claudio Faria > and Duncan Murdoch. Below is a simplified version of the function and two > examples. One example was presented as a test of the function and it works > fine. The second example is the plot I want to make and I can't seem to get > the scale on the x and y axes correct. Being unfamiliar with rgl, can > someone provide a hint on how to get the scales right? Thanks for the help. > > Tom > > > > plotlm3d <- function (x, y, z, >                       surface        = T, >                       model          = 'z ~ x + y', >                       simple.axes    = T, >                       box            = F, >                       xlab           = deparse(substitute(x)), >                       ylab           = deparse(substitute(y)), >                       zlab           = deparse(substitute(z)), >                       surface.col    = c('blue', 'orange', 'red', 'green', >                                          'magenta', 'cyan', 'yellow', > 'gray', 'brown'), >                       point.col      = 'yellow', >                       grid.col       = material3d("color"), >                       grid           = T, >                       grid.lines     = 26, >                       sphere.factor  = 1, >                       threshold      = 0.01) > { >   require(rgl) >   require(mgcv) >   xlab; ylab; zlab >   size <- max(c(x,y,z))/100 * sphere.factor >   if (size > threshold) >     spheres3d(x, y, z, color = point.col, radius = size) >   else >     points3d(x, y, z, color = point.col) >   aspect3d(c(1, 1, 1)) >   if (surface) { >     xvals <- seq(min(x), max(x), length = grid.lines) >     yvals <- seq(min(y), max(y), length = grid.lines) >     dat  <- expand.grid(x = xvals, y = yvals) >     for (i in 1:length(model)) { >       mod <- lm(formula(model[i])) >       zhat <- matrix(predict(mod, newdata = dat), grid.lines, grid.lines) >       surface3d(xvals, yvals, zhat, color = surface.col[i], alpha = 0.5, lit > = F) >       if (grid) >         surface3d(xvals, yvals, zhat, color = grid.col, alpha = 0.5, >         lit = F, front = 'lines', back = 'lines') }} >   if(simple.axes) { >     axes3d(c('x', 'y', 'z')) >     title3d(xlab = xlab, ylab = ylab, zlab = zlab) >   } >   else >     decorate3d(xlab = xlab, ylab = ylab, zlab = zlab, box = box) > } > > #This is an example of a 3D scatterplot that works fine > x <- c( 274,  180,  375,  205,   86,  265,   98,  330,  195,   53, >        430,  372,  236,  157,  370) > y <- c(2450, 3254, 3802, 2838, 2347, 3782, 3008, 2450, 2137, 2560, >       4020, 4427, 2660, 2088, 2605) > z <- c( 162,  120,  223,  131,   67,  169,   81,  192,  116,   55, >        252,  232,  144,  103,  212) > open3d() > plotlm3d(x, y, z, >          surface = T, >          model   = 'z ~ x + y', >          xlab    = 'x', >          ylab    = 'y', >          zlab    = 'z') > > #This is the plot I am trying to make - scales on x and y axes are wrong > x <- c(0.3405,0.1220,0.1028,0.08451,0.05668,0.0345,0.003788,0.002121) > y <- c(0.3460,0.1227,0.1097,0.09666,0.07677,0.06278,0.02168,0.01303) > z <- c(2720,1150,1010,790,482,358,78,35) > open3d() > plotlm3d(x, y, z, >          surface = T, >          model   = 'z ~ x + y - 1', >          xlab    = 'x', >          ylab    = 'y', >          zlab    = 'z') I think you're seeing the effect of the inaccurate bounding box calculation described in ?spheres3d.  The problem is that you're asking for a sphere, but your axes are on wildly different scales.  The bounding box calculation for that situation fails. You can work around this by telling rgl to ignore the extent of the spheres (via par3d(ignoreExtent=TRUE)), and plot some points in the corners of the bounding box you really want.  Set their alpha to 0 and they'll be invisible. Some day I'll probably fix this, but it's likely to be a while. Duncan Murdoch ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Tutorial on rgl Graphics

 On further experimentation I find that "points" (via points3d) serve my purpose well (instead of the much prettier but more troublesome spheres). The default "point" appears to be a square. Is there a way to make it a circle? Tom Duncan Murdoch-2 wrote On 7/25/2008 11:01 AM, Tom La Bone wrote: > After looking around a bit more I found the example I was looking for -- > plotlm3d, which I found on the R wiki > >      http://wiki.r-project.org/rwiki/doku.php?id=graph_gallery:new-graphics> > The original author was John Fox, and it was modified by Jose Claudio Faria > and Duncan Murdoch. Below is a simplified version of the function and two > examples. One example was presented as a test of the function and it works > fine. The second example is the plot I want to make and I can't seem to get > the scale on the x and y axes correct. Being unfamiliar with rgl, can > someone provide a hint on how to get the scales right? Thanks for the help. > > Tom > > > > plotlm3d <- function (x, y, z, >                       surface        = T, >                       model          = 'z ~ x + y', >                       simple.axes    = T, >                       box            = F, >                       xlab           = deparse(substitute(x)), >                       ylab           = deparse(substitute(y)), >                       zlab           = deparse(substitute(z)), >                       surface.col    = c('blue', 'orange', 'red', 'green', >                                          'magenta', 'cyan', 'yellow', > 'gray', 'brown'), >                       point.col      = 'yellow', >                       grid.col       = material3d("color"), >                       grid           = T, >                       grid.lines     = 26, >                       sphere.factor  = 1, >                       threshold      = 0.01) > { >   require(rgl) >   require(mgcv) >   xlab; ylab; zlab >   size <- max(c(x,y,z))/100 * sphere.factor >   if (size > threshold) >     spheres3d(x, y, z, color = point.col, radius = size) >   else >     points3d(x, y, z, color = point.col) >   aspect3d(c(1, 1, 1)) >   if (surface) { >     xvals <- seq(min(x), max(x), length = grid.lines) >     yvals <- seq(min(y), max(y), length = grid.lines) >     dat  <- expand.grid(x = xvals, y = yvals) >     for (i in 1:length(model)) { >       mod <- lm(formula(model[i])) >       zhat <- matrix(predict(mod, newdata = dat), grid.lines, grid.lines) >       surface3d(xvals, yvals, zhat, color = surface.col[i], alpha = 0.5, lit > = F) >       if (grid) >         surface3d(xvals, yvals, zhat, color = grid.col, alpha = 0.5, >         lit = F, front = 'lines', back = 'lines') }} >   if(simple.axes) { >     axes3d(c('x', 'y', 'z')) >     title3d(xlab = xlab, ylab = ylab, zlab = zlab) >   } >   else >     decorate3d(xlab = xlab, ylab = ylab, zlab = zlab, box = box) > } > > #This is an example of a 3D scatterplot that works fine > x <- c( 274,  180,  375,  205,   86,  265,   98,  330,  195,   53, >        430,  372,  236,  157,  370) > y <- c(2450, 3254, 3802, 2838, 2347, 3782, 3008, 2450, 2137, 2560, >       4020, 4427, 2660, 2088, 2605) > z <- c( 162,  120,  223,  131,   67,  169,   81,  192,  116,   55, >        252,  232,  144,  103,  212) > open3d() > plotlm3d(x, y, z, >          surface = T, >          model   = 'z ~ x + y', >          xlab    = 'x', >          ylab    = 'y', >          zlab    = 'z') > > #This is the plot I am trying to make - scales on x and y axes are wrong > x <- c(0.3405,0.1220,0.1028,0.08451,0.05668,0.0345,0.003788,0.002121) > y <- c(0.3460,0.1227,0.1097,0.09666,0.07677,0.06278,0.02168,0.01303) > z <- c(2720,1150,1010,790,482,358,78,35) > open3d() > plotlm3d(x, y, z, >          surface = T, >          model   = 'z ~ x + y - 1', >          xlab    = 'x', >          ylab    = 'y', >          zlab    = 'z') I think you're seeing the effect of the inaccurate bounding box calculation described in ?spheres3d.  The problem is that you're asking for a sphere, but your axes are on wildly different scales.  The bounding box calculation for that situation fails. You can work around this by telling rgl to ignore the extent of the spheres (via par3d(ignoreExtent=TRUE)), and plot some points in the corners of the bounding box you really want.  Set their alpha to 0 and they'll be invisible. Some day I'll probably fix this, but it's likely to be a while. Duncan Murdoch ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Tutorial on rgl Graphics

 On 7/25/2008 12:20 PM, Tom La Bone wrote: > On further experimentation I find that "points" (via points3d) serve my > purpose well (instead of the much prettier but more troublesome spheres). > The default "point" appears to be a square. Is there a way to make it a > circle? Not very easily.  Now you can ask for text of a letter "o" and that is not too bad.  You might be able to do it using "sprites". Duncan Murdoch > > Tom > > > > > Duncan Murdoch-2 wrote: >> >> On 7/25/2008 11:01 AM, Tom La Bone wrote: >>> After looking around a bit more I found the example I was looking for -- >>> plotlm3d, which I found on the R wiki >>> >>>     >>> http://wiki.r-project.org/rwiki/doku.php?id=graph_gallery:new-graphics>>> >>> The original author was John Fox, and it was modified by Jose Claudio >>> Faria >>> and Duncan Murdoch. Below is a simplified version of the function and two >>> examples. One example was presented as a test of the function and it >>> works >>> fine. The second example is the plot I want to make and I can't seem to >>> get >>> the scale on the x and y axes correct. Being unfamiliar with rgl, can >>> someone provide a hint on how to get the scales right? Thanks for the >>> help. >>> >>> Tom >>> >>> >>> >>> plotlm3d <- function (x, y, z, >>>                       surface        = T, >>>                       model          = 'z ~ x + y', >>>                       simple.axes    = T, >>>                       box            = F, >>>                       xlab           = deparse(substitute(x)), >>>                       ylab           = deparse(substitute(y)), >>>                       zlab           = deparse(substitute(z)), >>>                       surface.col    = c('blue', 'orange', 'red', >>> 'green', >>>                                          'magenta', 'cyan', 'yellow', >>> 'gray', 'brown'), >>>                       point.col      = 'yellow', >>>                       grid.col       = material3d("color"), >>>                       grid           = T, >>>                       grid.lines     = 26, >>>                       sphere.factor  = 1, >>>                       threshold      = 0.01) >>> { >>>   require(rgl) >>>   require(mgcv) >>>   xlab; ylab; zlab >>>   size <- max(c(x,y,z))/100 * sphere.factor >>>   if (size > threshold) >>>     spheres3d(x, y, z, color = point.col, radius = size) >>>   else >>>     points3d(x, y, z, color = point.col) >>>   aspect3d(c(1, 1, 1)) >>>   if (surface) { >>>     xvals <- seq(min(x), max(x), length = grid.lines) >>>     yvals <- seq(min(y), max(y), length = grid.lines) >>>     dat  <- expand.grid(x = xvals, y = yvals) >>>     for (i in 1:length(model)) { >>>       mod <- lm(formula(model[i])) >>>       zhat <- matrix(predict(mod, newdata = dat), grid.lines, grid.lines) >>>       surface3d(xvals, yvals, zhat, color = surface.col[i], alpha = 0.5, >>> lit >>> = F) >>>       if (grid) >>>         surface3d(xvals, yvals, zhat, color = grid.col, alpha = 0.5, >>>         lit = F, front = 'lines', back = 'lines') }} >>>   if(simple.axes) { >>>     axes3d(c('x', 'y', 'z')) >>>     title3d(xlab = xlab, ylab = ylab, zlab = zlab) >>>   } >>>   else >>>     decorate3d(xlab = xlab, ylab = ylab, zlab = zlab, box = box) >>> } >>> >>> #This is an example of a 3D scatterplot that works fine >>> x <- c( 274,  180,  375,  205,   86,  265,   98,  330,  195,   53, >>>        430,  372,  236,  157,  370) >>> y <- c(2450, 3254, 3802, 2838, 2347, 3782, 3008, 2450, 2137, 2560, >>>       4020, 4427, 2660, 2088, 2605) >>> z <- c( 162,  120,  223,  131,   67,  169,   81,  192,  116,   55, >>>        252,  232,  144,  103,  212) >>> open3d() >>> plotlm3d(x, y, z, >>>          surface = T, >>>          model   = 'z ~ x + y', >>>          xlab    = 'x', >>>          ylab    = 'y', >>>          zlab    = 'z') >>> >>> #This is the plot I am trying to make - scales on x and y axes are wrong >>> x <- c(0.3405,0.1220,0.1028,0.08451,0.05668,0.0345,0.003788,0.002121) >>> y <- c(0.3460,0.1227,0.1097,0.09666,0.07677,0.06278,0.02168,0.01303) >>> z <- c(2720,1150,1010,790,482,358,78,35) >>> open3d() >>> plotlm3d(x, y, z, >>>          surface = T, >>>          model   = 'z ~ x + y - 1', >>>          xlab    = 'x', >>>          ylab    = 'y', >>>          zlab    = 'z') >> >> >> I think you're seeing the effect of the inaccurate bounding box >> calculation described in ?spheres3d.  The problem is that you're asking >> for a sphere, but your axes are on wildly different scales.  The >> bounding box calculation for that situation fails. >> >> You can work around this by telling rgl to ignore the extent of the >> spheres (via par3d(ignoreExtent=TRUE)), and plot some points in the >> corners of the bounding box you really want.  Set their alpha to 0 and >> they'll be invisible. >> >> Some day I'll probably fix this, but it's likely to be a while. >> >> 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. >> >> > ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Tutorial on rgl Graphics

 In reply to this post by Duncan Murdoch Hi Duncan, I love your plotlm3d function! I want to plot the following data: > x    1  2  3  4  5  6  7  8  9 10 > y   0.5678042 0.3986702 0.3063544 0.2554426 0.1954868 0.1238850 0.1161249 0.1689951 0.1826783 0.2406300 > z   0.2558812 0.2619668 0.2585930 0.2563669 0.2588323 0.2734029 0.2770458 0.2782657 0.2850179 0.3015296 Then i execute: open3d() plotlm3d(x, y, z,          surface = T,          model   = 'z ~ x + y',          xlab    = 'x',          ylab    = 'y' ) The chart is beautiful but I would like to change the scale on the z-axis to: seq(0.25,0.31,0.01)) I tried this command: axis(3, at=seq(0.25,0.34,0.01)) But it not work - my guess is that the underlying function plotlm3d needs to modified but I cannot figure where. Best, Vincent.
Open this post in threaded view
|

## Re: Tutorial on rgl Graphics

 vincent.deluard wrote: > Hi Duncan, > > I love your plotlm3d function! >   That's not really my function.  I think I suggested some changes to it, but I didn't write most of it. Duncan Murdoch > I want to plot the following data: > >   >> x >>     >    1  2  3  4  5  6  7  8  9 10 >   >> y >>     >   0.5678042 0.3986702 0.3063544 0.2554426 0.1954868 0.1238850 0.1161249 > 0.1689951 0.1826783 0.2406300 >   >> z >>     >   0.2558812 0.2619668 0.2585930 0.2563669 0.2588323 0.2734029 0.2770458 > 0.2782657 0.2850179 0.3015296 > > Then i execute: > > open3d() > plotlm3d(x, y, z, >          surface = T, >          model   = 'z ~ x + y', >          xlab    = 'x', >          ylab    = 'y' > ) > > The chart is beautiful but I would like to change the scale on the z-axis > to: > seq(0.25,0.31,0.01)) > > I tried this command: > axis(3, at=seq(0.25,0.34,0.01)) > > But it not work - my guess is that the underlying function plotlm3d needs to > modified but I cannot figure where. > ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.