This R markdown document will show you how to generate subcortical plots illustrate your subcortical GM volume results

1. Install and load the required packages

If you have not previously installed these packages, run the following line without the #

#install.packages(c("readr","ggseg","ggplot2"))

then load the required packages

library(readr)
library(ggseg)
library(ggplot2)

2. Generating the subcortical plots

The results of the subcortical GM analyses (e.g., ridge/lasso regression beta values) will be entered in this step. The input takes the form of a vector of 32 values, corresponding to all parcellated regions in the subcortical brain. For the purpose of this demonstration, we will generate a vector of 32 random numbers ranging from -1 to 1 and use this vector as the results input

subGMresults <- runif(32, min = -1, max = 1)
subGMresults
##  [1]  0.392676488  0.631969186  0.952154434  0.234593168 -0.480899465
##  [6]  0.878557889 -0.678611606  0.952734170  0.533968803 -0.401802794
## [11] -0.737555886 -0.008558257 -0.498980472 -0.609129790  0.100373880
## [16]  0.586489427 -0.426506916  0.585755952  0.902907736  0.482173727
## [21]  0.184580499  0.668313524 -0.628560955 -0.799724258 -0.692784640
## [26]  0.015935699  0.981308204 -0.786274110 -0.967620128 -0.515033280
## [31]  0.404382171  0.527579411

Next, the subcortical plots are generated and exported as a png image (“subGMplot.png”) to your working directory

subGMlabels<-read_csv("https://blogs.ntu.edu.sg/cogbrainhealthlab/files/2021/11/subGM_labels.csv") #reading the labels required to format the results

#rearranging the results vector such that it is compatible with the ggseg input format

subGMlabels$beta<-0
subGMlabels$beta[1:15]<-subGMresults[1:15] #your results input
subGMlabels$beta[18:29]<-subGMresults[16:27] #your results input
subGMlabels$beta[33:37]<-subGMresults[28:32] #your results input

limits<-c(-max(abs(subGMlabels$beta)),max(abs(subGMlabels$beta)))

#generating the subcortical plots and exporting it as .png image
png(filename =  "subGMplot.png", width = 1100, height = 2200,  res = 300)

ggseg(mapping = aes(fill=as.numeric(beta)),atlas = aseg, position="stacked",color="black",.data=subGMlabels) +
  scale_fill_gradientn(name="Beta",colours=c("#00BFC4","white","#F8766D"),na.value = "lightgray", limits=limits, breaks=limits,labels=round(limits,2))+
  guides(fill = guide_colourbar(title.position ="left"))+
  theme(axis.text  = element_text(family="sans"),
        axis.title  = element_blank(),
        text  = element_text(family="sans",face = "bold"),
        legend.position="right",
        legend.title = element_text(hjust = 0.5, angle=90))

dev.off()

The output image should look something like this. Note that some regions (e.g., nucleus accumbens) cannot be shown in these plots due to the choice of the brain slices, which cannot be modified.