rgl package is used to produce interactive 3-D
plots. It contains high-level graphics commands modelled loosely after
classic R graphics, but working in three dimensions. It also contains
low level structure inspired by (but incompatible with) the
This document gives an overview. See the help pages for details.
For installation instructions, see the
README file in
the top level directory of the source tarball rgl_1.2.1.tar.gz (or a
This document was written in R Markdown, using the
package for production. It corresponds to
Most of the highlighted function names are HTML links. The internal links should work in any browser; the links to help topics should work if you view the vignette from within the R help system.
can be used to plot three columns of the
Allowed plot types include
"p", "l", "h", "s", meaning
points, lines, segments from z=0, and spheres. There’s a lot of
flexibility in specifying the coordinates; the
function from the
grDevices package is used for this.
You can use your mouse to manipulate the plot. The default is that if you click and hold with the left mouse button, you can rotate the plot by dragging it. The right mouse button is used to resize it, and the middle button changes the perspective in the point of view.
The other high level function is
to draw surfaces. It is similar to the classic
function, but with greater flexibility. First, any of
z can be specified using matrices, not
z. This allows parametric surfaces to be plotted. An
even simpler specification is possible:
x may be a
function, in which case
persp3d will work out the grid
for details. For example, the
MASS package estimates Gamma
parameters using maximum likelihood in a
example. Here we show the log likelihood surface.
# This example requires the MASS package library(MASS) # from the fitdistr example set.seed(123) x <- rgamma(100, shape = 5, rate = 0.1) fit <- fitdistr(x, dgamma, list(shape = 1, rate = 0.1), lower = 0.001) loglik <- function(shape, rate) sum(dgamma(x, shape=shape, rate=rate, log=TRUE)) loglik <- Vectorize(loglik) xlim <- fit$estimate+4*fit$sd*c(-1,1) ylim <- fit$estimate+4*fit$sd*c(-1,1) mfrow3d(1, 2, sharedMouse = TRUE) persp3d(loglik, xlim = xlim, ylim = ylim, n = 30) zlim <- fit$loglik + c(-qchisq(0.99, 2)/2, 0) next3d() persp3d(loglik, xlim = xlim, ylim = ylim, zlim = zlim, n = 30)
On the left, the whole surface over a range of the parameters; on the right, only the parts of the surface with log likelihood values near the maximum.
Note: this example used the
knitr hook functions (see
setupKnitr) to insert the scene
into this vignette; the previous example used the
function. We generally recommend the newer
Note that both
generic functions, with the following methods defined:
##  plot3d.ashape3d* plot3d.default* plot3d.deldir* ##  plot3d.formula* plot3d.function* plot3d.lm* ##  plot3d.mesh3d* plot3d.rglbackground* plot3d.rglbboxdeco* ##  plot3d.rglobject* plot3d.rglscene* plot3d.rglsubscene* ##  plot3d.rglWebGL* plot3d.tri* plot3d.triSht* ## see '?methods' for accessing help and source code
##  persp3d.ashape3d* persp3d.default* persp3d.deldir* persp3d.formula* ##  persp3d.function* persp3d.tri* persp3d.triSht* ## see '?methods' for accessing help and source code
Just as we have
in classic graphics, there are a number of low level functions in
rgl to add graphical elements to the currently active plot.
The “primitive” shapes are those that are native to OpenGL:
||adds line segments|
Each of the above functions takes arguments
z, again using
for flexibility. They group successive entries as necessary. For
function takes each successive triple of points as the vertices of a
You can use these functions to annotate the current graph, or to construct a figure from scratch.
rgl also has a number of objects which it constructs
from the primitives.
||adds straight lines to plot (like
||adds spherical arcs or spirals to plot|
||adds planes to plot|
||add clipping planes to plot|
||add sprites (fixed shapes or images) to plot|
||a surface (as used in
||drapes lines on a surface or object(s)|
||projects mesh onto a surface|
||add an arrow to a scene|
||draw base-style plotting symbols|
The following low-level functions control the look of the graph:
||add axes to plot|
||add box around plot|
||add title to plot|
||add marginal text to plot|
||add multiple “decorations” (scales, etc.) to plot|
||set the aspect ratios for the plot|
||set the background of the scene|
||show a 2D plot or image in a 3D scene|
||set a legend for the scene|
||add a reference grid to a graph|
||choose label positions to avoid overlap|
||set user-defined axis annotations|
For example, to plot three random triangles, one could use
triangles3d(cbind(x=rnorm(9), y=rnorm(9), z=rnorm(9)), col = "green") decorate3d() bg3d("lightgray")
*3d functions mentioned above, there are
rgl.triangles. You should avoid
using all of these functions, which do not work properly with the
*3d functions and will soon be removed from
rgl. See the
help topic for details.
By default when you open a new plot with
You can change the camera angle simply by dragging the picture with the mouse.
To set the camera angle programmatically, use
This uses polar coordinates:
thetasets the rotation around the vertical axis, in degrees.
phisets the “vertical” rotation around the horizontal axis, from -90 to 90 degrees.
The default angle is roughly
theta = 0, phi = -70.
Starting from this position:
theta, the graph will spin anticlockwise from your point of view.
phito 0, you start to look down at the scene from the top. If you increase
phiabove 0, you continue over and start to see the graph from the “back” (and upside down).
You can also use
to change the camera location using
x,y,z coordinates. In
particular, increasing the
z coordinate lets you zoom out,
and decreasing it zooms you in.
One useful approach is to use the mouse to find a nice viewing angle.
You can then save it using
par3d("userMatrix") and restore
the same view later:
In most scenes, objects are “lit”, meaning that their appearance depends on their position and orientation relative to lights in the scene. The lights themselves don’t normally show up, but their effect on the objects does.
function to specify the position and characteristics of a light. Lights
may be infinitely distant, or may be embedded within the scene. Their
specular components, all defaulting to white. The
ambient component appears the same from any direction. The
diffuse component depends on the angle between the surface
and the light, while the
specular component also takes the
viewer’s position into account.
rgl.light function should no longer be
The mental model used in
rgl is that the objects being
shown in scenes are physical objects in space, with material properties
that affect how light reflects from them (or is emitted by them). These
are mainly controlled by the
function, or by arguments to other functions that are passed to it.
The material properties that are recognized in calls to
material3d are described in detail in the
help page, and listed in the
variable. All of them can be set except the ones in
Here we give an overview.
|color||white||vector of surface colors to apply to successive vertices for diffuse light|
|alpha||1||transparency: 0 is invisible, 1 is opaque|
|lit||TRUE||whether lighting calculations should be done|
|ambient||black||color in ambient light|
|specular||white||color in specular light|
|emission||black||color emitted by the surface|
|shininess||50||controls the specular lighting: high values look shiny|
|smooth||TRUE||whether shading should be interpolated between vertices|
|texture||NULL||optional path to a “texture” bitmap to be displayed on the surface|
|front, back||fill||should polygons be filled, or outlined?|
|size||3||size of points in pixels|
|lwd||1||width of lines in pixels|
Other properties include “texmipmap”, “texmagfilter”, “texminfilter”, “texenvmap”, “fog”, “point_antialias”, “line_antialias”, “depth_mask”, “depth_test”, “polygon_offset”, “margin”, “floating”, “tag” and “blend”; see the help page for details.
There is also a deprecated
rgl.material function that
works at a lower level; users should avoid it.
As described in the previous section, one of the material properties
texture, the name of a bitmap file (in
format) containing an image to be displayed on the surface. This section
gives more details about textures.
OpenGL, each vertex in a polygon may be associated
with a particular location in the bitmap. The interior of the polygon
interpolates within the bitmap. There are two conventions in
rgl functions for specifying these coordinates.
Functions which specify primitives
triangles3d, etc.) accept an
optional matrix argument
texcoords which gives
s (horizontal) and
t (vertical) locations
within the bitmap in columns with one row per vertex. The coordinates
(0,0) for the lower left, and
the upper right. If values outside this range are given, the image
(1.1, 1.2) would specify the same point in
the image as
Other functions such as
surface3d that take matrices for
each vertex coordinate accept texture coordinates as matrices as well,
For example, the following code displays four copies of a 2D plot on
a quad, because the texture coordinates run from 0 to 2 in both
## agg_png ## 2
## null ## 10
Some other notes:
quads3d()above was specified to be white. By default, the colors in the bitmap will modify the color of the surface. If
colis black (a common default), you won’t see anything, so a warning may be issued.
specularto black prevents those.
"texmode"allows texture colors to be used differently. The default is
"modulate", where the texture values combine multiplicatively with the underlying values.
"textype". The default is
"rgb", which takes the red-green-blue colors from the bitmap and uses them to modify the corresponding colors in the polygon.
"texmode"are described in the material3d help page.
"tex*"material properties control how the interpolation within the image is done.
OpenGLsupports 1- and 3-dimensional textures; these are not currently supported in
rgl uses the same ideas as base graphics for drawing
text: there are font families named
"mono" for drawing text of those
"symbol" family is not
New font families can be defined using the low-level function
or more simply using the higher level function
The latter function requires the
extrafont package to be
function, modelled after the classic graphics
function, sets or reads a variety of different
parameters, listed in the
variable. All of them can be set except the ones in
Some parameters are completely read-only; others are fixed at the time
the window is opened, and others may be changed at any time.
|antialias||fixed||Amount of hardware antialiasing|
|cex||Default size for text|
|family||Device-independent font family name; see ?text3d|
|font||Integer font number|
|useFreeType||Should FreeType fonts be used if available?|
|fontname||read-only||System-dependent font name set by
|FOV||Field of view, in degrees. Zero means isometric perspective|
|maxClipPlanes||read-only||How many clip planes can be defined?|
|modelMatrix||read-only||The OpenGL ModelView matrix; partly set by
|projMatrix||read-only||The OpenGL Projection matrix|
|bbox||read-only||Current bounding-box of the scene|
|viewport||Dimensions in pixels of the scene within the window|
|windowRect||Dimensions in pixels of the window on the whole screen|
|listeners||Which subscenes respond to mouse actions in the current one|
|mouseMode||What the mouse buttons do. See
|observer||read-only||The position of the observer; set by
|scale||Rescaling for each coordinate; see
|zoom||Magnification of the scene|
rgl.viewpoint function should not be
list and the
function control defaults in new windows opened by
The function looks for the variable in the user’s global environment, and if not found there, finds the one in the
This allows the user to override the default settings for new
rgl includes a number of functions to construct and
display various solid shapes. These generate objects of class
"shapelist3d". The details of the classes are described
below. We start with functions to generate them.
These functions generate specific shapes. Optional arguments allow attributes such as color or transformations to be specified.
cols <- rainbow(7) layout3d(matrix(1:16, 4,4), heights=c(1,3,1,3)) text3d(0,0,0,"tetrahedron3d"); next3d() shade3d(tetrahedron3d(col=cols)); next3d() text3d(0,0,0,"cube3d"); next3d() shade3d(cube3d(col=cols)); next3d() text3d(0,0,0,"octahedron3d"); next3d() shade3d(octahedron3d(col=cols)); next3d() text3d(0,0,0,"dodecahedron3d"); next3d() shade3d(dodecahedron3d(col=cols)); next3d() text3d(0,0,0,"icosahedron3d"); next3d() shade3d(icosahedron3d(col=cols)); next3d() text3d(0,0,0,"cuboctahedron3d"); next3d() shade3d(cuboctahedron3d(col=cols)); next3d() text3d(0,0,0,"oh3d"); next3d() shade3d(oh3d(col=cols))
A very large collection of polyhedra is contained in the Rpolyhedra package.
These functions generate new shapes:
||generate a tube or cylinder|
||generate a flat polygon by triangulation|
||generate an “extrusion” of a polygon|
||generate a solid of rotation|
||generate an ellipsoid in various ways|
||generate a shape from indexed vertices|
||generate a shape by combining other shapes|
||a generic function; see below|
A related function is
which takes a two dimensional polygon and divides it up into triangles
using the “ear-clipping” algorithm.
The generic function
is provided to allow data structures produced by other code to be
converted to mesh structures. Currently the following classes are
||Delaunay triangulations of irregular point clouds|
||Also Delaunay triangulations|
||Generalized Delaunay triangulations|
function checks that a compatible version of the
package is installed.
method is a simple way to construct a mesh from a matrix of vertices; it
(which can also be used on its own) to merge repeated vertices within
the matrix, allowing
to be used to give a smooth appearance.
generic is a variation that guarantees the resulting object will have no
"mesh3d" is a descendant type. Objects of this type
contain the following fields:
|vb||A 4 by n matrix of vertices in homogeneous coordinates. Each column is a point.|
|ip||(optional) A vector of vertex indices for points.|
|is||(optional) A 2 by s matrix of vertex indices. Each column is a line segment.|
|it||(optional) A 3 by t matrix of vertex indices. Each column is a triangle.|
|ib||(optional) A 4 by q matrix of vertex indices. Each column is a quadrilateral.|
|material||(optional) A list of material properties.|
|normals||(optional) A matrix of the same shape as vb, containing normal vectors at each vertex.|
|texcoords||(optional) A 2 by n matrix of texture coordinates corresponding to each vertex.|
|values||(optional) A vector of length n holding values at each vertex|
|meshColor||(optional) A text value indicating how colors and texture coordinates should be interpreted.|
|tags||(optional) A vector added by some functions
These functions compute and plot contours of functions on surfaces, or clip objects along a contour of a function.
||draw contour lines on surface|
||fill between contours on surface|
||clip mesh object using curved boundary|
||clip general object using curved boundary|
These functions manipulate and modify mesh objects:
||add normal vectors to make a shape look smooth|
||add extra vertices to make it look even smoother|
||merge mesh objects|
||subset of mesh facing “up”|
||get the boundary of a mesh object|
rgl has several functions to support displaying multiple
different “subscenes” in the same window. The high level functions
||Multiple figures (like par(“mfrow”)|
||Multiple figures (like
||Move to the next figure (like
||List all the subscenes in the current layout|
||Clear the current list and revert to the previous one|
There are also lower level functions.
||Create a new subscene, with fine control over what is inherited from the parent|
||Report on the active subscene|
||Get information on current subscene|
||Make a different subscene active|
||Add objects to a subscene, or delete them|
||Do “garbage collection”: delete objects that are not displayed in any subscene|
rgl package can produce output that can be embedded
in other documents. The recommended way to do this has changed several
times over the years. We will start with the current recommendation,
then list older methods.
Currently the best way to embed an
rgl scene in a
document is to produce the document in HTML using R Markdown. Early in
the document, you should have code like this in one of the setup code
The call to
setupKnitr() will install a number of hooks
and set options in
knitr so that
rgl code is
handled properly. The
autoprint = TRUE argument makes
rgl act in the document almost the same way it would act in
the console, or the way base graphics are handled by
If you print the value of high level
rgl functions, a plot
will be inserted into the output, but maybe only after low level
modifications to it are complete. For example, this code block prints
both triangles and spheres in a single plot at the end:
There are a few differences if you have a complicated situation:
rglfunction calls being automatically printed. If the calls are in a loop or other code block where automatic printing doesn’t happen, you’ll need some trickery to get things to print. For example, this will print three plots:
rglfunctions return results using
highlevel()to mark which kind of plot they are. If you are using a function from another package to produce the plot, you may need to insert an explicit call to one of those to get it to print. Use
lowlevel()if the function just modifies an existing plot,
highlevel()if it starts a new one. For example,
This should display the output at the end of the code chunk, when modifications are assumed complete.
While some PDF previewers support interactive 3D graphics, most
don’t. To produce a screenshot of an
rgl scene in an R
Markdown document with PDF output, simply follow the directions given
above. The auto-printing will detect PDF output and use
snapshot3d to produce a PNG file to insert. (See below if
you want to insert a different format of graphic.)
If you really need interactive output, see the
You may not want to use the
setupKnitr(autoprint = TRUE)
method described above. It is very new, and may still have bugs; you may
have an older document and not want to edit it to work that way.
In this case, you can insert plots manually. Use setup code
rglwidget() at top level whenever you want to
insert a plot.
There are a couple of other differences in default behaviour if you
are not using
By default, each code chunk continues the
from earlier chunks. You’ll need an explicit
open3d call to get a clean
Also by default, the
rgl window is not closed at the
end of the chunk. This probably doesn’t matter, but you may find you run
out of memory if your scenes are really big.
The original way to insert an
rgl scene in a document
was to use the deprecated
writeWebGL function to write HTML
code to insert in a document. Later,
knitr hooks were added. These are no longer supported, and
you should update old documents to use the newer methods. If you are
reading documents that suggest using those methods, let the author know
they need updating!
rgl detects and handles mouse clicks within
your scene, and uses these to control its appearance. You can find out
the current handlers using the following code:
## none left right middle wheel ## "none" "trackball" "zoom" "fov" "pull"
c("left", "right", "middle") refer to the
buttons on a three button mouse, or simulations of them on other mice.
"wheel" refers to the mouse wheel, and
refers to actions that take place when the mouse is moved without
pressing any button.
The button actions generally correspond to click and drag operations.
Possible values for
for the mouse pointer or wheel are as follows:
||The mouse acts as a virtual trackball. Clicking and dragging rotates the scene|
||The mouse affects rotations by controlling polar coordinates directly|
||The mouse is being used by the
||The mouse zooms the display|
||The mouse affects perspective by changing the field of view|
||Rotating the mouse wheel towards the user “pulls the scene closer”|
||The same rotation “pushes the scene away”|
||A user action set by
The following functions make use of the mouse for selection within a scene.
||like the classic graphics
||returns a function that tests whether a coordinate was selected|
||selects from specific objects|
||displays “hover” info about points|
produces the selection function from information about the projection
and mouse selection region; it is used internally in the functions
rgl.select3d function is an obsolete
is a low-level support function.
rgl has several functions that can be used to construct
animations. These are based on functions that update the scene according
to the current real-world time, and repeated calls to those. The
||Repeatedly call the update function|
||Update the display by rotating at a constant rate|
||Compute new values of some
movie3d function for a
way to output an animation to a file on disk.
Animations are not currently supported in the HTML written by
rglwidget, though the
playwidget function provides equivalent functionality.
There are three functions in
rgl that support control of
rgl scene using the TCL/TK framework.
||Set up buttons in a window to control a scene|
||Embed the control buttons in a separate TCL/TK frame|
||Create a dialog to interactively save mouse actions|
These functions were formerly contained (without the
prefixes on their names) in the
tkrgl package. That package
is now deprecated.
rgl contains several functions to write scenes to disk
for use by other software, or to read them in.
In order from highest fidelity to lowest, the functions are:
||Save a scene to an R variable, which can be saved and reloaded|
||Write files for Asymptote|
||Write PLY files (commonly used in 3D printing)|
||Read or write OBJ files (commonly used in 3D graphics)|
||Read or write STL files (also common in 3D printing)|
||Generic function, no methods in
package can read or write GLTF and GLB files. It includes an
as.rglscene method to convert GLTF objects to
rgl scenes. The code in
R6 class is based on the GLTF format. It is used by
rglwidget to make output webpages
somewhat smaller than they were previously.
There are also functions to save snapshots or other recordings of a scene, without any 3D information being saved:
||Save a PNG file bitmap of the scene|
||Save a Postscript, LaTeX, PDF, SVG or PGF vector rendering of the scene|
||Save a series of bitmaps to be assembled into a movie|
||Obtain pixel-level information about the scene in an R variable|
||Driver function for inserting a snapshot into a Sweave document.|
||Function to set up
function is a low level version of
requires that the
rgl display be onscreen and copies from
snapshot3d() tries to use the
package so it will work even with no display. The functions
are involved in Sweave processing and not normally called by users.
There are two ways in which
rgl scenes are normally
displayed within R. The older one is in a dedicated window. In
Unix-alikes this is an X11 window; it is a native window in Microsoft
Windows. On macOS, the XQuartz system (see https://www.xquartz.org)
needs to be installed to support this.
To suppress this display, set
options(rgl.useNULL = TRUE) before opening a new
rgl window. See the help page for the
function for how to set this before starting R.
The newer way to display a scene is by using WebGL in a browser
window or in the Viewer pane in RStudio. To select this, set
options(rgl.printRglwidget = TRUE). Each operation that
would change the scene will return a value which triggers a new WebGL
display when printed.
You should use the following scheme for exporting a scene to a web page. There’s also an older scheme, which is no longer supported.
The recommended approach works with the
framework (see http://www.htmlwidgets.org/). In an R Markdown document
knitr, use the
rglwidget function. (You can also
use chunk option
webgl=TRUE; we recommend the explicit use
rglwidget.) This approach also allows display of
rgl scenes in RStudio.
rgl scenes, various controls for them can be
displayed, and there are a few utility functions that can be useful:
||set individual properties|
||control a clipping plane|
||control which objects are displayed|
||“age” vertices of an object|
||control properties of vertices|
||WebGL control like
||display and automate controls|
||display a button to toggle some items|
||Dimensions of figures in R Markdown document|
||share data using
||change mouse mode in RGL scene|
||arrange multiple objects in an HTML display|
These functions work with the above scheme in Shiny apps:
||get or set
||reset the mouse brush in Shiny|
function is also likely to be involved in mouse interactions when using
Some functions are mainly for internal use:
More details are given in the vignette User
Interaction in WebGL. The functions
are also for internal use, marking function results for automatic
printing. Finally, the function
allows you to use hand-written shaders in WebGL, and
allows you to see what shader would be used.
rgl maintains internal structures for all the scenes it
displays. The following functions allow users to find information about
them and manipulate them. In cases where there are both
rgl.* versions of functions, most users should use the
*3d version: the
rgl.* functions are more
primitive and are mainly intended for internal use.
||open a new window|
||close the current window|
||id of the active device|
||set a particular device to be active|
||delete objects from the scene|
||delete all objects of certain classes|
||ids, types and tags of current objects|
||find tags or objects with tags|
Some of these functions have alternate names for back
name will work, but the
*3d version is recommended for new
code. Some have deprecated versions:
rgl.set. Those should not be called.
These functions are mainly intended for programming, and have no
||bring the current window to the top|
||ids of all active devices|
||attributes of objects in the scene|
||return information about the current projection|
||convert between coordinates in the current projection|
In addition to these, there are some deprecated functions which
should not be called:
rgl functions work internally with “homogeneous”
coordinates. In this system, 3-D points are represented with 4
coordinates, generally called (x, y, z, w). The corresponding Euclidean
point is (x/w, y/w, z/w), if w is nonzero; zero values of w correspond
to “points at infinity”. The advantage of this system is that affine
transformations including translations and perspective shifts become
linear transformations, with multiplication by a 4 by 4 matrix.
rgl has the following functions to work with homogeneous
||convert between homogeneous and Euclidean coordinates when x, y and z are columns|
||convert when x, y and z are rows|
||apply a transformation|
||apply a general transformation|
||compute the transformation matrix|
||return a 4 x 4 identity matrix|
||a 3D to 2D projection down a vector|
For example, we first display the volcano data in
persp3d(volcano, col = "green")
Volcano in rgl
# Only evaluated if the lattice & orientlib packages are installed lattice::wireframe(volcano, col = "green", screen = rglToLattice())
Note that the
orientlib package must be available for
these functions to work.
testthat package is widely used for unit tests in
packages. Such tests are hard to write with
the output is visual and interactive rather than a simple value. The
functions help with this by removing system-dependent features of
The WebGL displays created using
rglwidget rely on a large body of
function was written. It may be useful in other packages that include
This section is for miscellaneous functions and objects that don’t fall in any of the other categories in this document.
function is designed to work around what appears to be a bug on macOS:
if a standard plot window is opened too quickly after an
rgl window, R can crash. This function inserts a one second
delay when it appears to be needed.
vector contains constants used in OpenGL and glTF.
This vignette is always a work in progress. Some aspects of the
rgl package are not described, or do not have examples.
There may even be functions that are missed completely, if the following
list is not empty:
##  "textureSource"
The following functions and constants are described in this