Friday, March 13, 2020

volume and surface brain plotting knitr tutorial

Here is the second of my pair of posts introducing my updated knitr brain plotting tutorials. The first tutorial describes setting up RStudio for knitr compilation, with base R graphics examples - start there. This post adds a pair of brain plotting tutorials, one for volumetric images and the other for surfaces. The source (knitrIntro_NIfTI.rnw, knitrIntro_gifti.rnw) and image files needed for compilation can be downloaded from the blog osf site, "knitr tutorials" section. The compiled pdfs are included as well, at knitrIntro_NIfTI.pdf and knitrIntro_gifti.pdf.

The surface and volume examples are roughly parallel, covering plotting both continuous statistical overlays and parcellations. The example code includes assigning values to parcels (e.g., to show the results of an analysis), adjusting the plot appearance, and doing math with the images in R. Please read both the text in the pdf and the code comments.




The brain images in the knitrs (a few of which are above) are added with a pair of functions I wrote: plot.volume() and plot.surface(). These are updated versions of the functions in previous posts on this blog, with changes to improve the appearance of the surface images and make the color scaling parameters for volumes more similar to those for surfaces.

My intention is that the plotting functions and usage examples in these knitrs will enable beginners to quickly start making useful (and attractive!) knitr documents to summarize, explore, and describe their own analyses. Please let me know if you encounter any bugs, have a suggestion for an improvement, or have a new feature suggestion.


UPDATE 13 January 2025: I did a major update of knitrIntro_gifti.rnw and replaced the gifti screen capture in this post to match. See also a new blog post introducing volume and surface datasets.

UPDATE 31 March 2023: I uploaded corrected versions of niftiPlottingFunctions.R and knitrIntro_NIfTI.pdf to osf and the volumetric screenshot above. The correction was needed because of a bug in my plot.volume() function causing the negative colors to be reversed: instead of values near zero being dark blue, they were cyan. Major thanks to Tan Nguyen for spotting the unexpected coloring! If you downloaded a copy of niftiPlottingFunctions.R before 30 March 2023, please replace it with this version.

UPDATE 29 June 2021: Changed the volumetric screenshot to show the new (diverging) cool colors (and a legend); niftiPlottingFunctions.R and giftiPlottingFunctions.R now use the same color scales.

4 comments:

  1. very nice. you might also be interested in "neurosurf"

    https://bbuchsbaum.github.io/neurosurf/articles/displaying-surfaces.html

    ReplyDelete
    Replies
    1. Interesting package, thanks for the pointer! I couldn't tell from the description; does it also work with giftis, or with a different file format?

      Delete
  2. Dear Dr. Etzel

    Thank you for sharing awesome codes and knowledge! I have one question. Do you think figures from your plot.surface code are eligible for the publication? Or is it only for the education?

    When I use your plot.surface code, I think it generates pretty accurate figures. But wondered developer's thought. And if I use your code to generate figure, how can I reference yours?

    Thanks!

    ReplyDelete
    Replies
    1. Thank you for your kind comment! I have made publication plots with my plot.surface() function, usually setting the resolution a bit higher and/or having R write directly to an image file instead of to a knitr pdf.

      For citing, the osf doi ought to be most stable, https://doi.org/10.17605/OSF.IO/W7ZKC. I would appreciate it if you include my name, and perhaps the overview blog post, too.

      My code depends on the gifti library, and he requests it be cited as Muschelli J (2020). _gifti: Reads in 'Neuroimaging' 'GIFTI' Files with Geometry Information_. R package version 0.8.0, .

      Delete