Re: ERP Top View Plot using ggplot2

586 views
Skip to first unread message

Brandon Hurr

unread,
Jan 29, 2013, 12:02:17 PM1/29/13
to Sebastian Geukes, ggplot2
It's been a while since I've seen it, but Hadley was working on plotting tools for "big data" and in it there was the capability to have plots that represent points within a larger overall plot that sounds very similar to what you want. I don't know if it actually made it into a real package or not.

Brandon


On Tue, Jan 29, 2013 at 2:01 PM, Sebastian Geukes <sebasti...@googlemail.com> wrote:
Hi all,

We have imported EEG data into R to plot event-related potentials using ggplot2. We have obtained beautiful graphs beating out all further attempts using other software. Using facet_wrap() we also manage to plot the ERPs from all of our 32 electrodes at once (see attachment for a rough example). 

Now our question is whether there is a neat way in ggplot2 or R base graphics how we could arrange the individual electrode subgraphs such that they approximately reflect the topographical electrode layout on the head? (Just like in the typical ERP top view plots you would get from your dedicated EEG software). Sorting the subgraphs for usage with facet_wrap() obviously won't do the job here. 

Is there a way to make up a "plotting canvas" and position the subgraphs to specific coordinates of that canvas? Any other suggestions?

Thank you for any hints!

Sebastian Geukes
University of Münster, Germany

--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2
 
---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Dennis Murphy

unread,
Jan 29, 2013, 3:46:50 PM1/29/13
to Brandon Hurr, Sebastian Geukes, ggplot2
Hi:

On Tue, Jan 29, 2013 at 9:02 AM, Brandon Hurr <brando...@gmail.com> wrote:
> It's been a while since I've seen it, but Hadley was working on plotting
> tools for "big data" and in it there was the capability to have plots that
> represent points within a larger overall plot that sounds very similar to
> what you want. I don't know if it actually made it into a real package or
> not.

I think you're referring to Garrett Grolemund's ggsubplot package,
Brandon, but I don't think that's quite what SG is after since the
subplots are applied to contiguous subregions IIRC...although the idea
is on the right track. If I interpreted the question correctly, it is
how to deconstruct a facet_wrap()ped plot into its constituent panels
(grobs) and reposition them in a graphics panel.

This sounds like a task for the gtable package with a (possibly heavy)
dose of grid graphics. A post by shashy in this group
(https://groups.google.com/forum/?fromgroups#!forum/ggplot2) yesterday
provided an example of how gtable can be used with ggplot2 graphics to
reposition grobs; a search of the list archives (as well as
StackOverflow) should elicit several others. The gtable approach would
be major surgery in this example: I'm wondering if it might be a bit
easier (although tedious) to make and ggsave the individual graphs to
separate files in a raster format and then use something like
annotation_raster() or annotation_custom() to position them in a
graphics panel (which is also major surgery, but of a different kind
:)

Suggestions welcome...

Dennis

Greg Snow

unread,
Jan 29, 2013, 5:00:00 PM1/29/13
to Sebastian Geukes, ggplot2
For the "or R base graphics" option there is the subplot function in the TeachingDemos package.  This would not do the automatic faceting, but you could do each individual plot where you do the subsetting (and specify the axes) yourself.  One of the examples on the subplot help page shows plotting a graph on each state on a map, you could modify that code for your locations.

Using grid graphics you can do similar by creating the overall plot, then creating new viewports where you want the new graph to go, then use ggplot to put the graph there.  You will again need to do the conditioning by hand and specify the limits rather than the nice automatic faceting.

Either approach can easily be automated in a loop or function.


On Tue, Jan 29, 2013 at 7:01 AM, Sebastian Geukes <sebasti...@googlemail.com> wrote:
Hi all,

We have imported EEG data into R to plot event-related potentials using ggplot2. We have obtained beautiful graphs beating out all further attempts using other software. Using facet_wrap() we also manage to plot the ERPs from all of our 32 electrodes at once (see attachment for a rough example). 

Now our question is whether there is a neat way in ggplot2 or R base graphics how we could arrange the individual electrode subgraphs such that they approximately reflect the topographical electrode layout on the head? (Just like in the typical ERP top view plots you would get from your dedicated EEG software). Sorting the subgraphs for usage with facet_wrap() obviously won't do the job here. 

Is there a way to make up a "plotting canvas" and position the subgraphs to specific coordinates of that canvas? Any other suggestions?

Thank you for any hints!

Sebastian Geukes
University of Münster, Germany

--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2
 
---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Gregory (Greg) L. Snow Ph.D.
538...@gmail.com

Mike Lawrence

unread,
Jan 30, 2013, 9:05:12 PM1/30/13
to ggp...@googlegroups.com
One approach you could try in the meantime is to use a single facet but shift each electrode's data to the locations centered on the electrode placement, then maybe use annotations to create new axes for each electrode. 

Kohske Takahashi

unread,
Jan 30, 2013, 11:57:47 PM1/30/13
to Mike Lawrence, ggp...@googlegroups.com
Hi,

Here is an example using grid viewport funcitons:

library(plyr)
library(ggplot2)
library(grid)

coords <- data.frame(x = c(1, 2, 3)/4, y = c(1, 2, 3)/4)
size <- 0.3

titles <- c("A", "B", "C")
plots <- llply(titles, function(x) ggplotGrob(qplot(rnorm(10),
rnorm(10), main=x)))

grid.newpage()
for (i in 1:3) {
pushViewport(vp = viewport(coords$x[i], coords$y[i], size, size))
grid.draw(plots[[i]])
popViewport()
}


2013/1/31 Mike Lawrence <mike....@gmail.com>:
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>



--
Kohske Takahashi <takahash...@gmail.com>

Assistant Professor,
Research Center for Advanced Science and Technology,
The University of Tokyo, Japan.
http://www.fennel.rcast.u-tokyo.ac.jp/profilee_ktakahashi.html
Rplot01.png

Herbert Jägle

unread,
Feb 3, 2013, 8:03:17 AM2/3/13
to ggp...@googlegroups.com
Using viewports in principle works. I had a similar task plotting multifocal VEP traces into a pre-defined arrangement (each position represents a visual field location rather than an electrode position like in your application). I tried to use viewports and removed the axis and labels from each subplot, but this was extremely slow due to the high number of subplots and datapoints in each trace.

My solution was to define a pattern of centers for each subplot using polar coordinates (the arrangement is organized concentric, so this makes scaling easy). For plotting i read the coordinates, scale them if required and calculate kartesian offset values for each subplot and just add them to the data. I use the same coordinates to create bubble plots (e.g. to illustrate significance of differences between recordings).

Best wishes,
Herbert

Bastien Boutonnet

unread,
Jun 25, 2013, 8:21:06 PM6/25/13
to ggp...@googlegroups.com
Hi,

This question is going to sound maybe opportunistic, but I am fairly new to R and i would like to know how to plot a single ERP (to start with) using R. I can make basic plots, but if you could show me a script of how time-based signal data can be plotted in R I would be extremely grateful.

My main problem is that I don't know how to make the different conditions appear on the same graph. My idea would be to get data for x(time) in one column of a table and y(amplitudes) on the other, but how can I specify in R another column for y that represents another condition.

Then there is formatting, how to make the graph look more or less like traditional ERP graphs. If you have any script or guidance to offer I would be extremely happy. Thanks B

Ben Bond-Lamberty

unread,
Jun 26, 2013, 8:06:14 AM6/26/13
to Bastien Boutonnet, ggplot2
I have no idea what an ERP is, but generally, the basic idea in ggplot
is to put all your data into a single (usually) data frame, including
x, y, and any grouping variables. For example, using sample data

d <- data.frame(time=c(1,2,3,1,2,3),condition=c(1,1,1,2,2,2),y=c(1,2,3,4,5,6))

you can do something like

qplot(time,y,color=condition,data=d,geom="line")

to plot the two conditions in different colors.

Ben

Sebastian Geukes

unread,
Aug 28, 2013, 5:51:06 AM8/28/13
to ggp...@googlegroups.com
Hello everybody,

as to the question about ERP Top View plots: We have finally tried out Koshke's proposal of using grid viewports and we do indeed get a decent result, albeit by loosing some of the ggplot-flexibilites. We load the XY-electrode-coordinates provided by our cap manufacturer, then we use the grid viewport functions to show one plot per electrode, at its original position on the 2D-head. Finally we add one more plot for axis labeling. Result in attachment. (the data are a bit too complex to produce a quick reproducible result, sorry)

# read electrode positions

EPos <- read.table("2D-Positions.txt")

 
# define plot function

plots <- llply(titles, function(x) ggplotGrob(ggplot([...])))



# run plotting loop (plots contain no axis labels for visual clarity)
grid.newpage() 
for (i in 1:length(Electrodes)) { 
 pushViewport(vp = viewport(EPos$X[i], EPos$Y[i], plotsize, plotsize)) 
  grid.draw(plots[[i]]) 
  popViewport() 
 
# add one axis labeling plot
pushViewport(vp = viewport(0.85, 0.12plotsizeplotsize)) 
  grid.draw(ggplotGrob(ggplot([...])))
popViewport() 

What we are missing so far is a neat way too add the legend for the line labels. Does anybody have a good idea for that?

In the end, we wonder whether it were helpful to have a facet_wrap_XY variant that would accept free positioning of the facets (by reading XY coordinates). What do you think?

Thanks for all your suggestions,
Sebastian
ERP Top Plot Grid Viewports.jpg

Akshay Maggu

unread,
May 3, 2015, 8:24:53 AM5/3/15
to ggp...@googlegroups.com
Hi Sebastian, 

Could you please share your code. I have a similar bunch of data to plot. 

Thanks, 
Akshay
Reply all
Reply to author
Forward
0 new messages