1

Just getting started with R and would value your input on this question.

What I'm trying to achieve is that:

  1. X axis has all values for "Timestamp"(from 0 to 9)
  2. Y axis has all values for "NID"(from 0 to 3)
  3. There are "dots" at the coordinates of ("Timestamp","NID") where the attribute "Fired" = 1.

The source data has the following format:

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

I tried to apply a filter, but it shows a subset of data for those "ID"s, where there is value 1 for the attribute "Fired" (no all values for the axes):

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

Case 1

Alternatively, I get all existing values for the attributes "Timestamp" and "NID" by using the following code:

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

so the picture looks in the following way:

enter image description here

Finally, from the comment below I modified the code to:

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

Is that the case that i should build two charts on one plot? Thank you!

Sergey Nasonov
  • 103
  • 1
  • 7
  • First of all, you don't need the `dat$` in your pipped commands. You can use just the variables names. – AntoniosK Jun 08 '18 at 17:24
  • Second, you won't need to build 2 chars if you manage to find a useful way to plot everything in one chart. I guess the problem is that some points will fall on top of others. But you can use different colors, different shapes, or `geom_jitter` instead of `geom_point`. – AntoniosK Jun 08 '18 at 17:28
  • 2
    Please post data in questions using `dput()` or similar so that others can reproduce your code, rather than posting images of data. See [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for details on including data. – Jan Boyer Jun 08 '18 at 20:36

2 Answers2

1

With ggplot2, never use data$ inside aes(), just use the column names. Similarly, the dplyr functions like filter should not be used with data$ - they know to look in the data frame for the column.

I think you want to build your ggplot with the full data, so the axes get set to cover the full data (we force this by adding a geom_blank() layer), and it is only the point layer that should be subset:

# 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

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • Hi, would it be possible for you to look at this example again? The problem is when i run your piece of code, it says "object 'Fires' not found" I use http://rextester.com/l/r_online_compiler – Sergey Nasonov Jul 03 '18 at 16:58
  • Your rextester link doesn't have any data. Does the data your are passing to the `ggplot` have a column named `Fires`? If so, it should work just fine, as I show with the sample data I made. If you provide sample data in a reproducible way, that would give me something to look at. See the FAQ on [making a reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for guidance on that - data shared with `dput()` is best because it gives all the structure and class information and it is copy/pasteable. – Gregor Thomas Jul 03 '18 at 17:35
  • Hi Gregor, I have added the entire code to the Rex: http://rextester.com/BPP88514 – Sergey Nasonov Jul 03 '18 at 18:57
  • 1
    The problem is that rextester didn't load `dplyr`, so it was trying to use `stats::filter` instead of `dplyr::filter`. Just replace `filter` with `subset` and it works fine. – Gregor Thomas Jul 03 '18 at 19:00
  • Wonderful! Thank you very much! – Sergey Nasonov Jul 03 '18 at 19:10
0

The code should look like that (see reasons in the comments):

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) ) 
Sergey Nasonov
  • 103
  • 1
  • 7