R 中比较两个矩阵的图
我有两个矩阵(大约 300 x 100),我想绘制一个图表来查看第一个矩阵高于第二个矩阵的部分。
例如,我可以这样做:
# Calculate the matrices and put them into m1 and m2
# Note that the values are between -1 and 1
par(mfrow=c(1,3))
image(m1, zlim=c(-1,1))
image(m2, zlim=c(-1,1))
image(m1-m2, zlim=c(0,1))
这将仅在第三个图中绘制所需的区域,但我想做一些不同的事情,例如在第一个图上的这些区域周围画一条线,以便直接突出显示它们。
知道我该怎么做吗?
谢谢 尼科
I have two matrices (of approximately 300 x 100) and I would like to plot a graph to see the parts of the first one that are higher than those of the second.
I can do, for instance:
# Calculate the matrices and put them into m1 and m2
# Note that the values are between -1 and 1
par(mfrow=c(1,3))
image(m1, zlim=c(-1,1))
image(m2, zlim=c(-1,1))
image(m1-m2, zlim=c(0,1))
This will plot only the desired regions in the 3rd plot but I would like to do something a bit different, like putting a line around those areas over the first plot in order to highlight them directly there.
Any idea how I can do that?
Thank you
nico
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
怎么样:
这会在区域周围添加等高线图。有点像在第三个图的区域周围放置圆环(可能想要摆弄轮廓的(n)层以获得更少的“圆”)。
How about:
This adds a contour map around the regions. Sort of puts rings around the areas of the 3rd plot (might want to fiddle with the (n)levels of the contours to get fewer 'circles').
制作第三张图像的另一种方法可能是:
这会生成一个 TRUE/FALSE 值矩阵,该矩阵被成像为 0/1,因此您有一个双色图像。不过,仍然不确定你的“划线”事情......
Another way of doing your third image might be:
this produces a matrix of TRUE/FALSE values which gets imaged as 0/1, so you have a two-colour image. Still not sure about your 'putting a line around' thing though...
这是我为执行类似操作而编写的一些代码。我想通过在阈值高于 0.95 的连续区域周围画一个框来突出显示它们,因此我得到了所有高于 0.95 的网格方块,并对它们进行了聚类。然后对聚类输出进行一些摆弄以获取区域的矩形坐标:
测试代码:
这应该为您提供一个矩形坐标矩阵:
现在您可以使用 rect: 在图像上绘制它们
并显示它们所在的位置应该是:
您当然可以调整它来绘制圆圈,但更复杂的形状会很棘手。当感兴趣的区域相当紧凑时,它效果最好。
巴里
Here's some code I wrote to do something similar. I wanted to highlight contiguous regions above a 0.95 threshold by drawing a box round them, so I got all the grid squares above 0.95 and did a clustering on them. Then do a bit of fiddling with the clustering output to get the rectangle coordinates of the regions:
Test code:
That should give you a matrix of rectangle coordinates:
Now you can draw them over the image with rect:
and to show they are where they should be:
You could of course adapt this to draw circles, but more complex shapes would be tricky. It works best when the regions of interest are fairly compact.
Barry