0

I've been struggling with this for a long time. I'd like to create a raster plot in R to show the times of the action potentials (AP) of a neuron relative to a stimulus. Something like this: Each row represents the AP series in a given time window aligned to a stimulus repeated several times (sometimes at different frequencies)

I have a matrix containing the AP times relative to each stimulus (positive and negative time values).

I figured that stripchart is a good solution as it plots one dimensional data. How can I plot each row of my matrix (or selected elements of the rows) to look like the attached image. I know that stripchart function can also plot lists containing numeric vectors with different length, so first I selected the suitable elements from each row, created a list and ploted. I also tried as.list function applied to the matrix so I didn't have to create a new list when I wanted to change the condition that selects the elements of the matrix. It seemed OK, but I have two problems with this. First, somehow I wasn't sure that it did the plotting correctly (row by row, selected elements) and also I'm not sure that this is the most effective way to do it. Second, since I had a lot of rows they didn't remain separate in the stripchart like they did on the first image. I've tried to adjust the parameters of the stripchart function but nothing helped. My plot looks like this:enter image description here

In summary I'm looking for an effective way to plot matrix rows (selected elements) as one dimensional vectors keeping the rows separate on the image.

Thank you for your help!

1 Answers1

0

It looks like stripchart works with dataframes by column.... So in short, transpose your matrix, convert your matrix to a df

x1 <- matrix(rnorm(50),nrow = 5)
x1<-cbind(x1, rep(4,5))
df<-as.data.frame(t(x1))
df
stripchart(df, pch = "|", las=1)

I think the reason that stripchart does not handle matrices as expected is because in R a matrix is just a vector internally...

Hope this helps.

My stripchart plot

Matplot

I can't seem to make it rotate the plot 90 degrees, though this does uses matrices as is with no coercion

matplot(x1, pch="_", col="black")

Matplot

Version 2 of matplot:

With some photoshopping this may work...

par(mar=c(5.1,2.1,4.1,4.1))
matplot(x1, pch="_", col="black", las=3, yaxt="n", ylab="")
axis(4)
mtext("x1",side = 4,line = 2)

enter image description here

Community
  • 1
  • 1
emilliman5
  • 5,816
  • 3
  • 27
  • 37
  • Yeah, I thought about that but I was trying to avoid this solution intentionally for multiple reasons. Most importantly because I work with really huge matrices and combine several of these to one, which creates a quite big df. I found it very slow but I'm gonna try to reduce the size and give it another shot. Thank you. – Viktor Plattner Oct 21 '16 at 18:36
  • I've found this as well. Can be good but still not exactly what I need. I think I will have to work on my data structure. – Viktor Plattner Oct 21 '16 at 18:52
  • @ViktorPlattner, how large of a dataset are we talking here? I regularly make 5 million row dataframes without much of an issue. If your dataset is really that large then you might be better off considering some other summary statistic plot rather than plotting every observation. – Bishops_Guest Oct 21 '16 at 19:10
  • Well, it's definitely not that big so probably it will work – Viktor Plattner Oct 21 '16 at 21:12