#| label: setup #| include: false knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.width = 6 )
If you work with neuroimaging data, you've probably spent time wrestling region-level results into brain plots. ggseg handles that plumbing for you. It stores brain atlas geometries and plots them as ggplot2 layers, so you get brain figures with the same code you'd use for any other ggplot.
#| label: load-packages library(ggseg) library(ggplot2)
A brain_atlas object bundles four things: a name, a type (cortical,
subcortical, or tract), region geometry, and an optional colour palette.
#| label: dk-atlas dk()
The dk atlas (Desikan-Killiany) ships with the package. Call plot() for a
quick look:
#| label: fig-dk-plot #| fig-cap: "Quick overview of the Desikan-Killiany cortical atlas." plot(dk())
Two helpers pull out the names you'll need for matching data:
#| label: dk-names library(ggseg.formats) atlas_regions(dk()) atlas_labels(dk())
geom_brain() works like any ggplot2 geom. Pass it an atlas and it
draws the regions:
#| label: fig-basic-brain #| fig-cap: "Default brain plot using geom_brain() with the dk atlas." ggplot() + geom_brain(atlas = dk(), show.legend = FALSE)
Brain atlases have multiple views (lateral, medial) and hemispheres.
position_brain() controls how they're laid out, using formula
syntax borrowed from facet_grid():
#| label: fig-position-formula #| fig-cap: "Brain views arranged using formula syntax in position_brain()." ggplot() + geom_brain( atlas = dk(), position = position_brain(hemi ~ view), show.legend = FALSE )
Or reorder views with a character vector:
#| label: fig-position-character #| fig-cap: "Brain views arranged using a character vector to specify order." ggplot() + geom_brain( atlas = dk(), position = position_brain(c( "right lateral", "right medial", "left lateral", "left medial" )), show.legend = FALSE )
See vignette("positioning-views") for the full set of layout options,
including grid layouts for subcortical and tract atlases and per-view zoom.
#| label: fig-filter-lateral #| fig-cap: "Brain plot showing only lateral views." ggplot() + geom_brain(atlas = dk(), view = "lateral", show.legend = FALSE)
#| label: fig-filter-left #| fig-cap: "Brain plot showing only the left hemisphere." ggplot() + geom_brain(atlas = dk(), hemi = "left")
For subcortical atlases, views are slice identifiers. Check what's available
with ggseg.formats::atlas_views():
#| label: aseg-views atlas_views(aseg())
Create a data frame with at least one column that matches the atlas
(typically region or label). Pass it to geom_brain() through
the data argument and it joins to the atlas automatically:
#| label: fig-external-data #| fig-cap: "Brain plot coloured by external p-values using a viridis scale." library(dplyr) some_data <- tibble( region = c( "transverse temporal", "insula", "precentral", "superior parietal" ), p = sample(seq(0, 0.5, 0.001), 4) ) ggplot() + geom_brain( atlas = dk(), data = some_data, position = position_brain(hemi ~ view), aes(fill = p) ) + scale_fill_viridis_c(option = "cividis", direction = -1) + theme_void()
See vignette("external-data") for more on data preparation and matching.
ggplot2 faceting works as you'd expect. Group your data by the faceting
variable and geom_brain() replicates the full atlas in each panel:
#| label: fig-faceting #| fig-cap: "Brain plots faceted by group, each panel showing the full atlas." some_data <- tibble( region = rep( c( "transverse temporal", "insula", "precentral", "superior parietal" ), 2 ), p = sample(seq(0, 0.5, 0.001), 8), group = c(rep("A", 4), rep("B", 4)) ) ggplot() + geom_brain( atlas = dk(), data = group_by(some_data, group), position = position_brain(hemi ~ view), aes(fill = p) ) + facet_wrap(~group)
To colour a few regions with their atlas colours, use two columns: one for
the join (region) and one for the fill aesthetic:
#| label: fig-highlighting #| fig-cap: "Highlighting selected regions using the atlas colour palette." data <- data.frame( region = atlas_regions(dk())[1:3], reg_col = atlas_labels(dk())[1:3] ) palette <- atlas_palette(dk())[1:3] ggplot() + geom_brain(atlas = dk(), data = data, aes(fill = reg_col)) + scale_fill_brain_manual(palette)
Atlas colour palettes are applied automatically by geom_brain().
For custom palettes, use scale_fill_brain_manual().
Built-in themes strip axes and grids for cleaner figures:
#| label: themes-example #| eval: false ggplot() + geom_brain(atlas = dk()) + theme_brain()
For full control over the data before it reaches ggplot2 — adding region
labels, layering other sf geoms, or using geom_sf() directly — see
vignette("geom-sf").
ggseg ships with dk() (cortical), aseg() (subcortical), suit() (cerebellar flamap), and tracula()
(white matter tracts). Many more are available through the
ggsegverse r-universe:
#| label: install-extra #| eval: false install.packages("ggsegYeo2011", repos = "https://ggsegverse.r-universe.dev")
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.