knitr::opts_chunk$set( collapse = TRUE, comment = "#>", out.width = "100%", fig.width = 10 )
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 alot 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))
Of course, as usual, people will have their own data they want to add to the plots, usingcolumns 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 facetting 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 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.