8

ggplot2いわゆるtopoplot (神経科学でよく使用される)の生成に使用できますか?

トポプロット

サンプルデータ:

   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は 2D 空間への投影を表し、列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 ツールボックスはこれらをほぼ瞬時に生成します。上記の「緊急時の解決策」には約 1 秒かかります。
  2. ご覧のとおり、中央部分の上下の境界線が「スライス」されているように見えます。これが原因かどうかはわかりませんが、3 つ目の問題である可能性があります。
  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)値で生成された 2 つのプロットの比較:

異なる値の比較

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,]

そして、R の等高線プロットの外観が本当に気に入ったので、 ggpplot2 のR plot filled.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()

ここに画像の説明を入力


コメントで言及されている改善点 (ギャップを埋めるための設定と呼び出しで、それぞれスプライン スムージングを行い、プロットする前に NA を削除する) により、次のようになりますextrap = TRUElinear = 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 に答える