R plot Fire Trace

时间:2018-06-08 17:20:43

标签: r plot ggplot2 trace

刚开始使用R并且会在这个问题上重视您的意见。

我想要实现的目标是:

  1. X轴具有"时间戳"(从0到9)的所有值
  2. Y轴具有" NID"(从0到3)
  3. 的所有值
  4. 有"点"在(" Timestamp"," NID")的坐标处,其中属性" Fired" = 1。
  5. 源数据具有以下格式:

    dat = structure(list(TimeStamp = c(0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 
    1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 
    4L), NID = c(0L, 1L, 2L, 3L, 4L, 0L, 1L, 2L, 3L, 4L, 0L, 1L, 
    2L, 3L, 4L, 0L, 1L, 2L, 3L, 4L, 0L, 1L, 2L, 3L, 4L), NumberSynapsesTotal = c(2L, 
    2L, 3L, 2L, 4L, 2L, 2L, 3L, 2L, 4L, 2L, 2L, 3L, 2L, 4L, 2L, 2L, 
    3L, 2L, 4L, 2L, 2L, 3L, 2L, 4L), NumberActiveSynapses = c(1L, 
    2L, 1L, 2L, 3L, 1L, 2L, 1L, 1L, 0L, 1L, 2L, 1L, 1L, 0L, 1L, 2L, 
    1L, 1L, 0L, 1L, 0L, 0L, 1L, 0L), Fires = c(1L, 1L, 1L, 1L, 0L, 
    1L, 1L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 1L, 
    0L, 1L, 0L, 0L)), row.names = c(NA, 25L), class = "data.frame")
    

    我尝试应用过滤器,但它显示了那些" ID" s的数据子集,其中属性的值为1" Fired" (没有轴的所有值):

    dat %>%
    filter(dat$Fires == 1) %>%
    ggplot(aes(x = dat$TimeStamp[dat$Fires == 1], y = dat$NID[dat$Fires == 1])) +
    geom_point()
    

    Case 1

    或者,我获得属性的所有现有值" Timestamp"和" NID"使用以下代码:

     plot(dat$TimeStamp, dat$NID,
     xlab = "Time", ylab = "Neuron ID")
     title(main = "Fire Trace Plot")
    

    因此图片以下列方式显示:

    enter image description here

    最后,从下面的评论中我将代码修改为:

    ggplot(dat, aes(x = TimeStamp, y = NID) , xlab = "Time", ylab ="Neuron 
    ID") +
    geom_blank() +
    geom_point(dat = filter(dat) +
    #title(main = "Fire Trace Plot")
    scale_x_continuous(breaks = F_int_time_breaks(1) ) 
    

    我应该在一个地块上建立两个图表吗? 谢谢!

2 个答案:

答案 0 :(得分:1)

使用ggplot2,永远不要在data$内使用aes(),只需使用列名。同样,dplyrfilter函数不应与data$一起使用 - 他们知道要查看列的数据框。

我认为您希望使用完整数据构建ggplot,因此将轴设置为覆盖整个数据(我们通过添加geom_blank()图层强制执行此操作),并且它只是点层应该是子集:

# create some sample data (it is nice if you provide this in the question)
dat = expand.grid(Timestamp = 0:9, NID = 0:3)
dat$Fires = ifelse(dat$NID == 2, 1, 0)

# make the plot
ggplot(dat, aes(x = Timestamp, y = NID)) +
    geom_blank() +
    geom_point(dat = filter(dat, Fires == 1))

enter image description here

答案 1 :(得分:0)

代码应如下所示(请参见注释中的原因):

F_int_time_breaks<- function(k) {
  step <- k
  function(y) seq(floor(min(y)), ceiling(max(y)), by = step)       
}

ggplot(dat, aes(x = TimeStamp, y = NID) , xlab = "Time", ylab ="Neuron ID") +
  geom_blank() +
  geom_point(dat = subset(dat, Fires == 1)) +
  #title(main = "Fire Trace Plot")
  scale_x_continuous(breaks = F_int_time_breaks(1) )