---
title: "Using geom_brain"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Using geom_brain}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 6
)
```

The new ggseg-package version has introduced a new way of plotting the brain atlases, through a custom `geom_brain` (variant of geom_sf). 
This has introduced a lot of new functionality into the package, in addition to some new custom methods and objects. 

```{r setup}
library(ggseg)
library(ggplot2)

```


# The brain-atlas class

The first new thing to notice is that we have introduced a new atlas class called `brain-atlas`. 
This class is a special class for ggseg-atlases, that contain information in a specific way. 
They are objects with 4-levels, each containing important information about the atlas in question.

```{r}
dk$atlas
dk$type
dk$palette
dk$data
```

Of these four, only the `palette` is an optional part, where some atlases may have this field empty.
The data, you might notice, is simple-features data, with a `geometry` column that includes all the information needed to plot the data as a simple features object. 
You can actually call `plot` directly on the data, and the standard simple features plot will appear.

```{r}
plot(dk$data)
```

Even better, though, you should call `plot` directly on the atlas object. 
This will give you a fast overview of the atlas you are thinking of using.

```{r}
plot(dk)
```

You will notice that the new atlas-class has better resolution and default values that what you get from the ggseg-atlas class. 

## Extracting atlas information
This new class also comes with a new custom printout method, that should give you a better idea of the atlas content.
It lists information such as:

- type of atlas  
- number of unique regions  
- hemispheres  
- slice views  
- whether it has a built-in palette  

And in addition it has a preview of the data content, so you may more easily discern how you might adapt your own data to fit the atlas data.

```{r}
dk
```

Some users have also wanted to easier ways of checking the names of regions and labels of an atlas, in order to check if their data fits the atlas data. 
In order to make this easier, we have added two new functions that should help you with that.

```{r}
brain_regions(dk)
brain_labels(dk)
```

## Plotting the atlas
For other than quick overviews of the atlas using `plot` this new atlas class is specifically made to work with the new `geom_brain`. 
Since we have better control over the geom, we have also optimised it so that when plotting just the atlas, without specifying `fill` the polygons are automatically filled with the `region` column.

```{r}
ggplot() +
  geom_brain(atlas = dk)
```

This new geom makes it possible for you to also better control the position of the brain slices, using specialised function for this to the position argument. The `position_brain` function takes a formula argument similar to that of `facet_grid` to alter the positions of the slices.

```{r}
ggplot() +
  geom_brain(atlas = dk, position = position_brain(hemi ~ side))
```

A new addition to the positions, is the ability to also specify the order directly through a character vector. By default, the position is:

```{r}
cortical_pos <- c("left lateral", "left medial", "right medial", "right lateral")
ggplot() +
  geom_brain(atlas = dk, position = position_brain(cortical_pos))

# Which can easily be switched around!
cortical_pos <- c("right lateral", "left medial", "right medial", "left lateral")
ggplot() +
  geom_brain(atlas = dk, position = position_brain(cortical_pos))
```

## Reducing slices

Many have wanted the option like in `ggseg()` to only see a single hemisphere or slice. This functionality had been added through the `hemi` and `side` arguments to `geom_brain()`, mimicking the way `ggseg()` works.

```{r}
ggplot() +
  geom_brain(atlas = dk, side = "lateral")

ggplot() +
  geom_brain(atlas = dk, hemi = "left")
```

This also should work for subcortical atlases, but the hemisphere (`hemi`) specification should be used carefully, as it might end up looking quite different than what you intended!

```{r}
ggplot() +
  geom_brain(atlas = aseg, side = "axial", hemi = "left")
```


## Plotting with data
Of course, as usual, people will have their own data they want to add to the plots, using columns from their own data to the plot aesthetics. 
By making sure _at least one column_ in your data has the same name and overlapping content as a column in the atlas data, geom_brain will merge your data with the atlas and create your plots. 

```{r}
library(dplyr)
        
someData = tibble(
  region = c("transverse temporal", "insula",
           "precentral","superior parietal"), 
  p = sample(seq(0,.5,.001), 4)
)

someData
```

And such plots can be further adapted with standard ggplot themes, scales etc, to your liking.

```{r}
ggplot(someData) +
  geom_brain(atlas = dk, 
             position = position_brain(hemi ~ side),
             aes(fill = p)) +
  scale_fill_viridis_c(option = "cividis", direction = -1) +
  theme_void() +
  labs(title = "My awesome title", 
       subtitle = "of a brain atlas plot",
       caption = "I'm pretty happy about this!")
```

### Facet group data

Just like in ggseg, though, you still need to do some double work for faceting to work correctly.
Because the atlas and your data need to be merged correctly, you will need to `group_by` your data _before_ giving it to ggplot, for facets to work.

```{r}
someData <- tibble(
  region = rep(c("transverse temporal", "insula",
           "precentral","superior parietal"), 2), 
  p = sample(seq(0,.5,.001), 8),
  groups = c(rep("g1", 4), rep("g2", 4))
)

someData
```


```{r}
someData %>%
  group_by(groups) %>%
  ggplot() +
  geom_brain(atlas = dk, 
             position = position_brain(hemi ~ side),
             aes(fill = p)) +
  facet_wrap(~groups) +
  ggtitle("correct facetting")
```

## Plotting with ggseg

You can also plot this new atlas class directly with the `ggseg` function, if you are more comfortable with that.

```{r}
ggseg(someData, atlas = dk, 
      colour = "black",
      size = .1, 
      position = "stacked",
      mapping = aes(fill = p))
```


