简体   繁体   中英

Plot matrix rows on stripchart as a raster plot in R

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!

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.

我的脱衣舞剧情

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)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM