简体   繁体   English

在带状图中绘制矩阵行作为R中的栅格图

[英]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. 我想在R中创建一个栅格图,以显示神经元相对于刺激的动作电位(AP)的时间。 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) 像这样: 每行代表给定时间窗口中的AP系列,与重复几次(有时以不同频率)的刺激对齐

I have a matrix containing the AP times relative to each stimulus (positive and negative time values). 我有一个矩阵,其中包含相对于每个刺激的AP时间(正负时间值)。

I figured that stripchart is a good solution as it plots one dimensional data. 我认为stripchart是一个很好的解决方案,因为它可以绘制一维数据。 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. 我知道stripchart函数还可以绘制包含长度不同的数值向量的列表,因此首先我从每一行中选择合适的元素,创建一个列表并进行绘制。 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. 我还尝试了将as.list函数应用于矩阵,因此当我想更改选择矩阵元素的条件时,不必创建新列表。 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. 我试图调整stripchart函数的参数,但没有任何帮助。 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 看起来stripchart按列处理数据帧。...总之,转置矩阵,将矩阵转换为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... 我认为stripchart不能按预期处理矩阵的原因是因为在R中,矩阵只是内部的向量...

Hope this helps. 希望这可以帮助。

我的脱衣舞剧情

Matplot Matplot

I can't seem to make it rotate the plot 90 degrees, though this does uses matrices as is with no coercion 我似乎无法使绘图旋转90度,尽管它确实使用矩阵而没有强制性

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

Matplot

Version 2 of matplot: matplot的版本2:

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)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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