R plot 火迹

R plot Fire Trace

刚开始使用 R,非常重视您对这个问题的意见。

我想要实现的是:

  1. X 轴具有 "Timestamp"(从 0 到 9)
  2. 的所有值
  3. Y 轴具有 "NID"(从 0 到 3)
  4. 的所有值
  5. 在("Timestamp","NID")的坐标处有"dots",其中属性"Fired" = 1.

源数据格式如下:

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" 的数据子集,其中属性 "Fired" 的值为 1(轴没有所有值):

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

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

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

所以图片看起来是这样的:

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

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) ) 

是不是我应该在一个地块上制作两个图表? 谢谢!

对于 ggplot2,永远不要在 aes() 中使用 data$,只需使用列名。同样,filterdplyr 函数不应与 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))

代码应该是这样的(原因见评论):

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) )