#| 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 as simple features 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 stored as simple features, 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 ggseg.formats::atlas_regions(dk()) ggseg.formats::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())
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))
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" )) )
See vignette("positioning-views") for the full set of layout options,
including grid layouts for subcortical and tract atlases.
#| label: fig-filter-lateral #| fig-cap: "Brain plot showing only lateral views." ggplot() + geom_brain(atlas = dk(), view = "lateral")
#| 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 ggseg.formats::atlas_views(aseg())
Create a data frame with at least one column that matches the atlas
(typically region or label). geom_brain() joins your data 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, .5, .001), 4) ) ggplot(some_data) + geom_brain( atlas = dk(), 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. geom_brain() detects faceting
variables and 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, .5, .001), 8), group = c(rep("A", 4), rep("B", 4)) ) ggplot(some_data) + geom_brain( atlas = dk(), position = position_brain(hemi ~ view), aes(fill = p) ) + facet_wrap(~group)
No group_by() needed -- the geom handles it.
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 = ggseg.formats::atlas_regions(dk())[1:3], reg_col = ggseg.formats::atlas_regions(dk())[1:3] ) ggplot(data) + geom_brain(atlas = dk(), aes(fill = reg_col)) + scale_fill_brain_manual(dk()$palette[data$region])
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()
ggseg ships with dk (cortical), aseg (subcortical), 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.