Plotting brainGraph object in a grid format

10 views
Skip to first unread message

Sean Ma

unread,
Apr 16, 2018, 4:35:45 PM4/16/18
to brainGraph-help
Hi Chris, 

I'd like to plot different `brainGraph` objects (the brain network plot in the cover of your User's Guide) in a grid format. 

Do you have any suggestions or scaffolding code I could use to accomplish that? I'm mostly familiar with the `ggplot` world which does not apply to your `brainGraph` objects.

Thanks!

Sean

Chris Watson

unread,
Apr 17, 2018, 1:36:27 PM4/17/18
to brainGr...@googlegroups.com
Hi Sean,

Plotting over an image (a brain slice, in this case) can make this a frustrating task. What is probably easiest is to work from the code in "plot_brainGraph_multi". Below is code for plotting in a 2x3 layout over an axial slice.

X <- mni152@.Data

# This is essentially what "oro.nifti::image.nifti" does
zlim <- c(3500, max(X[, , 46]))        # Change this if you want a different slice
imcol <- gray(0:64/64)
breaks <- c(zlim[1], seq(min(zlim), max(zlim), length=64), zlim[2])

layout(rbind(1:3, 4:6))

for (i in 1:6) {
  par(mar=c(0, 0, 0, 0), bg='black')
  graphics::image(1;91, 1:109, X[, , 46], col=imcol, breaks=breaks)        # Change slice number if you did above
  par(new=TRUE)

  plot(g[[i]], plane='axial', mni=FALSE, ...)    # The "mni=FALSE" is the key here
}


You can do pretty much any layout, I think, but then you might have to play around with the "widths" and "heights" arguments; otherwise, some of the plots might look "squished".

If you want to plot L or R sagittal, then the MNI152 data is:

X <- mni152@.Data
L <- nifti(X[rev(seq_len(nrow(X))), rev(seq_len(ncol(X))), ])
R <- aperm(X, c(2, 3, 1))
L <- aperm(L, c(2, 3, 1))

The differences for L and R are because I wanted the anterior part of the brain to be facing left and right, respectively.

The reason I don't use "ggplot2" for this kind of thing is because of plotting the brain slice underneath; I never found a solid way to do it, and it seemed like it wasn't worth the effort to me. I believe "ggplot2" has functionality for plotting over maps and things like that, so there is probably a similar way to do it for an image. But I've been satisfied with the solutions I came up with. You may want to check out the "ggraph" package; there is also "geomnet" and "ggnet", but I liked "ggraph" the most out of those 3.

Chris

--
You received this message because you are subscribed to the Google Groups "brainGraph-help" group.
To unsubscribe from this group and stop receiving emails from it, send an email to brainGraph-help+unsubscribe@googlegroups.com.
To post to this group, send email to brainGraph-help@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/brainGraph-help/afec8c17-fe7c-4d38-9423-831322d21ba8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sean Ma

unread,
Apr 20, 2018, 4:14:51 PM4/20/18
to brainGraph-help
Thank you, Chris for the code! It worked pretty well. 

Since I would like to show how my weights are changing over time, I've added

edge.width = 'weight'

into your plot code

plot(g[[i]], plane='axial', mni=FALSE, ...)  

However, the edge width is still very thin compared to what Fig. 12.4 in your User's Guide. Is there a way to enhance the edge weights? I've tried 

edge.width = 'weight' * 10

But that gives me an error. Any suggestion is helpful!

Thank you and happy Friday!

Sean
To unsubscribe from this group and stop receiving emails from it, send an email to brainGraph-he...@googlegroups.com.
To post to this group, send email to brainGr...@googlegroups.com.

Chris Watson

unread,
Apr 20, 2018, 5:30:17 PM4/20/18
to brainGr...@googlegroups.com
Hi Sean,

Yes the "edge.width" argument accepts only either a numeric or a character; if the latter, it must be a valid edge attribute. To scale it, you will have to do so outside of the function. You can either assign a new value to the same attribute (i.e., E(g)$weight <- E(g)$weight * 10) or just create a new one (say, "weight2") and use that for the "edge.width" argument.

Depending on the distribution of edge weights, you may want to consider a few different scenarios:

1. You can simply multiply by some integer, as you first attempted.

2. You can use the "vec.transform" function in brainGraph which just transforms the given vector by the following equation: http://quicklatex.com/cache3/db/ql_6f19101f4da6ed93a8f3d31220573cdb_l3.png
So for example, you can "stretch" the vector to have a new min of 1 and new max of 5, or whatever you choose. You can access the function with the "triple colon" operator, i.e.,
brainGraph:::vec.transform(E(g)$weight, min.val=1, max.val=5)

3. If the range is more than 1 or 2 orders of magnitude apart, then you will probably want to take the logarithm of the weights; i.e.,
E(g)$weight2 <- log10(E(g)$weight)

I have noticed that edge widths (as far as igraph plotting is concerned) equal to 5 is very wide, so you might want to consider this as the absolute maximum.

Let me know if you have other questions.
Chris

To unsubscribe from this group and stop receiving emails from it, send an email to brainGraph-help+unsubscribe@googlegroups.com.
To post to this group, send email to brainGraph-help@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/brainGraph-help/6ba93282-0876-4cce-8071-421b9686d5db%40googlegroups.com.

Sean Ma

unread,
Jun 13, 2018, 1:46:20 PM6/13/18
to brainGraph-help
Hi Chris, 

Just a trivial follow up on this thread. What if I only wanted to plot a sub-sample of the vertices for the `brainGraph` object `g[[i]]`?

Say my vertices of interests have labels '1' and '2', I've tried extending your code below by adding `regions = c('1','2') for the plot yet it still all of the vertices instead.

plot(g[[i]], plane='axial', regions = c('1','2'))    # The "mni=FALSE" is the key here


Any suggestions?

Sean

Chris Watson

unread,
Jun 13, 2018, 2:10:33 PM6/13/18
to brainGr...@googlegroups.com
Try this:

plot(g[[i]], subgraph='regions %in% c('1', '2'))

This assumes that you have a vertex attribute called "regions"

To unsubscribe from this group and stop receiving emails from it, send an email to brainGraph-help+unsubscribe@googlegroups.com.
To post to this group, send email to brainGraph-help@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/brainGraph-help/4bdc5a99-54df-4ab5-8ff4-986fa7133c7c%40googlegroups.com.

Sean Ma

unread,
Jun 13, 2018, 4:31:25 PM6/13/18
to brainGraph-help
Hi Chris, 

Thanks for your prompt reply. 

May I ask how the `atlas`$name variable is stored in your `brainGraph` `g[[i]]` list from the `set_brainGraph_attr()` function? The reason I asked is there is no `region` variable in the `g[[i]]` object and therefore the `subgraph` approach didn't work. It seems the `region` variable seen in the `dt.V` dataframe was generated after applying the `vertex_attr_dt` in `data_tables.R`.

Is it possible that I tackle with the `g[[i]]` index itself? 

I've attached my `g[[i]]` object (with 4 subjects and 4 threshold) if that would help. 

Thank you again!

Sean

Sean Ma

unread,
Jun 13, 2018, 4:52:10 PM6/13/18
to brainGraph-help
Hi Chris, 

On similar note with the g[[i]] I've provided through Dropbox link, I've tried plotting the edge weights in 'sagittal' plane by changing the `plane` option but had an error occurred (the default 'axial' plane was ok).

plot(g.norm[[1]][[1]][[1]], edge.width = 'weight', plane = 'sagittal')

Error in plot.brainGraph(g.norm[[1]][[1]][[1]], edge.width = "weight", : object 'X' not found

Is this X the `mni152@.Data` you've loaded?  I've tried loading it but it came with an error loading message:

Error: object 'mni152' not found 

Thanks again for your help!

Sean 

Chris Watson

unread,
Jun 13, 2018, 4:54:44 PM6/13/18
to brainGr...@googlegroups.com
The "name" attribute it assigned (via "make_brainGraph") to match that of "atlas$name". So if this is the variable you want to subset based on, it would be

​​plot(g[[i]], subgraph='name %in% c('1', '2')')

​You might need to switch one of the (sets of) apostrophes to quotation marks​.

To unsubscribe from this group and stop receiving emails from it, send an email to brainGraph-help+unsubscribe@googlegroups.com.
To post to this group, send email to brainGraph-help@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/brainGraph-help/f61bfadb-34a8-4fb1-aec0-7b64ef69e6ba%40googlegroups.com.

Chris Watson

unread,
Jun 13, 2018, 5:06:17 PM6/13/18
to brainGr...@googlegroups.com
Hi Sean,
I actually have discovered 2 bugs here.

1. When specifying a different plane, you must explicitly specify the "hemi" argument. The default (which is "both") gets selected and leads to the bug. So specify either hemi='L' or hemi='R' (until the next update)

2. Since your "subgraph" argument has parentheses in it, some of the text processing code will throw an error. I will fix this for the next update, as well, but for now you can store the regions in a separate variable, something like:

myRegions <- as.character(c(1, 2))
plot(g[[i]], subgraph='name %in% myRegions')

Try these solutions and let me know if they work.
Chris

Sean Ma

unread,
Jun 13, 2018, 5:23:24 PM6/13/18
to brainGraph-help
Will let you know if works! Thanks so much again! 

Sean
Reply all
Reply to author
Forward
0 new messages