Location on server: /space/chen-syn01/1/data/cinliu/plots/UKB_cortex_result_brainmaps
(code is with in folder with images)
Location on server: /space/chen-syn01/1/data/cinliu/plots/UKB_cortex_result_brainmaps
(code is with in folder with images)
This file looks something like this:
This data set has 18 different "inv" (in the picture above we can only see "1p22.1")
We want to plot the "p_global" for each "inv", thus we should end up with 18 graphs in the end.
Change the name of the regions on the data frame to match the region name formate of ggseg.
You can run the code ggsegChen::chenTh or ggsegChen::chenAr to see the proper spelling of the region names ggsegChen uses.
Note: Make sure to check there are no accidental spaces before or after of the region names, ggsegchen is not that forgiving if there is like an accidental space after a region name it will not recognize it and fail to geCalculate the negative log value (so we can later plot using -log10(p)
Get list of inversion names. There are 18 names in this case
To make a ggsegChen brain plot it is actually very simple. The package only requires 2 things to be able to build a graph.
Specify if you are using brain thickness or brain area
Give it data that has 2 column of information: region name and value assigned to the region.
However in this particular case we want to generate a brain plot for each "inv" region in this case, so we need to build a loop through the list of "inv_names".
In this dataset it is every 26 rows there is a new name for "inv" so we will set 1 and 26 as our starting number a <- 1 b <- 26
make loop for (i in 1:length(inv_names))
make a separate data frame that only has the first 26 rows dfs <- df[a:b,] dfs is the new dataframe we will be working with from now on
pull out the "inv" name for later use inv <- dfs$inv[1]
Making the area brainplot
Pull out the area data area <- dfs[1:12,]
Make the plot
plot1 <- area%>%
ggseg(atlas = "chenAr",mapping = aes(fill = as.numeric(neg_log_p_global)))+
labs(title =paste0(inv,"_area.png"), fill="-log10(p_global)")+
scale_fill_gradientn(colours = c("deepskyblue2","royalblue4"),na.value = "transparent")
ggsave(paste0(inv,"_area.png"), width = 10, height = 3)
Code break down
plot1 <- area%>% chains the data to the 4 lines of code that follows it
ggseg()
Specify we want to use the Chen Area map atlas = "chenAr"
Specify the value we want to plot mapping = aes(fill = as.numeric(neg_log_p_global)))+
Labels labs(title =paste0(inv,"_area.png"), fill="-log10(p_global)")+
the title of the plot title paste0(inv,"_area.png")
the title of the color key fill="-log10(p_global)
Specify the color scale scale_fill_gradientn(colours = c("deepskyblue2","royalblue4"),na.value = "transparent")
Save the plot using ggsave, saving it as 10x 3 in this case ggsave(paste0(inv,"_area.png"), width = 10, height = 3)
Repeat similar setps but this time do it for thickness (which is the next 26 line of data in dfs)
thickness <- dfs[13:24,]
thickness%>%
ggseg(atlas = "chenTh",mapping = aes(fill = as.numeric(neg_log_p_global)))+
labs(title = paste0(inv,"_thickness.png"),fill="-log10(p_global)")+
scale_fill_gradientn(colours = c("deepskyblue2","royalblue4"),na.value = "transparent")
ggsave(paste0(inv,"_thickness.png"), width = 10, height = 3)
In the end add 26 to "a" and "b" to move to the new net of "inv" data. Close the loop in the end.
a <- a+26
b <- b+26
}
Sample result plot:
Full code brainplot_p_global.R
Very useful papaer that provides instruction on how to change brain positioning and other things in ggseg:
Mowinckel, A. M., & Vidal-Piñeiro, D. (2020). Visualization of Brain Statistics With R Packages ggseg and ggseg3d. Advances in Methods and Practices in Psychological Science, 466–483. https://doi.org/10.1177/2515245920928009
Direct link to site: https://journals.sagepub.com/doi/full/10.1177/2515245920928009
Direct link to pdf: https://journals.sagepub.com/doi/pdf/10.1177/2515245920928009
Here are just some sample of different positions:
Position "stacked"
area <- dfs[1:12,]
plot1 <- area%>%
ggseg(atlas = "chenAr",mapping = aes(fill = as.numeric(neg_log_p_global)),position = "stacked")+
labs(title =paste0(inv,"_area.png"), fill="-log10(p_global)")+
scale_fill_gradientn(colours = c("deepskyblue2","royalblue4"),na.value = "transparent")
Position "stacked", theme dark
plot1 <- area%>%
ggseg(atlas = "chenAr",mapping = aes(fill = as.numeric(neg_log_p_global)),position = "stacked")+
labs(title =paste0(inv,"_area.png"), fill="-log10(p_global)")+
scale_fill_gradientn(colours = c("deepskyblue2","royalblue4"),na.value = "transparent")+
theme_dark()
Position "stacked", Left hemisphere (can do the same for right)
plot1 <- area%>%
ggseg(atlas = "chenAr",mapping = aes(fill = as.numeric(neg_log_p_global)),position = "stacked", hemisphere = "left")+
labs(title =paste0(inv,"_area.png"), fill="-log10(p_global)")+
scale_fill_gradientn(colours = c("deepskyblue2","royalblue4"),na.value = "transparent")
Position "stacked", medial (can do the same for lateral view)
plot1 <- area%>%
ggseg(atlas = "chenAr",mapping = aes(fill = as.numeric(neg_log_p_global)),position = "stacked", view = "medial")+
labs(title =paste0(inv,"_area.png"), fill="-log10(p_global)")+
scale_fill_gradientn(colours = c("deepskyblue2","royalblue4"),na.value = "transparent")