New {ggseg} with Geom!

New {ggseg} with Geom!

Table of Content

A new version of ggseg R-package for visualisations of brain atlases was released a couple of months ago . I wanted to create such an update for a long time, but getting the internal logics of a ggplot geom working as I wanted was rather difficult . New {ggseg} with Geom! The position is what I’m truly happy about!

A couple of months ago, we released a new version of our ggseg R-package for visualisations of brain atlases. I had been wanting to create such an update for a long time, but getting the internal logics of a ggplot geom working as I wanted was rather difficult. Likely because I was trying to do too many things, but, hey… that’s me!

One of the main things I wanted to get rid of, was this need to first group your data before sending it to ggplot if you want to facet. This rather unintuitive thing was really messing me and my users up. I had a hope that making a geom would be better. Also, a geom could be more familiar to ggplot users, and also likely harness the power that is ggplot better.

After many a long hours trying to figur this out, and some informal chats with Thomas Lin Pedersen, I realised that my wish might not come true. I’m still hoping and toying with an idea, but unsure if it will ever come to fruition as I’d like.

But I did get a geom working, and even a position! The geom it self I’m quite happy with, despite it still missing some features I’m rather keen on. But the position is what I’m truly happy about! Before, it was rather cumbersome to re-position the brain slices as you’d like to.

Let’s have a look at it.

library(ggplot2)
library(ggseg)

ggseg(atlas = dk, 
      mapping = aes(fill = region),
      show.legend = FALSE, 
      color = "black",
      position = "dispersed")

“dispersed” is the default position of any brain atlas we have for ggseg. Where the brain slices are ordered neatly in a single row.

This can be changes to a grid by using the “stacked” position.

ggseg(atlas = dk, 
      mapping = aes(fill = region),
      show.legend = FALSE, 
      color = "black",
      position = "stacked")

Any other re-ordering of slice positions just don’t work, because the internal logic of doing that is just not there. And it’s too much to get it in there.

But in the geom, we have more flexibility because of the position!

When using the geom, the syntax is quite different from ggseg() but more close to other ggplots.

ggplot() +
  geom_brain(atlas = dk, show.legend = FALSE)

We can see here that there are several things that have changed. For instance, I did not specify the fill colour by regions, but it happened automatically! This is a thing I got for free basically, because geom_brain is built on geom_sf which also does this. This is kind of what most users would expect as default behaviour, when just plotting the atlas.

The colour of the edges around the regions is also more delicate and the entire plot actually looks more high-def and sleek. Again, we’re getting the for free from the sf geom.

The downside to the geom is that I’ve now lost the ability to automatically alter the axis and grid lines based on the hemisphere and side, so the axes now dont contain any informative information. I’ve again had a chat with both Thomas Lin Pedersen and Claus Wilke about what I was after and it was not within scope. I’ll likely add som convenience functions to help users along for this, but its just not there right now, and won’t ever be in the automatic way as in ggseg().

Now, let’s get to the positioning magic that I made to alter the brain slice positions. Currently, this only works for the cortical atlases, I’m working on something even more versatile, but its still not ready.

So, the new ggseg version now also comes with a position_brain() function, that is meant to be called from within geom_brain(). It takes a formula, that should be familiar to ggplot users used to working with facets, in particular facet_grid().

ggplot() +
  geom_brain(
    atlas = dk, 
    show.legend = FALSE,
    position = position_brain(hemi ~ side)
  )

See what’s happened? By using the formula, we could now position the slices so that the hemispheres are in each row, and the lateral/medial side are in the columns. We could easily switch that up, though, so that the sides are in rows and hemisphere in columns

ggplot() +
  geom_brain(
    atlas = dk, 
    show.legend = FALSE,
    position = position_brain(side ~ hemi)
  )

We could even alter their order in the default single row if we wanted.

ggplot() +
  geom_brain(
    atlas = dk, 
    show.legend = FALSE,
    position = position_brain(. ~ side  + hemi)
  )

or get them stacked in one single column.

ggplot() +
  geom_brain(
    atlas = dk, 
    show.legend = FALSE,
    position = position_brain(side  + hemi ~ .)
  )

As you can see, the flexibility here is just so much more, so people can get their plots into the shape they are after. I’m pretty happy about that! There are still some limitations to the position altering, but all-in-all there is a lot more you can do now than before.

So do please explore the newer ggseg-package, I believe it’s truly a step up from what we had before. It is also now filled with custom S3 classes for the atlases, meaning you’ll have better print, plot and other methods related to the atlases. These should together give everyone a better experience with the package and all the atlases that are compatible with this plotting system we have created.

plot(dk, 
     show.legend = FALSE,
     position = position_brain(hemi ~ side)
     ) +
  theme_brain2()
  • Apr 30, 2020

    Using Freesurfer annotation files to plot in R

    Using Freesurfer, the Desikan Killany coortical atlas (aparc) is an atlas that consist of 36 segments, which are well-defined Automatically Parcellating the Human Cerebral Cortex Using Freisurfer annotation files to plot in R is our package where you can render the proper 3d version using ggplot2 .

    Read
    r
    neuroimaging
    plots
  • Dec 06, 2019

    ggseg3d - creating interactive brain segmentation plots with plotly

    ggseg3d is a package for 3d mesh plots of brain atlases using plotly . It is made to plot brain atlas data . It should be fairly easy to use or atlele to make the first plot . It can be installed from our works GitHub account using remotes .

    Read
    packages
    neuroimaging
    plots
    r
  • Oct 01, 2018

    Get the brain animated!

    The ggseg package was so well received that we almost immediately got it on [Neuroconductor]. We got some pull requests from people helping us improve the code, and particularly get it ready for CRAN when we decide to submit it there . The package vignettes are update on how you may supply data for facetting .

    Read
    packages
    neuroimaging
    r
  • Aug 30, 2018

    Introducing the ggseg R-package for brain segmentations

    The ggseg R-package for brain segmentations is intended for those who need to easily plot results or overviews of segmentet data . The package is developed as part of our lab [The Center for Lifespanism and Cognition [LCBC-UiO]

    Read
    plots
    packages
    neuroimaging
    r
  • May 25, 2018

    Circular plots in R and adding images

    Circular plots are becoming more and more popular in for visualising associatetions between different phenomena . I think they look pretty cool, and while I have not yet published a paper with it, I did prep a lot of scripts for plotting my analysis results with it .

    Read
    plots
    neuroimaging
    r