Chapter 4 FreeSurfer

4.1 recon-all

There are three main components to FreeSurfer’s output: quality control measure, cortical parcellation, subcortical segmentation. If available, longitudinal analyses of all the regions can also be provided.

1. FreeSurfer’s Euler number summarizes the topological complexity of the reconstructed cortical surface. The Euler number can be used to identify images as unusable and is a highly reliable measure of image quality.
2. From FreeSurfer’s cortical parcellation several measures can be obtained from within each region: surface area, cortical thickness, gray matter volume, white matter volume, and cortical mean curvature.
3. From FreeSurfer’s subcortical segmentation, volumes can be obtained.
4. Percentage change and raw values can also be obtained through longitudinal pipelines.

Basic command:

``````recon-all \
-subjid \${SUBJ}_\${SES} \
-i \${DERIV}/\${SUBJ}_\${SES}_T1w.nii.gz \
-wsatlas \ # Use atlas instead of watershed threshold
-all \
-sd \${DERIV}/freesurfer``````

Include T2 or FLAIR:

``````recon-all \
-subjid \${SUBJ}_\${SES} \
-i \${DERIV}/\${SUBJ}_\${SES}_T1w.nii.gz \
-T2 \${DERIV}/\${SUBJ}_\${SES}_inplaneT2w.nii.gz \
-T2pial \
-wsatlas \
-all \
-sd \${DERIV}/freesurfer``````
``````recon-all \
-subjid \${SUBJ}_\${SES} \
-i \${DERIV}/\${SUBJ}_\${SES}_T1w.nii.gz \
-FLAIR \${DERIV}/\${SUBJ}_\${SES}_inplaneFLAIR.nii.gz \
-FLAIRpial \
-wsatlas \
-all \
-sd \${DERIV}/freesurfer``````

Analyze just one hemisphere:

``````recon-all \
-subjid \${SUBJ}_\${SES} \
-i \${DERIV}/\${SUBJ}_\${SES}_T1w.nii.gz \
-T2 \${DERIV}/\${SUBJ}_\${SES}_inplaneT2w.nii.gz \
-T2pial \
-wsatlas \
-all \
-hemi lh \ # lh or rh
-sd \${DERIV}/freesurfer``````

If your study includes participants with larger than normal ventricles (e.g., cancer, Alzheimer’s):

``````recon-all \
-subjid \${SUBJ}_\${SES} \
-i \${DERIV}/\${SUBJ}_\${SES}_T1w.nii.gz \
-T2 \${DERIV}/\${SUBJ}_\${SES}_inplaneT2w.nii.gz \
-T2pial \
-wsatlas \
-all \
-bigventricles
-sd \${DERIV}/freesurfer``````

4.2 Euler

After Leonhard Euler (1707-83), the Euler number is a topological invariant of a surface that can be computed from the number of edges, vertices and faces in a polygonal tessellation. For a completely flat and smooth surface with no handles or holes, the Euler value should be 2. For a surface that contains a lot of holes or handles, the Euler number is 2-2n, where n is the number of defects. The higher the Euler number, the higher the data quality for FreeSurfer cortical reconstruction. In other words, the less negative the number the better. Unfortunately, there’s no set number for determining quality as the number varies between populations (i.e., adolescents vs. adults), and across scanners and sequences. The Euler value can only be used within a study to determine quality control issues (Rosen et al., 2018). However, it has been proposed that the Euler value may be used towards optimization and standardization of scans across sites (Chalavi, Simmons, Dijkstra, Barker, & Reinders, 2012).

4.3 Subcortical Segmentation (aseg)

Visualize data on BYU supercomputer (specific code for Naomi): viz.rc.byu.edu

``````source ~/.bashrc_default
freesurfer-dev
cd ~/compute/data/KPCA/freesurfer_subjects/``````

Load white matter and pial segmentation plus aseg file:

``````freeview -v \
mri/T1.mgz \
mri/wm.mgz \
mri/aseg.mgz:colormap=lut:opacity=0.2 \
-f surf/lh.white:edgecolor=yellow \
surf/lh.pial:edgecolor=red \
surf/rh.white:edgecolor=yellow \
surf/rh.pial:edgecolor=red \
--viewport coronal \
--layout 1 \
--viewsize 800 800 \
--zoom 1.5``````

4.3.1 Volume in mm3

The hippocampus and amygdala volumes in this file are WRONG. Please verify the ROIs of any other subcortical region before using as these segmentations are often inaccurate. Refer to the seperate hippocampus / amygdala, brainstem, and thalamus pipelines.

``````asegstats2table \
--subjectsfile subjects.txt \
--meas volume \
--skip \
--delimiter comma \
--tablefile tablefiles/aseg-volume-stats.csv``````
• Cerebral White Matter
• Lateral Ventricle
• Inferior Lateral Ventricle
• Cerebellum White Matter
• Cerebellum Cortex
• Caudate
• Putamen
• Pallidum
• Lesions
• Accumbens area
• Vessel
• Third Ventricle
• Fourth Ventricle
• Cerebrospinal Fluid
• Corpus Callosum (Posterior, Mid Posterior, Central, Mid Anterior, Anterior)
• …and more

4.4 Cortical Parcellation (aparc)

View pial, white matter, inflated white matter, thickness, and parcellation in 3D:

``````freeview -f  surf/lh.pial:annot=aparc.annot:name=pial_aparc:visible=0 \
surf/lh.inflated:overlay=lh.thickness:overlay_threshold=0.1,3::name=inflated_thickness:visible=0 \
surf/lh.inflated:visible=0 \
surf/lh.white:visible=0 \
surf/lh.pial \
--viewport 3d``````

4.4.1 Area in mm2

``````for i in lh rh; do
aparcstats2table \
--subjectsfile subjects.txt \
--hemi \$i \
--meas area \
--skip \
--delimiter comma \
--tablefile tablefiles/aparc-area-\${i}-stats.csv
done``````

4.4.2 Volume in mm3

``````for i in lh rh; do
/Applications/freesurfer-dev/bin/aparcstats2table \
--subjectsfile subjects.txt \
--hemi \$i \
--meas volume \
--skip \
--delimiter comma \
--tablefile tablefiles/aparc-volume-\${i}-stats.csv
done``````

4.4.3 White Matter Volume in mm3

``````asegstats2table \
--stats wmparc.stats \
--subjectsfile subjects.txt \
--meas volume \
--skip \
--delimiter comma \
--tablefile tablefiles/wmparc-volume-stats.csv``````

4.4.4 Cortical Thickness in mm

``````for i in lh rh; do
aparcstats2table \
--subjectsfile subjects.txt \
--hemi \$i \
--meas thickness \
--skip \
--delimiter comma \
--tablefile tablefiles/aparc-thickness-\${i}-stats.csv
done``````

4.4.5 Mean Curvature

``````for i in lh rh; do
aparcstats2table \
--subjectsfile subjects.txt \
--hemi \$i \
--meas meancurv \
--skip \
--delimiter comma \
--tablefile tablefiles/aparc-meancurv-\${i}-stats.csv
done``````

4.4.6 Compare Atlases

``````freeview -v \
mri/orig.mgz \
mri/aparc+aseg.mgz:colormap=lut:opacity=0.4 \
mri/aparc.a2009s+aseg.mgz:colormap=lut:opacity=0.4``````

4.4.7 APARC Parcellations by Lobes

Frontal

• Superior Frontal
• Rostral and Caudal Middle Frontal
• Pars Opercularis, Pars Triangularis, and Pars Orbitalis
• Lateral and Medial Orbitofrontal
• Precentral
• Paracentral
• Frontal Pole

Parietal

• Superior Parietal
• Inferior Parietal
• Supramarginal
• Postcentral
• Precuneus

Temporal

• Superior, Middle, and Inferior Temporal
• Banks of the Superior Temporal Sulcus
• Fusiform
• Transverse Temporal
• Entorhinal
• Temporal Pole
• Parahippocampal

Occipital

• Lateral Occipital
• Lingual
• Cuneus
• Pericalcarine

Cingulate (if you want to include in a lobe)

• Rostral Anterior (Frontal)
• Caudal Anterior (Frontal)
• Posterior (Parietal)
• Isthmus (Parietal)

Other

• Insula

4.5 Hippocampus & Amygdala

After running recon-all:

``````segmentHA_T1.sh \
\${SUBJ}_\${SES} \
\${DERIV}/freesurfer/``````

To visualize the segmentations:

``````freeview -v \
mri/T1.mgz \
-v mri/lh.hippoAmygLabels-T1.v21.CA.mgz:colormap=lut:opacity=0.3 \
-v mri/rh.hippoAmygLabels-T1.v21.CA.mgz:colormap=lut:opacity=0.3 \
--viewport coronal \
--layout 1 \
--viewsize 800 800 \
--zoom 1.5``````

Output results to CSV:

``````# LEFT HIPPOCAMPUS
asegstats2table \
--subjectsfile subjects.txt \
--statsfile=hipposubfields.lh.T1.v21.stats \
--skip \
--delimiter comma \
--tablefile tablefiles/hipposubfields-lh-stats.csv

# RIGHT HIPPOCAMPUS
asegstats2table \
--subjectsfile subjects.txt \
--statsfile=hipposubfields.rh.T1.v21.stats \
--skip \
--delimiter comma \
--tablefile tablefiles/hipposubfields-rh-stats.csv

# LEFT AMYGDALA
asegstats2table \
--subjectsfile subjects.txt \
--statsfile=amygdalar-nuclei.lh.T1.v21.stats \
--skip \
--delimiter comma \
--tablefile tablefiles/amygdalarnuclei-lh-stats.csv

# RIGHT AMYGDALA
asegstats2table \
--subjectsfile subjects.txt \
--statsfile=amygdalar-nuclei.Rh.T1.v21.stats \
--skip \
--delimiter comma \
--tablefile tablefiles/amygdalarnuclei-rh-stats.csv``````

Hippocampus

If you have used results from this software for a publication, please use this citation (Iglesias, Augustinack, et al., 2015).

• Hippocampal Tail
• Hippocampal Body
• Whole Hippocampus
• Subiculum
• Presubiculum
• Parasubiclum
• CA1
• CA3
• CA4
• Molecular layer
• Fimbria
• Hippocampal fissure
• HATA

Amygdala

In addition, if you have used the segmentation of the nuclei of the amygdala, please also cite (Saygin et al., 2017).

• Whole amygdala
• Lateral nucleus
• Basal nucleus
• Accessory basal nucleus
• Anterior amygdaloid area
• Central nucleus
• Medial nucleus
• Cortical nucleus
• Corticoamygdaloid transition area
• Paralaminar nucleus

4.6 Brainstem

After running recon-all:

``````segmentBS.sh \
\${SUBJ}_\${SES} \
\${DERIV}/freesurfer/``````

To view the segmentation:

``````freeview -v \
mri/nu.mgz \
-v mri/brainstemSsLabels.v12.mgz:colormap=lut:opacity=0.3 \
--viewport coronal \
--layout 1 \
--viewsize 800 800 \
--zoom 1.5``````

Output results to CSV:

``````asegstats2table \
--subjectsfile subjects.txt \
--statsfile=brainstem.v12.stats \
--skip \
--delimiter comma \
--tablefile tablefiles/brainstem-stats.csv``````

If you have used results from this software for a publication, please use this citation (Iglesias, Van Leemput, et al., 2015).

• Whole brainstem
• Medulla
• Pons
• Midbrain
• Superior cerebellar peduncles

4.7 Thalamus

After running recon-all:

``````segmentThalamicNuclei.sh \
\${SUBJ}_\${SES} \
\${DERIV}/freesurfer/``````

Output results to CSV:

``````asegstats2table \
--subjectsfile subjects.txt \
--statsfile=thalamic-nuclei.lh.v10.T1.stats \
--skip \
--delimiter comma \
--tablefile tablefiles/thalamicnuclei-lh-stats.csv

asegstats2table \
--subjectsfile subjects.txt \
--statsfile=thalamic-nuclei.rh.v10.T1.stats \
--skip \
--delimiter comma \
--tablefile tablefiles/thalamicnuclei-rh-stats.csv``````

If you have used results from this software for a publication, please use this citation (Iglesias et al., 2018).

4.8 Longitudinal Analyses

4.8.1 Unbiased Subject Template

``````recon-all \
-base \${SUBJ} \
-tp \${SUBJ}_ses-1 \
-tp \${SUBJ}_ses-2 \
-all \
-sd \${DERIV}/freesurfer/``````

4.8.2 Longitudinal Analysis

Process session 1:

``````recon-all \
-long \
\${SUBJ}_ses-1 \
\${SUBJ} \
-all \
-sd \${DERIV}/freesurfer/``````

Process session 2:

``````recon-all \
-long \
\${SUBJ}_ses-2 \
\${SUBJ} \
-all \
-sd \${DERIV}/freesurfer/``````

As quoted from the FreeSurfer website:

Cortical reconstruction and volumetric segmentation was performed with the Freesurfer image analysis suite, which is documented and freely available for download online (http://surfer.nmr.mgh.harvard.edu/). The technical details of these procedures are described in prior publications (Dale, Fischl, & Sereno, 1999; Fischl & Dale, 2000; Fischl, Kouwe, et al., 2004; Fischl, Liu, & Dale, 2001; Fischl et al., 2002, 1999a, 1999b; Fischl, Salat, et al., 2004; Han et al., 2006; Jovicich et al., 2006; Reuter & Fischl, 2011; Reuter, Schmansky, Rosas, & Fischl, 2012; Segonne et al., 2004). Briefly, this processing includes motion correction and averaging (Reuter, Rosas, & Fischl, 2010) of multiple volumetric T1 weighted images (when more than one is available), removal of non-brain tissue using a hybrid watershed/surface deformation procedure (Segonne et al., 2004), automated Talairach transformation, segmentation of the subcortical white matter and deep gray matter volumetric structures (including hippocampus, amygdala, caudate, putamen, ventricles) (Fischl et al., 2002; Fischl, Salat, et al., 2004) intensity normalization (Sled, Zijdenbos, & Evans, 1998), tessellation of the gray matter white matter boundary, automated topology correction (Fischl et al., 2001; Segonne, Pacheco, & Fischl, 2007), and surface deformation following intensity gradients to optimally place the gray/white and gray/cerebrospinal fluid borders at the location where the greatest shift in intensity defines the transition to the other tissue class (Dale et al., 1999; Fischl & Dale, 2000). Once the cortical models are complete, a number of deformable procedures can be performed for further data processing and analysis including surface inflation (Fischl et al., 1999a), registration to a spherical atlas which is based on individual cortical folding patterns to match cortical geometry across subjects (Fischl et al., 1999b), parcellation of the cerebral cortex into units with respect to gyral and sulcal structure (Desikan et al., 2006; Fischl, Kouwe, et al., 2004), and creation of a variety of surface based data including maps of curvature and sulcal depth. This method uses both intensity and continuity information from the entire three dimensional MR volume in segmentation and deformation procedures to produce representations of cortical thickness, calculated as the closest distance from the gray/white boundary to the gray/CSF boundary at each vertex on the tessellated surface (Fischl & Dale, 2000). The maps are created using spatial intensity gradients across tissue classes and are therefore not simply reliant on absolute signal intensity. The maps produced are not restricted to the voxel resolution of the original data thus are capable of detecting submillimeter differences between groups. Procedures for the measurement of cortical thickness have been validated against histological analysis (Rosas et al., 2002) and manual measurements (Kuperberg et al., 2003; Salat et al., 2004). Freesurfer morphometric procedures have been demonstrated to show good test-retest reliability across scanner manufacturers and across field strengths (Han et al., 2006; Reuter et al., 2012).

To extract reliable volume and thickness estimates, images are automatically processed with the longitudinal stream (Reuter et al., 2012) in FreeSurfer. Specifically an unbiased within-subject template space image is created using robust, inverse consistent registration (Reuter et al., 2010). Several processing steps, such as skull stripping, Talairach transforms, atlas registration as well as spherical surface maps and parcellations are then initialized with common information from the within-subject template, significantly increasing reliability and statistical power (Reuter et al., 2012).

Please cite at least (Reuter et al., 2012) if the longitudinal stream was used!

subjid ses hemi area wm.vol
sub-001 1 lh bankssts 3458.5
sub-001 2 lh bankssts 3502.4
sub-002 1 lh bankssts 3994.3
sub-002 2 lh bankssts 3933.2
sub-003 1 lh bankssts 3414.6
sub-003 2 lh bankssts 3427.2
sub-004 1 lh bankssts 3352.9
sub-004 2 lh bankssts 3403.0
sub-005 1 lh bankssts 3511.6
sub-005 2 lh bankssts 3611.9
sub-001 1 lh caudalanteriorcingulate 2213.9
sub-001 2 lh caudalanteriorcingulate 2205.7
sub-002 1 lh caudalanteriorcingulate 2271.9
sub-002 2 lh caudalanteriorcingulate 2243.4
sub-003 1 lh caudalanteriorcingulate 2513.7
sub-003 2 lh caudalanteriorcingulate 2496.6
sub-004 1 lh caudalanteriorcingulate 2678.1
sub-004 2 lh caudalanteriorcingulate 3006.6
sub-005 1 lh caudalanteriorcingulate 3780.7
sub-005 2 lh caudalanteriorcingulate 3811.6
sub-001 1 lh fusiform 7382.8
sub-001 2 lh fusiform 7457.0
sub-002 1 lh fusiform 7536.4
sub-002 2 lh fusiform 7637.2
sub-003 1 lh fusiform 7438.7
sub-003 2 lh fusiform 7497.3
sub-004 1 lh fusiform 7941.5
sub-004 2 lh fusiform 7918.5
sub-005 1 lh fusiform 8160.0
sub-005 2 lh fusiform 8152.7

4.9 Hippocampus & Amygdala Longitudinal

After running longitudinal recon-all:

``````segmentHA_T1_long.sh \
\${SUBJ}_\${SES}_long.\${SUBJ} \
\${DERIV}/freesurfer/``````

Output results to CSV files:

``````quantifyHAsubregions.sh \
hippoSf \
T1.long \
\${DERIV}/freesurfer/tablefiles/hipposubfields-long-stats.csv \
\${DERIV}/freesurfer/

quantifyHAsubregions.sh \
amygNuc \
T1.long \
\${DERIV}/freesurfer/tablefiles/amygdalarnuclei-long-stats.csv \
\${DERIV}/freesurfer/``````

4.10 Files

4.10.1 FreeSurfer Subjects Directory

All freesurfer directories are located under the derivatives directory located in each BIDS organized dataset:

``````STUDY_DIR/
|–– derivatives/
|–– pT1T2/
|–– freesurfer/
|–– sub-001/
|–– sub-002/
|–– sub-003/
participants.tsv
dataset_description.json``````

If the dataset does not contain multiple sessions, the freesurfer directory will be organized in the following manner:

``````./freesurfer_subjects
\$ Analysis of sub-001
├── sub-001
│   ├── label
│   ├── mri
│   ├── scripts
│   ├── stats
│   ├── surf
│   ├── tmp
│   ├── touch
│   └── trash
\$ Analysis of sub-002
└── sub-002
├── label
├── mri
├── scripts
├── stats
├── surf
├── tmp
├── touch
└── trash``````

However if you have multiple timepoints and the longitudinal stream was run, then you’ll have multiple directories for each participant. First, each timepoint is processed independently, which outputs cross-sectional results. In the FreeSurfer directory the time points will be named:

``````./freesurfer_subjects
\$ Cross sectional analysis of sub-001 at timepoint 1
├── sub-001_ses-1
│   ├── label
│   ├── mri
│   ├── scripts
│   ├── stats
│   ├── surf
│   ├── tmp
│   ├── touch
│   └── trash
\$ Cross sectional analysis of sub-001 at timepoint 2
└── sub-001_ses-2
├── label
├── mri
├── scripts
├── stats
├── surf
├── tmp
├── touch
└── trash``````

Next, a within subject template is created across all time points. This is called the base directory and will just consist of the subject ID and not contain any session information. There should be only one base directory per subject. The data contained in this directory is not to be used for any analyses!

``````./freesurfer_subjects
\$ Base template for sub-001 across all timepoints
├── sub-001
│   ├── label
│   ├── mri
│   ├── scripts
│   ├── stats
│   ├── surf
│   ├── tmp
│   ├── touch
│   └── trash
\$ Cross sectional analysis of sub-001 at timepoint 1
├── sub-001_ses-1
│   ├── label
│   ├── mri
│   ├── scripts
│   ├── stats
│   ├── surf
│   ├── tmp
│   ├── touch
│   └── trash
\$ Cross sectional analysis of sub-001 at timepoint 2
└── sub-001_ses-2
├── label
├── mri
├── scripts
├── stats
├── surf
├── tmp
├── touch
└── trash``````

Finally, there will be longitudinal directories for each session.They contain the final, most reliable and accurate processing results that can be used.

``````./freesurfer_subjects
\$ Base template for sub-001 across all timepoints
├── sub-001
│   ├── label
│   ├── mri
│   ├── scripts
│   ├── stats
│   ├── surf
│   ├── tmp
│   ├── touch
│   └── trash
\$ Cross sectional analysis of sub-001 at timepoint 1
├── sub-001_ses-1
│   ├── label
│   ├── mri
│   ├── scripts
│   ├── stats
│   ├── surf
│   ├── tmp
│   ├── touch
│   └── trash
\$ Cross sectional analysis of sub-001 at timepoint 2
├── sub-001_ses-2
│   ├── label
│   ├── mri
│   ├── scripts
│   ├── stats
│   ├── surf
│   ├── tmp
│   ├── touch
│   └── trash
\$ Longitudinal analysis of sub-001 at timepoint 1
├── sub-001_ses-1.long.sub-001
│   ├── label
│   ├── mri
│   ├── scripts
│   ├── stats
│   ├── surf
│   ├── tmp
│   ├── touch
│   └── trash
\$ Longitudinal analysis of sub-001 at timepoint 2
└── sub-001_ses-2.long.sub-001
├── label
├── mri
├── scripts
├── stats
├── surf
├── tmp
├── touch
└── trash``````

4.10.2 Output Tables found on Github

List of files will include euler values:

• euler-lh.csv
• euler-rh.csv

Cortical parcellation:

• aparc-area-lh-stats.csv
• aparc-area-rh-stats.csv
• aparc-meancurv-lh-stats.csv
• aparc-meancurv-rh-stats.csv
• aparc-thickness-lh-stats.csv
• aparc-thickness-rh-stats.csv
• aparc-volume-lh-stats.csv
• aparc-volume-rh-stats.csv
• wmparc-volume-stats.csv

Subcortical volumes:

• aseg-volume-stats.csv
• amygdalarnuclei-lh-stats.csv
• amygdalarnuclei-rh-stats.csv
• hipposubfields-lh-stats.csv
• hipposubfields-rh-stats.csv
• thalamicnuclei-lh-stats.csv
• thalamicnuclei-rh-stats.csv
• brainstem-stats.csv

Longitudinal:

• aparc-area-lh-long-stats.csv
• aparc-area-rh-long-stats.csv
• aparc-meancurv-lh-long-stats.csv
• aparc-meancurv-rh-long-stats.csv
• aparc-thickness-lh-long-stats.csv
• aparc-thickness-rh-long-stats.csv
• aparc-volume-lh-long-stats.csv
• aparc-volume-rh-long-stats.csv
• aseg-volume-long-stats.csv
• amygdalarnuclei-long-stats.csv
• hipposubfields-long-stats.csv
• wmparc-volume-long-stats.csv

4.11 Generating Publication Figures

4.11.1 BrainNet Viewer (MATLAB)

Convert mgz image to NIFTI

``mri_convert mri/aparc+aseg.mgz aparc+aseg.nii.gz``

You can also extract specific ROIs:

``````c3d \
aparc+aseg.nii.gz -thresh 4 4 1 0 \
aparc+aseg.nii.gz -thresh 43 43 1 0 \
-o lateralventricles.nii.gz``````

How to load the program in MATLAB:

``````cd ('/fslhome/intj5/compute/data/KPCA/freesurfer_subjects/sub-M2PR002_ses-1/');

% Get home directory:
var = getenv('HOME');

BrainNetPath = [var, '/apps/matlab/BrainNetViewer_20191031'];

Merge your left and right surface pial to get a participant specific image: Tools > Merge Mesh

1. Load surface (pial or standard)
• Option > Layout > Lateral, medial, and dorsal view
• Option > Volume > ROI drawing > Custom (4,43)

4.11.2 R GGSEG

https://lcbc-uio.github.io/ggseg/articles/ggseg.html

To install ggseg make sure to run gcc and the latest version of R:

``````module load gcc

Installing ggseg is a bit of a chore. Install programs and load programs in this precise order. ORDER MATTERS!

``````chooseCRANmirror()
install.packages("ggplot2", dependencies = TRUE)
install.packages("remotes")
remotes::install_github("LCBC-UiO/ggseg", build_vignettes = FALSE)
remotes::install_github("LCBC-UiO/ggseg3d")
remotes::install_github("LCBC-UiO/ggsegExtra")``````
``````library(ggseg)
library(ggseg3d)
library(ggsegExtra)``````
``ggseg()``

``ggseg(position="stacked")``

``ggseg(mapping=aes(fill=area), position="stacked")``

``````ggseg(mapping=aes(fill=area),
position = "stacked") +
scale_fill_brain("dkt") +
theme_void()``````

``````ggseg(mapping=aes(fill=area), position="stacked", show.legend = F) +
ggtitle("Desikan-Killany cortical atlas")``````

``````ggseg(atlas="aseg", mapping=aes(fill=area)) +
theme_void()``````

``````ggseg(atlas="yeo7",position="stacked",mapping=aes(fill=area),) +
ggtitle("Yeo 7 resting-state networks")``````

``````someData = data.frame(
area = c("transverse temporal", "insula",
"pre central","superior parietal"),
p = sample(seq(0,.5,.001), 4),
stringsAsFactors = FALSE)

ggseg(.data=someData, mapping=aes(fill=p))``````

``````someData = data.frame(
area = rep(c("transverse temporal", "insula", "pre central","superior parietal"), 2),
p = sample(seq(0,.5,.001), 8),
AgeG = c(rep("Young",4), rep("Old",4)),
stringsAsFactors = FALSE)

someData = someData %>%
group_by(AgeG)

ggseg(.data = someData, atlas=dkt, colour="white", mapping=aes(fill=p)) +
facet_wrap(~AgeG, ncol=1) +
theme(legend.position = "bottom")``````

``````someData = data.frame(
area = c("superior frontal", "rostral middle frontal", "caudal middle frontal", "pars opercularis",
"pars triangularis", "pars orbitalis", "lateral orbitofrontal", "medial orbito frontal",
"pre central", "para central", "frontal pole", "superior parietal", "inferior parietal",
"supramarginal", "post central", "precuneus", "superior temporal", "middle temporal",
"inferior temporal", "banks superior temporal", "fusiform", "transverse temporal",
"entorhinal","temporal pole", "parahippocampal", "lateral occipital", "lingual", "cuneus",
"pericalcarine","rostral anterior cingulate", "caudal anterior cingulate",
"posterior cingulate","isthmus cingulate", "insula"),
p = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6),
stringsAsFactors = FALSE)

ggseg(position="stacked", colour="white", .data=someData, mapping=aes(fill=factor(p))) +
theme_void() +
scale_fill_manual(breaks = c("1", "2", "3", "4", "5", "6"),
labels = c("Frontal", "Parietal", "Temporal", "Occipital", "Cingulate", "Insula"),
values = c("red", "blue", "green", "purple", "yellow", "orange"),
na.value="grey") +
theme(legend.position = "bottom") +
labs(fill="")``````

4.11.3 R GGSEG 3D

https://lcbc-uio.github.io/ggseg3d/articles/ggseg3d.html

``````ggseg3d(atlas=aseg_3d) %>%