r - 如何使用 ggplot2 在 R 中绘制刺激时间直方图 (PSTH)

标签 r ggplot2 neuroscience

假设我有两个条件,“a”和“b”。一个神经元在条件“a”下平均发射 40 个脉冲/秒 (Hz),在条件“b”下平均发射 80 个脉冲/秒。对条件“a”的响应呈现 20 次,条件“b”呈现 10 次,每次呈现 1000 毫秒。

AB <- rbind(
    ldply( 1:20, 
        function(trial) { 
          data.frame( 
              trial=trial, 
              cond=factor('a',c('a','b')), 
              spiketime = runif(40,0,1000))
        }
    ), ldply(21:30, 
        function(trial) {
          data.frame(
              trial=trial, 
              cond=factor('b',c('a','b')), 
              spiketime = runif(80,0,1000))
        }
  )
)

可以绘制一个简单的直方图:
qplot(spiketime, data=AB, geom='line',stat='bin',y=..count.., 
      xlim=c(0,1000), colour=cond, binwidth=100,xlab='Milliseconds')

然而,这并没有对演示进行平均,因此,y 轴上的值大致相同。我想沿着 y 轴绘制尖峰速率(尖峰/秒),这将表明条件“b”每秒引起大约两倍的尖峰。尖峰率不会随着演示次数的增加而增加,它只是变得不那么嘈杂。有没有办法在不预处理数据帧 AB 的情况下做到这一点?

换句话说,我可以按照以下方式做一些事情:
qplot(spiketime, data=AB, geom='line',stat='bin',
      y=..count../num_presentations*1000/binwidth, ylab='Spikes per second',
      xlim=c(0,1000), colour=cond, binwidth=100,xlab='Milliseconds')

其中,条件“a”的 num_presentations 为 20,条件“b”的 num_presentations 为 10,而 1000/binwidth 只是使单位正确的常量?

最佳答案

这是一个解决方案:

AB$ntrial <- ifelse(AB$cond=="a", 20, 10)
ggplot(AB, aes(spiketime, ntrial=ntrial, colour=cond)) + 
  stat_bin(aes(y=..count../ntrial*1000/100), binwidth=100, geom="line", position="identity") +
  xlim(0,1000) + 
  labs(x='Milliseconds', y="Firing rate [times/s]")

关于r - 如何使用 ggplot2 在 R 中绘制刺激时间直方图 (PSTH),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7070648/

相关文章:

python - 如何使用 NiBabel (Python) 预处理 NIfTI 数据格式

r - stargazer()LaTeX输出中的赔率比率而不是logits

r - Quantstrat:回测自定义 'bollinger band' 类策略

r - 如何在 R 中提取两列数据

r - 将 save_kable() 保存为白色背景的 png,而不是默认的灰色

r - 如何将 stat_function 美学映射到 ggplot2 中的数据?

machine-learning - 泄漏积分和火神经元模型

r - 在 ggplot2 中,如何使 stat_function 服从 scale_x_log10?

r - 将 directlabels 稍微向左移动

python - 如何通过MNE-Python读取Enobio8设备EGG信号?