---
title: "Getting started with ggseg"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Getting started with ggseg}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r}
#| 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.

```{r}
#| label: load-packages
library(ggseg)
library(ggplot2)
```

## What's in a brain atlas

A `brain_atlas` object bundles four things: a name, a type (cortical,
subcortical, or tract), region geometry, and an optional colour palette.

```{r}
#| label: dk-atlas
dk()
```

The `dk` atlas (Desikan-Killiany) ships with the package. Call `plot()` for a
quick look:

```{r}
#| 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:

```{r}
#| label: dk-names
library(ggseg.formats)

atlas_regions(dk())
atlas_labels(dk())
```

## Your first brain plot

`geom_brain()` works like any ggplot2 geom. Pass it an atlas and it
draws the regions:

```{r}
#| label: fig-basic-brain
#| fig-cap: "Default brain plot using geom_brain() with the dk atlas."
ggplot() +
  geom_brain(atlas = dk(), show.legend = FALSE)
```

## Arranging views

Brain atlases have multiple views (lateral, medial) and hemispheres.
`position_brain()` controls how they're laid out, using formula
syntax borrowed from `facet_grid()`:

```{r}
#| 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:

```{r}
#| 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.

## Showing only certain hemispheres or views

```{r}
#| label: fig-filter-lateral
#| fig-cap: "Brain plot showing only lateral views."
ggplot() +
  geom_brain(atlas = dk(), view = "lateral", show.legend = FALSE)
```

```{r}
#| 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()`:

```{r}
#| label: aseg-views
atlas_views(aseg())
```

## Plotting your own data

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:

```{r}
#| 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.

## Faceting

ggplot2 faceting works as you'd expect. Group your data by the faceting
variable and `geom_brain()` replicates the full atlas in each panel:

```{r}
#| 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)
```


## Highlighting specific regions

To colour a few regions with their atlas colours, use two columns: one for
the join (`region`) and one for the fill aesthetic:

```{r}
#| 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)
```

## Scales and themes

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:

```{r}
#| label: themes-example
#| eval: false
ggplot() +
  geom_brain(atlas = dk()) +
  theme_brain()
```

## Going further

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")`.

## Finding more atlases

ggseg ships with `dk()` (cortical), `aseg()` (subcortical), `suit()` (cerebellar flamap), and `tracula()`
(white matter tracts). Many more are available through the
[ggsegverse r-universe](https://ggsegverse.r-universe.dev):

```{r}
#| label: install-extra
#| eval: false
install.packages("ggsegYeo2011", repos = "https://ggsegverse.r-universe.dev")
```
