title: topoplot in ggplot2 – 2D visualisation of e.g. EEG data
body: <p><strong>Can <code>ggplot2</code> be used to produce a so-called <em>topoplot</em> (often used in neuroscience)?</strong></p>
  
  <p><a href="https://i.stack.imgur.com/WIQBC.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WIQBC.jpg" alt="topoplot"></a></p>
  
  <p>Sample data:</p>
  
  <pre><code>   label          x          y     signal
  1     R3 0.64924459 0.91228430  2.0261520
  2     R4 0.78789621 0.78234410  1.7880972
  3     R5 0.93169511 0.72980685  0.9170998
  4     R6 0.48406513 0.82383895  3.1933129
  </code></pre>
  
  <p><strong><a href="https://gist.github.com/anonymous/d8975f76f5bcde7bd455#file-topoplot_sample_data-csv" rel="nofollow noreferrer">Full sample data.</a></strong></p>
  
  <p>Rows represent individual electrodes. Columns <code>x</code> and <code>y</code> represent the projection into 2D space and the column <code>signal</code> is essentially the z-axis representing voltage measured at a given electrode.</p>
  
  <p><code>stat_contour</code> doesn't work, apparently due to unequal grid.</p>
  
  <p><code>geom_density_2d</code> only provides a density estimation of <code>x</code> and <code>y</code>.</p>
  
  <p><code>geom_raster</code> is one not fitted for this task or I must be using it incorrectly since it quickly runs out of memory.</p>
  
  <p>Smoothing (like in the image on the right) and head contours (nose, ears) aren't necessary.</p>
  
  <p>I want to avoid Matlab and transforming the data so that it fits this or that toolbox… Many thanks!</p>
  
  <h2>Update (26 January 2016)</h2>
  
  <p>The closest I've been able to get to my objective is via</p>
  
  <pre><code>library(colorRamps)
  ggplot(channels, aes(x, y, z = signal)) + stat_summary_2d() + scale_fill_gradientn(colours=matlab.like(20))
  </code></pre>
  
  <p>which produces an image like this:</p>
  
  <p><a href="https://i.stack.imgur.com/GBfX0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GBfX0.png" alt="enter image description here"></a></p>
  
  <h2>Update 2 (27 January 2016)</h2>
  
  <p>I've tried @alexforrence's approach with full data and this is the result:</p>
  
  <p><a href="https://i.stack.imgur.com/bigeA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bigeA.png" alt="@alexforrence&#39;s approach"></a></p>
  
  <p>It's a great start but there is a couple of issues:</p>
  
  <ol>
  <li>The last call (<code>ggplot()</code>) takes about 40 seconds on an Intel i7 4790K while Matlab toolboxes manage to generate these almost instantly; my ‘emergency solution’ above takes about a second.</li>
  <li>As you can see, the upper and lower border of the central part appear to be ‘sliced’ – I'm not sure what causes this but it could be the third issue.</li>
  <li><p>I'm getting these warnings:</p>
  
  <pre><code>1: Removed 170235 rows containing non-finite values (stat_contour). 
  2: Removed 170235 rows containing non-finite values (stat_contour). 
  </code></pre></li>
  </ol>
  
  <h2>Update 3 (27 January 2016)</h2>
  
  <p>Comparison between two plots produced with different <code>interp(xo, yo)</code> and <code>stat_contour(binwidth)</code> values:</p>
  
  <p><a href="https://i.stack.imgur.com/ro5ZJ.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ro5ZJ.jpg" alt="comparison between different values"></a></p>
  
  <p>Ragged edges if one chooses low <code>interp(xo, yo)</code>, in this case <code>xo</code>/<code>yo = seq(0, 1, length = 100)</code>:</p>
  
  <p><a href="https://i.stack.imgur.com/jPI9k.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jPI9k.png" alt="ragged edges"></a></p>