knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.width = 6 )
The new ggseg-package version has introduced a new way of plotting the brain atlases, through a custom geom_brain
(variant of geom_sf).
This has introduced a lot of new functionality into the package, in addition to some new custom methods and objects.
library(ggseg) library(ggplot2)
The first new thing to notice is that we have introduced a new atlas class called brain-atlas
.
This class is a special class for ggseg-atlases, that contain information in a specific way.
They are objects with 4-levels, each containing important information about the atlas in question.
dk$atlas dk$type dk$palette dk$data
Of these four, only the palette
is an optional part, where some atlases may have this field empty.
The data, you might notice, is simple-features data, with a geometry
column that includes all the information needed to plot the data as a simple features object.
You can actually call plot
directly on the data, and the standard simple features plot will appear.
plot(dk$data)
Even better, though, you should call plot
directly on the atlas object.
This will give you a fast overview of the atlas you are thinking of using.
plot(dk)
You will notice that the new atlas-class has better resolution and default values that what you get from the ggseg-atlas class.
This new class also comes with a new custom printout method, that should give you a better idea of the atlas content. It lists information such as:
And in addition it has a preview of the data content, so you may more easily discern how you might adapt your own data to fit the atlas data.
dk
Some users have also wanted to easier ways of checking the names of regions and labels of an atlas, in order to check if their data fits the atlas data. In order to make this easier, we have added two new functions that should help you with that.
brain_regions(dk) brain_labels(dk)
For other than quick overviews of the atlas using plot
this new atlas class is specifically made to work with the new geom_brain
.
Since we have better control over the geom, we have also optimised it so that when plotting just the atlas, without specifying fill
the polygons are automatically filled with the region
column.
ggplot() + geom_brain(atlas = dk)
This new geom makes it possible for you to also better control the position of the brain slices, using specialised function for this to the position argument. The position_brain
function takes a formula argument similar to that of facet_grid
to alter the positions of the slices.
ggplot() + geom_brain(atlas = dk, position = position_brain(hemi ~ side))
A new addition to the positions, is the ability to also specify the order directly through a character vector. By default, the position is:
cortical_pos <- c("left lateral", "left medial", "right medial", "right lateral") ggplot() + geom_brain(atlas = dk, position = position_brain(cortical_pos)) # Which can easily be switched around! cortical_pos <- c("right lateral", "left medial", "right medial", "left lateral") ggplot() + geom_brain(atlas = dk, position = position_brain(cortical_pos))
Many have wanted the option like in ggseg()
to only see a single hemisphere or slice. This functionality had been added through the hemi
and side
arguments to geom_brain()
, mimicking the way ggseg()
works.
ggplot() + geom_brain(atlas = dk, side = "lateral") ggplot() + geom_brain(atlas = dk, hemi = "left")
This also should work for subcortical atlases, but the hemisphere (hemi
) specification should be used carefully, as it might end up looking quite different than what you intended!
ggplot() + geom_brain(atlas = aseg, side = "coronal", hemi = "left")
Of course, as usual, people will have their own data they want to add to the plots, using columns from their own data to the plot aesthetics. By making sure at least one column in your data has the same name and overlapping content as a column in the atlas data, geom_brain will merge your data with the atlas and create your plots.
library(dplyr) someData = tibble( region = c("transverse temporal", "insula", "precentral","superior parietal"), p = sample(seq(0,.5,.001), 4) ) someData
And such plots can be further adapted with standard ggplot themes, scales etc, to your liking.
ggplot(someData) + geom_brain(atlas = dk, position = position_brain(hemi ~ side), aes(fill = p)) + scale_fill_viridis_c(option = "cividis", direction = -1) + theme_void() + labs(title = "My awesome title", subtitle = "of a brain atlas plot", caption = "I'm pretty happy about this!")
Just like in ggseg, though, you still need to do some double work for faceting to work correctly.
Because the atlas and your data need to be merged correctly, you will need to group_by
your data before giving it to ggplot, for facets to work.
someData <- tibble( region = rep(c("transverse temporal", "insula", "precentral","superior parietal"), 2), p = sample(seq(0,.5,.001), 8), groups = c(rep("g1", 4), rep("g2", 4)) ) someData
someData %>% group_by(groups) %>% ggplot() + geom_brain(atlas = dk, position = position_brain(hemi ~ side), aes(fill = p)) + facet_wrap(~groups) + ggtitle("correct facetting")
You can call plot()
on any ggseg-atlas and get a preview of the entire atlas, with labels for each region.
plot(dk)
Sometimes, though, you might still want to plot regions as categorical, but only a subset of them. To do this, we need to do a little hack. Since the ggseg plotting function copies over the entire atlas (so it can display each region), we need two columns in the incoming data. One to merge nicely with the atlas data and one to specify which regions to colour. These two columns will likely contain mirrored information, but with different names.
data <- data.frame( region = brain_regions(dk)[1:3], reg_col = brain_regions(dk)[1:3] ) data ggplot(data) + geom_brain(atlas = dk, aes(fill = reg_col)) + scale_fill_brain2(dk$palette[data$region] )
You can also plot this new atlas class directly with the ggseg
function, if you are more comfortable with that.
ggseg(someData, atlas = dk, colour = "black", size = .1, position = "stacked", mapping = aes(fill = p))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.