简体   繁体   English

如何在R面板图中从一个图绘制线条或箭头到另一个图?

[英]How to draw lines or arrows from one plot to another in a R panel plot?

I am tracking objects in images. 我正在跟踪图像中的对象。 I want to show my results on a plot. 我想在绘图上显示我的结果。 So I use image2D() function to plot two consecutive images and label the objects by numbers then shows how they maps from image1 to image2 as 16 => 0, 17 => 16, 18 => 0, 19 => 17, 20 => 18, 21 => 20, 22 => 19, 23 => 22, 24 => 23, 25 => 25, 26 => 24 0 = object died. 因此,我使用image2D()函数绘制两个连续的图像,并用数字标记对象,然后显示它们如何从image1映射到image2,如下所示: 16 => 0, 17 => 16, 18 => 0, 19 => 17, 20 => 18, 21 => 20, 22 => 19, 23 => 22, 24 => 23, 25 => 25, 26 => 24 0,17 16 => 0, 17 => 16, 18 => 0, 19 => 17, 20 => 18, 21 => 20, 22 => 19, 23 => 22, 24 => 23, 25 => 25, 26 => 24 0,19 16 => 0, 17 => 16, 18 => 0, 19 => 17, 20 => 18, 21 => 20, 22 => 19, 23 => 22, 24 => 23, 25 => 25, 26 => 24 17,20 16 => 0, 17 => 16, 18 => 0, 19 => 17, 20 => 18, 21 => 20, 22 => 19, 23 => 22, 24 => 23, 25 => 25, 26 => 24 0 =对象死亡。

Now, I want to show arrow from n^th object (say 26) in image1 to m^th in image2 (ie 24) for visually intuitive presentation. 现在,我想显示从图像1中的第n个对象(例如26)到图像2中的第m个对象(即24)的箭头,以实现直观的呈现。 Is it possible to draw an arrow from given coordinates in plot1 p1(x1, y1) to coordinates in plot2 p2(x2, y2)? 是否可以从plot1 p1(x1,y1)中给定坐标绘制箭头到plot2 p2(x2,y2)中的坐标绘制箭头?

在此处输入图片说明

EDIT: Adding a sample script showing dummy image plots. 编辑:添加一个示例脚本,显示虚拟图像图。

library(plot3D)

#we will create dummy images first
img1 <- matrix(0, nrow = 100, ncol = 100)
img2 <- matrix(0, nrow = 100, ncol = 100)

#add an objects to img1 and img2
img1[41:44, 31:35] <- rnorm(20) #object1 in image 1
img2[44:47, 33:37] <- rnorm(20) #object1 in image 2

img1[11:14, 71:75] <- rnorm(20) #object2 in image 1
img2[14:17, 73:77] <- rnorm(20) #object2 in image 2

# cordinates are x and y
x <- 1:100
y <- 1:100

#now plot them
par(mfrow=c(1,2))
image2D(img1, x, y)
image2D(img2, x, y)

Now, I want to plot two arrows from objects in image1 to objects in image2 to show association. 现在,我想绘制两个箭头,从图像1中的对象到图像2中的对象以显示关联。

You could use grid graphics' limited facilities to draw lines between different viewports, 您可以使用网格图形的有限功能在不同视口之间绘制线条,

library(grid)
library(gridGraphics)

par(mfrow=c(1,2), xaxs="i", yaxs="i")
plot(1:10, 1:10, xlim=c(0,10), ylim=c(0,10))
plot(1:10, 1:10, xlim=c(0,10), ylim=c(0,10))

grid.echo()
grid.ls(viewports = T, grobs = F, flatten = T, recursive = F)
seekViewport("graphics-plot-1")
grid.rect(gp=gpar(col="red", fill=NA))
grid.move.to(unit(8/10, "npc"), unit(8/10, "npc"))
upViewport()
seekViewport("graphics-plot-2")
grid.rect(gp=gpar(col="red", fill=NA))
grid.line.to(unit(4/10, "npc"), unit(4/10, "npc"), gp=gpar(lty=2), arrow = arrow())

在此处输入图片说明

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

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