| Title: | Interactive 3D Brain Atlas Visualization |
|---|---|
| Description: | Plot brain atlases as interactive 3D meshes using 'Three.js' via 'htmlwidgets', or render publication-quality static images through 'rgl' and 'rayshader'. A pipe-friendly API lets you map data onto brain regions, control camera angles, toggle region edges, overlay glass brains, and snapshot or ray-trace the result. Additional atlases are available through the 'ggsegverse' r-universe. Mowinckel & Vidal-Piñeiro (2020) <doi:10.1177/2515245920928009>. |
| Authors: | Athanasia Mo Mowinckel [aut, cre] (ORCID: <https://orcid.org/0000-0002-5756-0223>), Didac Vidal-Piñeiro [aut] (ORCID: <https://orcid.org/0000-0001-9997-9156>), Center for Lifespan Changes in Brain and Cognition (LCBC), University of Oslo [cph], three.js authors [ctb, cph] (Three.js and OrbitControls libraries, see LICENSE.note) |
| Maintainer: | Athanasia Mo Mowinckel <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 2.1.2.9004 |
| Built: | 2026-10-14 18:53:22 UTC |
| Source: | https://github.com/ggsegverse/ggseg3d |
Adds a translucent brain surface to a ggseg3d plot for anatomical reference.
Particularly useful for subcortical and tract visualizations where spatial
context helps interpretation. Works with both htmlwidget (ggseg3d) and
rgl (ggsegray) objects. The default "inflated" surface ships with
ggseg.formats; every other surface is drawn from ggseg.meshes.
add_glassbrain( p, hemisphere = c("left", "right"), surface = "inflated", colour = "#CCCCCC", opacity = 0.3, brain_meshes = NULL )add_glassbrain( p, hemisphere = c("left", "right"), surface = "inflated", colour = "#CCCCCC", opacity = 0.3, brain_meshes = NULL )
p |
A |
hemisphere |
Character vector. Hemispheres to add: |
surface |
Surface type: |
colour |
Character. Colour for the glass brain surface (hex or named). |
opacity |
Numeric. Transparency of the glass brain (0-1). |
brain_meshes |
Optional user-supplied mesh data. Passed through to
|
The input object (modified), for piping.
Other brain modifiers:
pan_camera(),
set_background(),
set_dimensions(),
set_edges(),
set_flat_shading(),
set_legend(),
set_orthographic(),
set_positioning()
ggseg3d(atlas = aseg()) |> add_glassbrain("left", opacity = 0.2) ## Not run: # rgl requires OpenGL; not run in check environments. ggsegray(atlas = aseg()) |> add_glassbrain(opacity = 0.15) |> pan_camera("right lateral") ## End(Not run)ggseg3d(atlas = aseg()) |> add_glassbrain("left", opacity = 0.2) ## Not run: # rgl requires OpenGL; not run in check environments. ggsegray(atlas = aseg()) |> add_glassbrain(opacity = 0.15) |> pan_camera("right lateral") ## End(Not run)
ggseg3d creates and returns an interactive Three.js brain mesh
visualization. Dispatches to atlas-type-specific methods via
prepare_brain_meshes().
ggseg3d( .data = NULL, atlas = dk(), label_by = "region", text_by = NULL, colour_by = "colour", palette = NULL, na_colour = "darkgrey", na_alpha = 1, ..., label = deprecated(), text = deprecated(), colour = deprecated() )ggseg3d( .data = NULL, atlas = dk(), label_by = "region", text_by = NULL, colour_by = "colour", palette = NULL, na_colour = "darkgrey", na_alpha = 1, ..., label = deprecated(), text = deprecated(), colour = deprecated() )
.data |
A data.frame to use for plot aesthetics. Must include a column called "region" corresponding to regions. |
atlas |
A |
label_by |
String. Column name used as hover label for each region. |
text_by |
String. Column name for extra hover text shown below the region label. |
colour_by |
String. Column name mapped to mesh colours. |
palette |
Character vector of colour names or hex colours. May instead be a named numeric vector whose names are the colours and whose values are the breakpoints those colours sit at. |
na_colour |
String. Colour drawn for regions with no value in
|
na_alpha |
Numeric between 0 and 1. Opacity of regions with no
value in |
... |
Type-specific arguments passed to the atlas method. See section Type-specific arguments below. |
label, text, colour
|
an htmlwidget object for interactive 3D brain visualization
Cortical atlases (cortical_atlas):
surface"LCBC" (default, an alias for "inflated") or any
surface resolve_brain_mesh() accepts.
hemisphereCharacter vector of hemispheres: "left",
"right", or their "lh"/"rh" spellings.
edge_byColumn name for region boundary edges.
brain_meshesCustom brain mesh data.
Cerebellar atlases (cerebellar_atlas):
surface_opacityOpacity of the cerebellar surface mesh.
Defaults to 0.3 when the atlas carries deep nuclei, so they stay
visible through the surface, and 1 otherwise.
Tract atlases (tract_atlas):
tract_color"palette" (default) or "orientation"
(direction-based RGB).
tube_radiusTube radius (numeric, default 2).
tube_segmentsTube segment count (integer, default 10).
Athanasia Mowinckel and Didac Piñeiro
pan_camera() for camera position, set_background() for
background colour, set_legend() for legend visibility
Other brain plots:
ggsegray()
ggseg3d() ggseg3d(hemisphere = "left") |> pan_camera("left lateral") ggseg3d() |> set_legend(FALSE) ggseg3d() |> set_background("black")ggseg3d() ggseg3d(hemisphere = "left") |> pan_camera("left lateral") ggseg3d() |> set_legend(FALSE) ggseg3d() |> set_background("black")
Output and render functions for using ggseg3d within Shiny applications and interactive R Markdown documents.
ggseg3dOutput(outputId, width = "100%", height = "400px") renderGgseg3d(expr, env = parent.frame(), quoted = FALSE)ggseg3dOutput(outputId, width = "100%", height = "400px") renderGgseg3d(expr, env = parent.frame(), quoted = FALSE)
outputId |
output variable to read from |
width, height
|
Must be a valid CSS unit (like |
expr |
An expression that generates a ggseg3d |
env |
The environment in which to evaluate |
quoted |
Is |
ggseg3dOutput returns an HTML widget output element for use in
a Shiny UI. renderGgseg3d returns a render function for use in a
Shiny server.
library(shiny) ui <- fluidPage(ggseg3dOutput("brain")) server <- function(input, output) { output$brain <- renderGgseg3d(ggseg3d()) }library(shiny) ui <- fluidPage(ggseg3dOutput("brain")) server <- function(input, output) { output$brain <- renderGgseg3d(ggseg3d()) }
Creates an rgl 3D scene from a brain atlas. Uses the same atlas
preparation pipeline as ggseg3d() but outputs to rgl instead of
htmlwidgets. The resulting scene can be piped into pan_camera(),
add_glassbrain(), and set_background(), then rendered with
rayshader's render_highquality() or captured with rgl::snapshot3d().
ggsegray( .data = NULL, atlas = dk(), label_by = "region", text_by = NULL, colour_by = "colour", palette = NULL, na_colour = "darkgrey", na_alpha = 1, material = list(), ..., label = deprecated(), text = deprecated(), colour = deprecated() )ggsegray( .data = NULL, atlas = dk(), label_by = "region", text_by = NULL, colour_by = "colour", palette = NULL, na_colour = "darkgrey", na_alpha = 1, material = list(), ..., label = deprecated(), text = deprecated(), colour = deprecated() )
.data |
A data.frame to use for plot aesthetics. Must include a column called "region" corresponding to regions. |
atlas |
A |
label_by |
String. Column name used as hover label for each region. |
text_by |
String. Column name for extra hover text shown below the region label. |
colour_by |
String. Column name mapped to mesh colours. |
palette |
Character vector of colour names or hex colours. May instead be a named numeric vector whose names are the colours and whose values are the breakpoints those colours sit at. |
na_colour |
String. Colour drawn for regions with no value in
|
na_alpha |
Numeric between 0 and 1. Opacity of regions with no
value in |
material |
Named list of rgl material properties passed to
|
... |
Type-specific arguments passed to the atlas method. See section Type-specific arguments below. |
label, text, colour
|
An object of class ggsegray (invisibly), which wraps the
rgl device ID. Pipe into pan_camera(), add_glassbrain(), or
set_background() to modify the scene.
Useful material list entries:
specular"black" (matte) or "white" (glossy).
shininessSpecular exponent. Higher = tighter highlights.
litFALSE disables lighting.
alphaTransparency, 0 (invisible) to 1 (opaque).
smoothTRUE for Gouraud shading, FALSE for flat.
See rgl::material3d() for the full list.
Cortical atlases (cortical_atlas):
surface"LCBC" (default, an alias for "inflated") or any
surface resolve_brain_mesh() accepts.
hemisphereCharacter vector of hemispheres: "left",
"right", or their "lh"/"rh" spellings.
edge_byColumn name for region boundary edges.
brain_meshesCustom brain mesh data.
Cerebellar atlases (cerebellar_atlas):
surface_opacityOpacity of the cerebellar surface mesh.
Defaults to 0.3 when the atlas carries deep nuclei, so they stay
visible through the surface, and 1 otherwise.
Tract atlases (tract_atlas):
tract_color"palette" (default) or "orientation"
(direction-based RGB).
tube_radiusTube radius (numeric, default 2).
tube_segmentsTube segment count (integer, default 10).
Other brain plots:
ggseg3d()
## Not run: # rgl requires OpenGL; not run in check environments. ggsegray(hemisphere = "left") |> pan_camera("left lateral") ggsegray(atlas = aseg()) |> add_glassbrain(opacity = 0.15) |> pan_camera("right lateral") |> set_background("black") ## End(Not run)## Not run: # rgl requires OpenGL; not run in check environments. ggsegray(hemisphere = "left") |> pan_camera("left lateral") ggsegray(atlas = aseg()) |> add_glassbrain(opacity = 0.15) |> pan_camera("right lateral") |> set_background("black") ## End(Not run)
Sets the camera position for a ggseg3d widget or ggsegray rgl scene to standard anatomical views or custom positions.
pan_camera(p, camera)pan_camera(p, camera)
p |
A |
camera |
string, list, or numeric vector. Camera position preset
name, custom eye position list, or Available camera presets:
|
The input object (modified), for piping.
Other brain modifiers:
add_glassbrain(),
set_background(),
set_dimensions(),
set_edges(),
set_flat_shading(),
set_legend(),
set_orthographic(),
set_positioning()
ggseg3d() |> pan_camera("right lateral") ## Not run: # rgl requires OpenGL; not run in check environments. ggsegray(atlas = dk(), hemisphere = "left") |> pan_camera("left lateral") ## End(Not run)ggseg3d() |> pan_camera("right lateral") ## Not run: # rgl requires OpenGL; not run in check environments. ggsegray(atlas = dk(), hemisphere = "left") |> pan_camera("left lateral") ## End(Not run)
Resolves and prepares a brain surface mesh for rendering. Delegates to
ggseg.formats::get_brain_mesh() for inflated surfaces and to
ggseg.meshes::get_cortical_mesh() for pial, white, semi-inflated, and
other surfaces. Returned meshes use the shared anatomical axis convention
(x = left-right, y = anterior-posterior, z = superior-inferior) with
lh positioned at x <= 0 and rh at x >= 0, medial edges meeting at
the midline (x = 0).
resolve_brain_mesh( hemisphere = c("lh", "rh"), surface = c("inflated", "semi-inflated", "white", "pial", "midthickness", "sphere", "smoothwm", "orig"), brain_meshes = NULL )resolve_brain_mesh( hemisphere = c("lh", "rh"), surface = c("inflated", "semi-inflated", "white", "pial", "midthickness", "sphere", "smoothwm", "orig"), brain_meshes = NULL )
hemisphere |
|
surface |
Surface type: |
brain_meshes |
Optional user-supplied mesh data. Passed through to
|
list with vertices (data.frame with x, y, z) and faces (data.frame with i, j, k), or NULL if mesh not found
mesh <- resolve_brain_mesh("lh", "inflated") str(mesh, max.level = 1)mesh <- resolve_brain_mesh("lh", "inflated") str(mesh, max.level = 1)
Changes the background color of a ggseg3d widget or ggsegray rgl scene.
set_background(p, colour = "#ffffff")set_background(p, colour = "#ffffff")
p |
A |
colour |
string. Background color (hex or named color) |
The input object (modified), for piping.
Other brain modifiers:
add_glassbrain(),
pan_camera(),
set_dimensions(),
set_edges(),
set_flat_shading(),
set_legend(),
set_orthographic(),
set_positioning()
ggseg3d() |> set_background("black") ## Not run: # rgl requires OpenGL; not run in check environments. ggsegray(atlas = dk()) |> set_background("black") ## End(Not run)ggseg3d() |> set_background("black") ## Not run: # rgl requires OpenGL; not run in check environments. ggsegray(atlas = dk()) |> set_background("black") ## End(Not run)
Changes the width and height of a ggseg3d widget.
set_dimensions(p, width = NULL, height = NULL)set_dimensions(p, width = NULL, height = NULL)
p |
ggseg3d widget object |
width |
numeric. Widget width in pixels (NULL for default) |
height |
numeric. Widget height in pixels (NULL for default) |
ggseg3d widget object with updated dimensions
Other brain modifiers:
add_glassbrain(),
pan_camera(),
set_background(),
set_edges(),
set_flat_shading(),
set_legend(),
set_orthographic(),
set_positioning()
ggseg3d() |> set_dimensions(width = 800, height = 600)ggseg3d() |> set_dimensions(width = 800, height = 600)
Adds coloured outlines around brain regions. This is useful for
highlighting region boundaries in figures. Works with both
htmlwidget (ggseg3d) and rgl (ggsegray) objects. For rgl,
edges must have been computed at creation time via edge_by.
set_edges(p, colour = "black", width = 1)set_edges(p, colour = "black", width = 1)
p |
A |
colour |
string. Edge colour (hex or named color). Set to NULL to hide edges. |
width |
numeric. Width of edge lines (default: 1). Note: line width > 1 may not render on all systems due to WebGL limitations. |
The input object (modified), for piping.
Other brain modifiers:
add_glassbrain(),
pan_camera(),
set_background(),
set_dimensions(),
set_flat_shading(),
set_legend(),
set_orthographic(),
set_positioning()
ggseg3d(hemisphere = "left", edge_by = "region") |> set_edges("black") |> pan_camera("left lateral") ## Not run: # rgl requires OpenGL; not run in check environments. ggsegray(hemisphere = "left", edge_by = "region") |> set_edges("red", width = 2) |> pan_camera("left lateral") ## End(Not run)ggseg3d(hemisphere = "left", edge_by = "region") |> set_edges("black") |> pan_camera("left lateral") ## Not run: # rgl requires OpenGL; not run in check environments. ggsegray(hemisphere = "left", edge_by = "region") |> set_edges("red", width = 2) |> pan_camera("left lateral") ## End(Not run)
Disables lighting effects to show colors exactly as specified. Useful for screenshots where accurate color reproduction is needed, such as atlas creation pipelines that extract contours from images.
set_flat_shading(p, flat = TRUE)set_flat_shading(p, flat = TRUE)
p |
ggseg3d widget object |
flat |
logical. Enable flat shading (default: TRUE) |
ggseg3d widget object with updated shading
Other brain modifiers:
add_glassbrain(),
pan_camera(),
set_background(),
set_dimensions(),
set_edges(),
set_legend(),
set_orthographic(),
set_positioning()
ggseg3d() |> set_flat_shading()ggseg3d() |> set_flat_shading()
For htmlwidget output, toggles legend visibility. For rgl output, draws or removes the legend overlay.
set_legend(p, show = TRUE)set_legend(p, show = TRUE)
p |
A |
show |
logical. Whether to show the legend (default: TRUE) |
The input object, modified
Other brain modifiers:
add_glassbrain(),
pan_camera(),
set_background(),
set_dimensions(),
set_edges(),
set_flat_shading(),
set_orthographic(),
set_positioning()
ggseg3d() |> set_legend(FALSE) ## Not run: # rgl requires OpenGL; not run in check environments. ggsegray(hemisphere = "left") |> set_legend() ## End(Not run)ggseg3d() |> set_legend(FALSE) ## Not run: # rgl requires OpenGL; not run in check environments. ggsegray(hemisphere = "left") |> set_legend() ## End(Not run)
Uses orthographic projection instead of perspective. This eliminates perspective distortion and ensures consistent sizing across all views.
set_orthographic(p, ortho = TRUE, frustum_size = 220)set_orthographic(p, ortho = TRUE, frustum_size = 220)
p |
ggseg3d widget object |
ortho |
logical. Enable orthographic mode (default: TRUE) |
frustum_size |
numeric. Size of the orthographic frustum. Controls how much of the scene is visible. Default 220 works well for brain meshes. Use the same value across all views for consistent sizing. |
ggseg3d widget object with updated camera mode
Other brain modifiers:
add_glassbrain(),
pan_camera(),
set_background(),
set_dimensions(),
set_edges(),
set_flat_shading(),
set_legend(),
set_positioning()
ggseg3d() |> set_orthographic()ggseg3d() |> set_orthographic()
Repositions cortical hemisphere meshes in a ggseg3d widget. Meshes are produced with anatomical positioning by default (medial edges at x = 0). Use this to centre each hemisphere at the origin for atlas-creation snapshots, or to reapply anatomical positioning after manual edits.
set_positioning(p, positioning = c("anatomical", "centered"))set_positioning(p, positioning = c("anatomical", "centered"))
p |
ggseg3d widget object |
positioning |
How to position hemispheres:
|
Only cortical hemisphere meshes are repositioned (those named
"<hemi> <surface>", e.g. "left inflated"). Subcortical, cerebellar
and glassbrain meshes are left untouched.
ggseg3d widget object with repositioned meshes
Other brain modifiers:
add_glassbrain(),
pan_camera(),
set_background(),
set_dimensions(),
set_edges(),
set_flat_shading(),
set_legend(),
set_orthographic()
# View both hemispheres anatomically positioned (default) ggseg3d(hemisphere = c("left", "right")) |> pan_camera("left lateral") # Atlas creation: centered for consistent sizing ggseg3d(hemisphere = "left") |> set_positioning("centered") |> set_orthographic() |> pan_camera("left lateral")# View both hemispheres anatomically positioned (default) ggseg3d(hemisphere = c("left", "right")) |> pan_camera("left lateral") # Atlas creation: centered for consistent sizing ggseg3d(hemisphere = "left") |> set_positioning("centered") |> set_orthographic() |> pan_camera("left lateral")
Takes a screenshot of a ggseg3d widget and saves it as a PNG image. Requires a Chrome-based browser to be installed.
snapshot_brain(p, file, width = 600, height = 500, delay = 1, zoom = 2, ...)snapshot_brain(p, file, width = 600, height = 500, delay = 1, zoom = 2, ...)
p |
ggseg3d widget object |
file |
string. Output file path (should end in .png) |
width |
numeric. Image width in pixels (default: 600) |
height |
numeric. Image height in pixels (default: 500) |
delay |
numeric. Seconds to wait for widget to render before capture (default: 1) |
zoom |
numeric. Zoom factor for higher resolution (default: 2) |
... |
Additional arguments passed to webshot2::webshot |
The file path (invisibly)
## Not run: ggseg3d() |> pan_camera("left lateral") |> snapshot_brain("brain.png") ## End(Not run)## Not run: ggseg3d() |> pan_camera("left lateral") |> snapshot_brain("brain.png") ## End(Not run)
Sends a message to update the background color of a ggseg3d widget in a Shiny app.
updateGgseg3dBackground(session, outputId, colour)updateGgseg3dBackground(session, outputId, colour)
session |
The Shiny session object |
outputId |
The output ID of the ggseg3d widget |
colour |
Background color (hex or named color) |
None, called for side effects (sends message to client)
## Not run: updateGgseg3dBackground(session, "brain", "black") ## End(Not run)## Not run: updateGgseg3dBackground(session, "brain", "black") ## End(Not run)
Sends a message to update the camera position of a ggseg3d widget in a Shiny app.
updateGgseg3dCamera(session, outputId, camera)updateGgseg3dCamera(session, outputId, camera)
session |
The Shiny session object |
outputId |
The output ID of the ggseg3d widget |
camera |
Camera position preset or custom position |
None, called for side effects (sends message to client)
## Not run: updateGgseg3dCamera(session, "brain", "left lateral") ## End(Not run)## Not run: updateGgseg3dCamera(session, "brain", "left lateral") ## End(Not run)