gpt4 book ai didi

r - r 中绘图区域内的子绘图

转载 作者:行者123 更新时间:2023-12-04 05:11:49 25 4
gpt4 key购买 nike

> 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(第一列)和 y(第二列)坐标。
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 中第一个点的边界的行。使用这些信息,我们可以绘制该区域内的子分割吗? ——

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

最佳答案

我理解它的方式(我的意思是如果我正确理解你的问题),因为 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")

enter image description here

首先让我们提取多边形的数据:正如您在评论中注意到的,由于 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")

enter image description here

编辑
关于限制边界内的线条,我能想到的唯一解决方案是一个非常丑陋的解决方法:首先绘制分割,然后隐藏感兴趣的多边形的外部,然后绘制全局分割。
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)

enter image description here

关于r - r 中绘图区域内的子绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14833285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com