【问题标题】:subplot within a region of a plot in rr中绘图区域内的子图
【发布时间】:2013-02-12 13:06:45
【问题描述】:
> d
         [,1]        [,2]
1  -0.5561835  1.49947588
2  -2.3985544  3.07130217
3  -3.8833659 -4.29331711
4   3.1025836  5.45359160
5   0.7438354 -2.80116065
6   7.0787294 -2.78121213
7  -1.6633598 -1.17898157
8  -0.6751930  0.03466162
9   1.4633841  0.50173157
10 -3.2118758  0.49390863

上表给出了我想要绘制的绘图的 x(第 1 列)和 y(第 2 列)坐标。

require(MASS)               # for sammon using which i generated the above coordinates
require(deldir)             # for voronoi tessellations
dd <- deldir(d[,1], d[,2])  # voronoi tessellations
plot(dd,wlines="tess")      # This will give me tessellations

我希望在上述细分中的一个区域中绘制我的下一个细分。我可以使用 dd$dirsgs 获得形成镶嵌的线条。在此,镶嵌中的每条线都给出了它们的端点。前四列分别给出 x1,y1 和 x2,y2 坐标。这些坐标是线的端点。使用这些数据,我可以在上述细分中的这一区域内绘制下一个子细分。

对于下一个子细分,您可以生成您选择的坐标。但我只希望它们位于上述曲面细分的一个区域中。

dd$dirsgs 中的 ind 1 和 ind2 给出了 'd' 中的点,它们由 dd$dirsgs 的前 4 列表示的线分隔。

例如,如果我们要在包含 d 中的第一个点的图中绘制子曲面细分,那么第 1、2、9、12、17 行是形成 d 中第一个点的边界的行.使用这些信息,我们可以绘制该区域内的子细分吗? ——

我想我已经涵盖了理解我的问题所必需的所有内容。如果还有我没有包含的更多数据,请告诉我。我会提供信息。

【问题讨论】:

  • 我认为答案在于将边界传递到 mar 字段。 mar= c(-10,-10,10,10) 在两个轴上创建一个介于 -10 和 10 之间的区域。因此,如果我们可以传递坐标函数,例如c(3-y,4-x,6+x,2+y),它可以在 x+y=3、x+y=4、y-x=6 和 x-y=2 行之间产生一个界限。
  • par 函数的 mar 参数给出了一个矩形作为边界,但在上面的镶嵌中,我可以有 5 条或更多条线形成特定区域的边界。我还使用了“TeachingDemos”包的“子图”功能,但在我的情况下无法应用。你也可以试试“subplot”,看看你们有没有发现
  • 您是否尝试过使用少于 4 个变量的 mar?我相信它可能会持续到无穷大。如果发生这种情况,那么它也可能会接受更多。试一试!

标签: r plot voronoi tessellation


【解决方案1】:

我理解它的方式(我的意思是如果我理解正确你的问题),因为plot.deldir 允许传递参数add=TRUE,它可以直接完成。

d<-structure(list(V1 = c(-0.5561835, -2.3985544, -3.8833659, 3.1025836, 0.7438354, 
                  7.0787294, -1.6633598, -0.675193, 1.4633841, -3.2118758), V2 = 
                  c(1.49947588, 3.07130217, -4.29331711, 5.4535916, -2.80116065, 
                  -2.78121213, -1.17898157, 0.03466162, 0.50173157, 0.49390863)), .Names =        
                  c("V1","V2"), class = "data.frame", row.names = c(NA, -10L))

library(MASS)
library(deldir)
dd <- deldir(d[,1], d[,2])
plot(dd, wlines="tess")

首先让我们提取多边形的数据:正如您在 cmets 中注意到的那样,它需要我之前认为的更多处理,因为 plot.deldir 中的多边形是逐行绘制的,而不是一个接一个多边形的,所以线条的顺序是打乱了dd$dirsgs。

ddd <- as.matrix(dd$dirsgs[dd$dirsgs$ind2==1,1:4])
d1poly <- rbind(ddd[1,1:2],ddd[1,3:4])
for( i in 2:nrow(ddd)){
    x <- ddd[ddd[,1]==d1poly[i,1], 3:4]
    d1poly <- rbind(d1poly, x)
    }
d1poly
         x2       y2
  -2.096990 1.559118
   0.303986 4.373353
x  1.550185 3.220238
x  0.301414 0.692558
x -1.834581 0.866098
x -2.096990 1.559118

让我们使用包splancs 在感兴趣的多边形中创建一些随机数据:

library(splancs)
rd <- csr(as.matrix(d1poly),10) # For 10 random points in the polygon containing point 1
rd
              xc        yc
 [1,] -1.6904093 1.9281052
 [2,] -1.1321334 1.7363064
 [3,]  0.2264649 1.3986126
 [4,] -1.1883844 2.5996515
 [5,] -0.6929208 0.8745020
 [6,] -0.8348241 2.3318222
 [7,]  0.9101748 1.9439797
 [8,]  0.1665160 1.8754703
 [9,] -1.1100710 1.3517257
[10,] -1.5691826 0.8782223

rdd <- deldir(c(rd[,1],d[1,1]),c(rd[,2],d[1,2])) 
# don't forget to add the coordinates of your point 1 so it s part of the sub-tessellation
plot(dd, wlines="tess")
plot(rdd, add=TRUE, wlines="tess")

编辑
关于限制边界内的线,我能想到的唯一解决方案是一个非常丑陋的解决方法:首先绘制子细分,然后隐藏感兴趣的多边形的外部,然后绘制全局细分。

plot(dd, wlines="tess", col="white", wpoints="none")
plot(rdd, wlines="tess", add=TRUE)

plotlim <- cbind(par()$usr[c(1,2,2,1)],par()$usr[c(3,3,4,4)])
extpoly <- rbind(plotlim, d1poly) 
#Here the first point of d1poly is oriented toward the upper left corner: if it is oriented otherwise the order of plotlim has to be changed accordingly

polygon(extpoly, border=NA, col="white")

plot(dd, wlines="tess", add=TRUE)

【讨论】:

  • 谢谢plannapus :) 我的问题还有一些限制...但是使用您提供的信息我想我应该找到解决方案...我会在这方面做更多的工作,如果有什么问题我会告诉你的。
  • 有没有办法限制边界内的镶嵌线。我不希望他们走出边界
  • @Chetan 我正在寻找一种方法,但没有什么明显的想法。如果我找到什么,我会再找你
【解决方案2】:

您可能要考虑为此使用spatstat 包,因为它可以大大简化将新细分约束到现有细分的图块的过程。您的设置将如下所示:

library(spatstat)
# Plot the main tessellation and points
d<-structure(list(V1 = c(-0.5561835, -2.3985544, -3.8833659, 3.1025836, 0.7438354, 
                  7.0787294, -1.6633598, -0.675193, 1.4633841, -3.2118758), V2 = 
                  c(1.49947588, 3.07130217, -4.29331711, 5.4535916, -2.80116065, 
                  -2.78121213, -1.17898157, 0.03466162, 0.50173157, 0.49390863)), .Names =        
                  c("V1","V2"), class = "data.frame", row.names = c(NA, -10L))

d_points <- ppp(d$V1, d$V2, window=owin(c(-5, 8), c(-6, 6)))
main_tessellation <- dirichlet(d_points)
plot(main_tessellation, lty=3) # plot the tessellation
plot(d_points, add=TRUE) # add the points

# Plot the interior tessellation and points (color=red so the difference is clear)
# Arbitrarily choosing the 9th tile from the above tessellation:
target_poly <- owin(poly=main_tessellation$tiles[[9]]$bdry[[1]])

# Generate random set of points within the boundaries of the polygon chosen above
new_points <- runifpoint(6, win=target_poly)
# Generate and plot the new tessellation and points
new_tessellation <- dirichlet(new_points)
plot(new_tessellation, add=TRUE, col='red')
plot(new_points, add=TRUE, col='red')

这将产生:

查看这个密切相关的问题:Voronoi diagram polygons enclosed in geographic borders

【讨论】:

    猜你喜欢
    • 2017-04-14
    • 2015-06-04
    • 2012-09-01
    • 2012-07-10
    • 2016-02-13
    • 1970-01-01
    • 2016-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多