| Title: | Plotting Tool for Brain Atlases |
|---|---|
| Description: | Provides a 'ggplot2' geom and position for visualizing brain region data on cortical, subcortical, and white matter tract atlases. Brain atlas geometries are stored as polygon data, enabling seamless integration with the 'ggplot2' ecosystem including faceting, custom scales, and themes. 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>), Ramiro Magno [ctb] (ORCID: <https://orcid.org/0000-0001-5226-3441>), Center for Lifespan Changes in Brain and Cognition, University of Oslo, Norway [cph] |
| Maintainer: | Athanasia Mo Mowinckel <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 2.2.1.9008 |
| Built: | 2026-10-14 14:24:30 UTC |
| Source: | https://github.com/ggsegverse/ggseg |
Annotates each brain view with a text label positioned above the view's bounding box. For cortical atlases, labels show hemisphere and view (e.g., "left lateral"). For subcortical and tract atlases, labels show the view name directly (e.g., "axial_1", "sagittal").
annotate_brain( atlas, position = position_brain(), hemi = NULL, view = NULL, size = 3, colour = "grey30", family = "mono", padding = 0.05, nudge_y = 0, ... )annotate_brain( atlas, position = position_brain(), hemi = NULL, view = NULL, size = 3, colour = "grey30", family = "mono", padding = 0.05, nudge_y = 0, ... )
atlas |
A |
position |
The same layout you passed to |
hemi |
Character vector of hemispheres to include. If |
view |
Character vector of views to include. If |
size |
Text size in mm (default: |
colour |
Text colour (default: |
family |
Font family (default: |
padding |
Vertical gap between each label and its view, as a
fraction of the plot's total height (default: |
nudge_y |
Additional absolute vertical offset for labels
(default: |
... |
Additional arguments passed to |
Pass the same position you gave geom_brain() and the labels line up with
the views automatically.
A ggplot2 annotation layer.
library(ggplot2) pos <- position_brain(hemi ~ view) ggplot() + geom_brain(atlas = dk(), position = pos, show.legend = FALSE) + annotate_brain(atlas = dk(), position = pos) ggplot() + geom_brain(atlas = dk(), show.legend = FALSE) + annotate_brain(atlas = dk())library(ggplot2) pos <- position_brain(hemi ~ view) ggplot() + geom_brain(atlas = dk(), position = pos, show.legend = FALSE) + annotate_brain(atlas = dk(), position = pos) ggplot() + geom_brain(atlas = dk(), show.legend = FALSE) + annotate_brain(atlas = dk())
Matches your data to a brain atlas by a shared column (usually region),
keeping every atlas region whether or not you have a value for it. Grouped
data (via dplyr::group_by()) gives one complete atlas per group. You
rarely need this directly – geom_brain() joins your data for you; reach
for brain_join() when you want the joined sf object yourself.
brain_join(data, atlas, by = NULL)brain_join(data, atlas, by = NULL)
data |
A data.frame with a column matching an atlas column
(typically |
atlas |
A |
by |
Character vector of column names to join by. If |
An sf object if the atlas contains geometry, otherwise a tibble.
someData <- data.frame( region = sort(unique(ggseg.formats::atlas_regions(dk())))[1:4], p = sample(seq(0, .5, .001), 4), stringsAsFactors = FALSE ) brain_join(someData, dk()) brain_join(someData, dk(), "region")someData <- data.frame( region = sort(unique(ggseg.formats::atlas_regions(dk())))[1:4], p = sample(seq(0, .5, .001), 4), stringsAsFactors = FALSE ) brain_join(someData, dk()) brain_join(someData, dk(), "region")
Builds a minimal, deterministic plot of a ggseg_atlas: every region filled
by its label, no legend, and ggplot2::theme_void(). This is the canonical
construction used across the ggsegverse for visual-regression (vdiffr)
snapshots, so that every atlas is rendered the same way and a stray legend,
axis, or title cannot creep into a snapshot. It doubles as a quick way to
preview an atlas.
brain_test_plot(atlas, position = NULL, na.value = "grey")brain_test_plot(atlas, position = NULL, na.value = "grey")
atlas |
|
position |
A |
na.value |
Fill colour for regions with no palette entry. Defaults to
|
The atlas palette is applied with ggplot2::scale_fill_manual() when it is
present; atlases without a palette fall back to the default ggplot2 fill
scale.
A ggplot2::ggplot() object.
brain_test_plot(dk())brain_test_plot(dk())
Fixes the aspect ratio so brains aren't stretched by the shape of the
plotting window. geom_brain() adds coord_brain() for you, so you rarely
need to call it yourself – reach for it only to adjust the ratio or
clip behaviour. You can safely add your own coord or stack several
geom_brain() layers; it steps aside cleanly.
coord_brain(ratio = 1, clip = "off", ...)coord_brain(ratio = 1, clip = "off", ...)
ratio |
Aspect ratio, expressed as |
clip |
Should drawing be clipped to the panel extent ( |
... |
Additional arguments passed to |
A ggplot2 coordinate system that registers as a default.
library(ggplot2) ## Not run: poly <- ggseg.formats::as_polygon_atlas(dk()) # Equivalent to the default; shown explicitly: ggplot() + geom_brain(atlas = poly, show.legend = FALSE) + coord_brain() ## End(Not run)library(ggplot2) ## Not run: poly <- ggseg.formats::as_polygon_atlas(dk()) # Equivalent to the default; shown explicitly: ggplot() + geom_brain(atlas = poly, show.legend = FALSE) + coord_brain() ## End(Not run)
Colour brain regions by your own values. Give geom_brain() an atlas like
dk() and a data frame, and it matches your values to the right regions and
lays out the brain views for you. No data? It just draws the atlas.
geom_brain( mapping = aes(), data = NULL, atlas, hemi = NULL, view = NULL, position = position_brain(), context = TRUE, fun = mean, show.legend = NA, inherit.aes = TRUE, ... )geom_brain( mapping = aes(), data = NULL, atlas, hemi = NULL, view = NULL, position = position_brain(), context = TRUE, fun = mean, show.legend = NA, inherit.aes = TRUE, ... )
mapping |
Set of aesthetic mappings created by |
data |
A data.frame containing variables to map. If |
atlas |
A |
hemi |
Character vector of hemispheres to include (e.g. |
view |
Character vector of views to include, as recorded in the atlas
data. For cortical atlases: |
position |
Brain-view layout: the result of a call to
|
context |
Keep the rest of the brain as a soft grey backdrop ( |
fun |
Function used to combine multiple |
show.legend |
Logical. Should this layer be included in the legends? |
inherit.aes |
Logical. If |
... |
Additional arguments passed to |
Map neither fill nor colour and geom_brain() colours the atlas by its
own palette for you, by mapping fill to label – the key atlas palettes
are built on, and the ecosystem's canonical matching key. Map either one and
yours wins; your data then drives the colours and regions you supply no
value for stay grey.
Match your data to the atlas on label (hemisphere-qualified, unique per
atlas row) or on region (which repeats across hemispheres).
Regions are drawn in the order they appear in your data, so when outlines
overlap (e.g. mapping colour to a threshold with a wide linewidth) the
later rows draw on top. Reorder your data with dplyr::arrange() to control
the layering; regions you supply no value for stay underneath in atlas order.
Faceting works without any dplyr::group_by(): the atlas geometry is drawn
by StatBrain, which ggplot2 recomputes per panel, so the complete brain
appears in every facet. If your data has several rows for the same region
(e.g. one per subject), they are combined with fun (mean by default) before
drawing.
A list of ggplot2 layer and coord objects.
GeomBrain is the ggplot2::Geom ggproto that renders brain atlas polygons.
It subclasses ggplot2::GeomPolygon and supplies the brain defaults through
default_aes – outline colour (grey35), linewidth (0.2), and fill
(grey) – which apply when the user has not mapped those aesthetics but yield
to a mapping when present (ggsegverse/ggseg#160). The grey default fill is
what an atlas with no palette of its own falls back to; otherwise
geom_brain() maps fill to label and applies the palette. Used
internally by geom_brain(); not typically called directly.
StatBrain is the ggplot2::Stat that powers geom_brain() and
stat_brain(). Its compute_panel() receives one facet panel's user data,
aggregates the mapped values per atlas region with fun, and joins them onto
the full atlas geometry – so every region renders in every panel whether or
not you supplied a value for it. You rarely reference it directly; use it via
geom_brain(..., stat = StatBrain) or stat_brain() when building a custom
layer.
stat_brain() for the stat-first spelling.
library(ggplot2) ggplot() + geom_brain(atlas = dk(), show.legend = FALSE)library(ggplot2) ggplot() + geom_brain(atlas = dk(), show.legend = FALSE)
Controls how an atlas's hemispheres and views are arranged in the plot –
side by side, stacked, or in a grid – and can zoom each view in on the
regions you care about. Pass the result to the position argument of
geom_brain() (or annotate_brain()).
position_brain( position = "horizontal", nrow = NULL, ncol = NULL, views = NULL, zoom = NULL, zoom_pad = 0.05 )position_brain( position = "horizontal", nrow = NULL, ncol = NULL, views = NULL, zoom = NULL, zoom_pad = 0.05 )
position |
Formula describing the rows ~ columns organisation for
cortical atlases (e.g., |
nrow |
Number of rows for grid layout, a positive whole number. If
|
ncol |
Number of columns for grid layout, a positive whole number. If
|
views |
Character vector specifying which views to include and their
order. If |
zoom |
Controls per-view zoom. |
zoom_pad |
Fractional padding added around the focus window when |
A layout specification to hand to geom_brain()'s position
argument.
library(ggplot2) # Cortical atlas with formula ggplot() + geom_brain( atlas = dk(), aes(fill = region), position = position_brain(. ~ view + hemi), show.legend = FALSE ) ggplot() + geom_brain( atlas = dk(), aes(fill = region), position = position_brain(view ~ hemi), show.legend = FALSE ) ggplot() + geom_brain( atlas = aseg(), aes(fill = region), position = position_brain(nrow = 2) ) ggplot() + geom_brain( atlas = aseg(), aes(fill = region), position = position_brain( views = c("sagittal", "axial_3", "coronal_2"), nrow = 1 ) ) ggplot() + geom_brain( atlas = aseg(), aes(fill = region), position = position_brain(type ~ .) )library(ggplot2) # Cortical atlas with formula ggplot() + geom_brain( atlas = dk(), aes(fill = region), position = position_brain(. ~ view + hemi), show.legend = FALSE ) ggplot() + geom_brain( atlas = dk(), aes(fill = region), position = position_brain(view ~ hemi), show.legend = FALSE ) ggplot() + geom_brain( atlas = aseg(), aes(fill = region), position = position_brain(nrow = 2) ) ggplot() + geom_brain( atlas = aseg(), aes(fill = region), position = position_brain( views = c("sagittal", "axial_3", "coronal_2"), nrow = 1 ) ) ggplot() + geom_brain( atlas = aseg(), aes(fill = region), position = position_brain(type ~ .) )
Repositions pre-joined sf atlas data (i.e. data and atlas already joined to a single sf data frame) for control over final plot layout. For even more detailed control, convert the "hemi" and "view" columns into factors ordered by wanted order of appearance.
reposition_brain( data, position = "horizontal", nrow = NULL, ncol = NULL, views = NULL )reposition_brain( data, position = "horizontal", nrow = NULL, ncol = NULL, views = NULL )
data |
sf-data.frame of joined brain atlas and data |
position |
Position formula for slices. For cortical atlases, use
formulas like |
nrow |
Number of rows for grid layout; cannot be combined with a
|
ncol |
Number of columns for grid layout; cannot be combined with a
|
views |
Character vector specifying view order. Names the data does not have are dropped with a warning. |
This is the sf layout helper. It requires the sf package (an optional
dependency); for the sf-free default, build a layout with position_brain()
and pass it to geom_brain().
sf-data.frame with re-positioned slices
reposition_brain(dk(), hemi ~ view) reposition_brain(dk(), view ~ hemi) reposition_brain(dk(), hemi + view ~ .) reposition_brain(dk(), . ~ hemi + view) reposition_brain(aseg(), nrow = 2) reposition_brain(aseg(), views = c("sagittal", "axial_3"))reposition_brain(dk(), hemi ~ view) reposition_brain(dk(), view ~ hemi) reposition_brain(dk(), hemi + view ~ .) reposition_brain(dk(), . ~ hemi + view) reposition_brain(aseg(), nrow = 2) reposition_brain(aseg(), views = c("sagittal", "axial_3"))
Atlas palettes are now applied automatically by geom_brain().
Use scale_fill_brain_manual() for custom palettes.
scale_brain( name = "dk", na.value = "grey", ..., aesthetics = c("fill", "colour", "color") ) scale_colour_brain(name = "dk", na.value = "grey", ...) scale_color_brain(name = "dk", na.value = "grey", ...) scale_fill_brain(name = "dk", na.value = "grey", ...)scale_brain( name = "dk", na.value = "grey", ..., aesthetics = c("fill", "colour", "color") ) scale_colour_brain(name = "dk", na.value = "grey", ...) scale_color_brain(name = "dk", na.value = "grey", ...) scale_fill_brain(name = "dk", na.value = "grey", ...)
name |
String name of the atlas palette (e.g. |
na.value |
Colour for |
... |
Additional arguments passed to |
aesthetics |
Which aesthetic to scale: |
A ggplot2 scale object.
## Not run: library(ggplot2) ggplot() + geom_brain(atlas = dk(), aes(fill = label), show.legend = FALSE) + scale_brain("dk") ## End(Not run)## Not run: library(ggplot2) ggplot() + geom_brain(atlas = dk(), aes(fill = label), show.legend = FALSE) + scale_brain("dk") ## End(Not run)
Apply a custom named colour palette to brain atlas plots. Use this when you want to override the atlas default colours with your own colour mapping.
scale_brain_manual( palette, na.value = "grey", ..., aesthetics = c("fill", "colour", "color") ) scale_colour_brain_manual(...) scale_color_brain_manual(...) scale_fill_brain_manual(...)scale_brain_manual( palette, na.value = "grey", ..., aesthetics = c("fill", "colour", "color") ) scale_colour_brain_manual(...) scale_color_brain_manual(...) scale_fill_brain_manual(...)
palette |
Named character vector mapping region names to colours. |
na.value |
Colour for |
... |
Additional arguments (unused). |
aesthetics |
Which aesthetic to scale: |
A ggplot2 scale object.
library(ggplot2) labels <- sort(unique(ggseg.formats::atlas_labels(dk())))[1:2] pal <- setNames(c("red", "blue"), labels) ggplot() + geom_brain(atlas = dk(), aes(fill = label), show.legend = FALSE) + scale_fill_brain_manual(palette = pal)library(ggplot2) labels <- sort(unique(ggseg.formats::atlas_labels(dk())))[1:2] pal <- setNames(c("red", "blue"), labels) ggplot() + geom_brain(atlas = dk(), aes(fill = label), show.legend = FALSE) + scale_fill_brain_manual(palette = pal)
These functions have been renamed for clarity:
scale_brain2() -> scale_brain_manual()
scale_fill_brain2() -> scale_fill_brain_manual()
scale_colour_brain2() -> scale_colour_brain_manual()
scale_color_brain2() -> scale_color_brain_manual()
scale_brain2(...) scale_colour_brain2(...) scale_color_brain2(...) scale_fill_brain2(...)scale_brain2(...) scale_colour_brain2(...) scale_color_brain2(...) scale_fill_brain2(...)
... |
Arguments passed to the replacement function. |
A ggplot2 scale object.
pal <- c("transversetemporal" = "#FF0000", "insula" = "#00FF00") suppressWarnings(scale_fill_brain2(palette = pal))pal <- c("transversetemporal" = "#FF0000", "insula" = "#00FF00") suppressWarnings(scale_fill_brain2(palette = pal))
Add axis labels and tick labels corresponding to brain atlas regions. These scales add hemisphere or view labels to the x and y axes based on the atlas layout.
scale_continous_brain( atlas = dk(), position = "dispersed", aesthetics = c("y", "x") ) scale_x_brain(...) scale_y_brain(...) scale_labs_brain(atlas = dk(), position = "dispersed", aesthetics = "labs")scale_continous_brain( atlas = dk(), position = "dispersed", aesthetics = c("y", "x") ) scale_x_brain(...) scale_y_brain(...) scale_labs_brain(atlas = dk(), position = "dispersed", aesthetics = "labs")
atlas |
A |
position |
Layout style: |
aesthetics |
Which axis to scale: |
... |
Additional arguments passed to |
A ggplot2 scale or labs object.
library(ggplot2) ggplot() + geom_brain(atlas = dk(), show.legend = FALSE) + scale_x_brain() + scale_y_brain() + scale_labs_brain()library(ggplot2) ggplot() + geom_brain(atlas = dk(), show.legend = FALSE) + scale_x_brain() + scale_y_brain() + scale_labs_brain()
The stat-first spelling of geom_brain(). Both build the same layer – a
StatBrain that aggregates and joins your data onto the atlas geometry,
drawn by geom. Reach for stat_brain() when you want to pair StatBrain
with a different geom (e.g. a custom polygon geom); otherwise geom_brain()
is the friendlier entry point.
stat_brain( mapping = aes(), data = NULL, atlas, hemi = NULL, view = NULL, position = position_brain(), context = TRUE, fun = mean, geom = GeomBrain, show.legend = NA, inherit.aes = TRUE, ... )stat_brain( mapping = aes(), data = NULL, atlas, hemi = NULL, view = NULL, position = position_brain(), context = TRUE, fun = mean, geom = GeomBrain, show.legend = NA, inherit.aes = TRUE, ... )
mapping |
Set of aesthetic mappings created by |
data |
A data.frame containing variables to map. If |
atlas |
A |
hemi |
Character vector of hemispheres to include (e.g. |
view |
Character vector of views to include, as recorded in the atlas
data. For cortical atlases: |
position |
Brain-view layout: the result of a call to
|
context |
Keep the rest of the brain as a soft grey backdrop ( |
fun |
Function used to combine multiple |
geom |
The geometric object used to render the atlas. Defaults to GeomBrain. |
show.legend |
Logical. Should this layer be included in the legends? |
inherit.aes |
Logical. If |
... |
Additional arguments passed to |
A list of ggplot2 layer and coord objects.
library(ggplot2) # Equivalent to geom_brain(atlas = dk()) ggplot() + stat_brain(atlas = dk(), show.legend = FALSE)library(ggplot2) # Equivalent to geom_brain(atlas = dk()) ggplot() + stat_brain(atlas = dk(), show.legend = FALSE)
A set of ggplot2 themes designed for brain atlas visualisations. All themes remove axis ticks and grid lines for a clean presentation.
theme_brain(text.size = 12, text.family = "mono") theme_darkbrain(text.size = 12, text.family = "mono") theme_custombrain( plot.background = "white", text.colour = "darkgrey", text.size = 12, text.family = "mono" ) theme_brain2( plot.background = "white", text.colour = "darkgrey", text.size = 12, text.family = "mono" )theme_brain(text.size = 12, text.family = "mono") theme_darkbrain(text.size = 12, text.family = "mono") theme_custombrain( plot.background = "white", text.colour = "darkgrey", text.size = 12, text.family = "mono" ) theme_brain2( plot.background = "white", text.colour = "darkgrey", text.size = 12, text.family = "mono" )
text.size |
Text size in points (default: |
text.family |
Font family (default: |
plot.background |
Background fill colour ( |
text.colour |
Text colour ( |
theme_brainDefault theme. Transparent background, no axes, no grid.
theme_darkbrainDark theme with black background and light text.
theme_custombrainFully customisable background, text colour, size, and font.
theme_brain2Like theme_custombrain but with axis text removed entirely.
A ggplot2::theme object.
geom_brain(), ggplot2::theme()
library(ggplot2) p <- ggplot() + geom_brain(atlas = dk(), show.legend = FALSE) p + theme_brain() p + theme_darkbrain()library(ggplot2) p <- ggplot() + geom_brain(atlas = dk(), show.legend = FALSE) p + theme_brain() p + theme_darkbrain()