8

ggplot2用于产生所谓的拓扑图(常用于神经科学)?

拓扑图

样本数据:

   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

完整的样本数据。

行代表单个电极。列xy代表在二维空间中的投影,并且列signal本质上是代表在给定电极处测量的电压的 z 轴。

stat_contour不起作用,显然是由于网格不相等。

geom_density_2dx仅提供和的密度估计y

geom_raster不适合这项任务,或者我必须不正确地使用它,因为它很快就会耗尽内存。

不需要平滑(如右图)和头部轮廓(鼻子、耳朵)。

我想避免使用 Matlab 并转换数据以使其适合这个或那个工具箱……非常感谢!

更新(2016 年 1 月 26 日)

我最接近我的目标的是通过

library(colorRamps)
ggplot(channels, aes(x, y, z = signal)) + stat_summary_2d() + scale_fill_gradientn(colours=matlab.like(20))

这会产生这样的图像:

在此处输入图像描述

更新 2(2016 年 1 月 27 日)

我用完整的数据尝试了@alexforrence 的方法,结果如下:

@alexforrence 的方法

这是一个很好的开始,但有几个问题:

  1. 最后一次调用 ( ggplot()) 在 Intel i7 4790K 上大约需要 40 秒,而 Matlab 工具箱几乎可以立即生成这些;我上面的“紧急解决方案”大约需要一秒钟。
  2. 正如你所看到的,中心部分的上下边界似乎被“切片”了——我不确定是什么原因造成的,但这可能是第三个问题。
  3. 我收到以下警告:

    1: Removed 170235 rows containing non-finite values (stat_contour). 
    2: Removed 170235 rows containing non-finite values (stat_contour). 
    

更新 3(2016 年 1 月 27 日)

interp(xo, yo)用不同的和stat_contour(binwidth)值生成的两个图之间的比较:

不同值之间的比较

如果选择 low interp(xo, yo),则边缘参差不齐,在本例中为xo/ yo = seq(0, 1, length = 100)

参差不齐的边缘

4

1 回答 1

8

这是一个潜在的开始:

首先,我们将附加一些包。我正在使用akima进行线性插值,虽然看起来 EEGLAB在这里使用了某种球面插值?(尝试它的数据有点稀疏)。

library(ggplot2)
library(akima)
library(reshape2)

接下来,读入数据:

dat <- read.table(text = "   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")

我们将插入数据,并将其粘贴在数据框中。

datmat <- interp(dat$x, dat$y, dat$signal, 
                 xo = seq(0, 1, length = 1000),
                 yo = seq(0, 1, length = 1000))
datmat2 <- melt(datmat$z)
names(datmat2) <- c('x', 'y', 'value')
datmat2[,1:2] <- datmat2[,1:2]/1000 # scale it back

我将借用一些以前的答案。以下circleFun来自用 ggplot2 画一个圆圈

circleFun <- function(center = c(0,0),diameter = 1, npoints = 100){
  r = diameter / 2
  tt <- seq(0,2*pi,length.out = npoints)
  xx <- center[1] + r * cos(tt)
  yy <- center[2] + r * sin(tt)
  return(data.frame(x = xx, y = yy))
}

circledat <- circleFun(c(.5, .5), 1, npoints = 100) # center on [.5, .5]

# ignore anything outside the circle
datmat2$incircle <- (datmat2$x - .5)^2 + (datmat2$y - .5)^2 < .5^2 # mark
datmat2 <- datmat2[datmat2$incircle,]

而且我真的很喜欢 ggpplot2 中R plotfilled.contour() 输出中的等高线图的外观,所以我们将借用那个。

ggplot(datmat2, aes(x, y, z = value)) +
  geom_tile(aes(fill = value)) +
  stat_contour(aes(fill = ..level..), geom = 'polygon', binwidth = 0.01) +
  geom_contour(colour = 'white', alpha = 0.5) +
  scale_fill_distiller(palette = "Spectral", na.value = NA) + 
  geom_path(data = circledat, aes(x, y, z = NULL)) +
  # draw the nose (haven't drawn ears yet)
  geom_line(data = data.frame(x = c(0.45, 0.5, .55), y = c(1, 1.05, 1)), 
            aes(x, y, z = NULL)) +
  # add points for the electrodes
  geom_point(data = dat, aes(x, y, z = NULL, fill = NULL), 
             shape = 21, colour = 'black', fill = 'white', size = 2) +
  theme_bw()

在此处输入图像描述


通过评论中提到的改进(分别设置extrap = TRUE和调用以填充间隙和进行样条平滑,并在绘图前删除 NA),我们得到:linear = FALSEinterp

在此处输入图像描述


mgcv可以做球面样条。这将替换akima(不需要包含 interp() 的块)。

library(mgcv)
spl1 <- gam(signal ~ s(x, y, bs = 'sos'), data = dat)
# fine grid, coarser is faster
datmat2 <- data.frame(expand.grid(x = seq(0, 1, 0.001), y = seq(0, 1, 0.001)))
resp <- predict(spl1, datmat2, type = "response")
datmat2$value <- resp

在此处输入图像描述

于 2016-01-27T00:30:15.323 回答