围绕一组点绘制曲线 [英] plotting a curve around a set of points
问题描述
我在飞机上有一组点。它们被分成子集。
我想在属于同一子集的点周围绘制一条闭合曲线,以使属于子集的点位于曲线的内部,而不属于子集的点将在曲线的外部。因此,简单的圆或凸包可能不起作用。
对于初学者来说,我只想在一组点周围有一条平滑的曲线(不需要这样)
-稍后再添加?
任何想法如何在R中做到这一点?
---
我最终要看的东西是这里图形的精神:https://tex.stackexchange.com/questions/1175/drawing-a-hypergraph -尽管上下文不是一个超图,而是一组给定的点和这些点的一个分区。
好的,这是一个答案的版本,我认为它与您所追求的目标很接近:
它使用在此答案上创建的 spline.poly
函数( https://gis.stackexchange.com/a/24929 )在GIS论坛上。
以下是一些足够的点:
testpts<-
结构(list(x = c(4.9,4.2,4,4.1 ,4.4、5.8、5.8、5.8、5.8、5.8,
5.5、4.9、3.2、3.2、3.3、5.4、5.4、5.7、6.4、6.7、6.7、6、4.8,
3.6、2.8、3.5 ,4.4,5.1,4,3.7,4.5,4.9,5.7),y = c(6.9,6.2,
5.3,4.1,3.1,2.9,2.9,3.5,4.2,4.9,5.1,4.9,4.9, 5.2、6.9,
6.9、5.3、3.8、4.2、5.6、6.9、5.8、1.2、2.5、5.3、6.4、6.8、7.6,
6.9、5.4、4.8、4.4))、. = c( x, y))
设置基本图
图(NA,xlim = c(0,10),ylim = c(0,10))
分(testpts, pch = 19)
多边形<-lapply(testpts, [,chull(testpts))
多边形(chuld,lty = 2,border = gray)
多边形(样条曲线.poly(as.matrix(as.data.frame(chuld)),100),border = red,lwd = 2)
结果为:
编辑以添加示例示例
这答案的一部分使用 alphahull
库
#加载所需的库
库(alphahull)
图(NA,xlim = c(0,10),ylim = c(0,10))
分(testpts,pch = 19)
#删除重复的点,以便ahull函数不会出错
testptsnodup<-lapply(testpts, [,which(!duplicated(as.matrix(as.data.frame(testpts)) ))))
生成并绘制ahull对象-alpha值对于确定对象非常重要
ahull.obj<-ahull(testptsnodup,alpha = 2)
图(ahull.obj,add = TRUE,col = red,wpoints = FALSE)
结果为:
I have a set of points on a plane. They are partitioned into subsets. I want to plot a closed curve around points that belong to the same subset, so that points that belong to a subset will be inside the curve, and those that aren't will be outside. Therefore simple circles, or a convex hull might not work.
For a starter, let's say I just want to have a smooth curve around a set of point (without the requirement that it excludes other points)
Any ideas how to do that in R?
---added later---
What I'm looking eventually, is something in the spirit of the graphics in here: https://tex.stackexchange.com/questions/1175/drawing-a-hypergraph - although the context is not a hypergraph, but rather a given set of points and a partition of those.
Okay, here's a version of an answer that I think gets close to what you are chasing:
It uses the spline.poly
function created over at this answer ( https://gis.stackexchange.com/a/24929 ) on the GIS forum.
Here's some example points:
testpts <-
structure(list(x = c(4.9, 4.2, 4, 4.1, 4.4, 5.8, 5.8, 5.8, 5.8,
5.5, 4.9, 3.2, 3.2, 3.3, 5.4, 5.4, 5.7, 6.4, 6.7, 6.7, 6, 4.8,
3.6, 2.8, 3.5, 4.4, 5.1, 4, 3.7, 4.5, 4.9, 5.7), y = c(6.9, 6.2,
5.3, 4.1, 3.1, 2.9, 2.9, 3.5, 4.2, 4.9, 5.1, 4.9, 4.9, 5.2, 6.9,
6.9, 5.3, 3.8, 4.2, 5.6, 6.9, 5.8, 1.2, 2.5, 5.3, 6.4, 6.8, 7.6,
6.9, 5.4, 4.8, 4.4)), .Names = c("x", "y"))
Set up a basic plot
plot(NA,xlim=c(0,10),ylim=c(0,10))
points(testpts,pch=19)
chuld <- lapply(testpts,"[",chull(testpts))
polygon(chuld,lty=2,border="gray")
polygon(spline.poly(as.matrix(as.data.frame(chuld)),100),border="red",lwd=2)
And the result:
EDIT TO ADD A CONCAVE EXAMPLE
This part of the answer uses the alphahull
library
# load the required library
library(alphahull)
plot(NA,xlim=c(0,10),ylim=c(0,10))
points(testpts,pch=19)
# remove duplicate points so the ahull function doesn't error out
testptsnodup <- lapply(testpts,"[",which(!duplicated(as.matrix(as.data.frame(testpts)))))
Generate and plot the ahull object - the alpha value seems to be very important in determining the fit of the polygon to the data.
ahull.obj <- ahull(testptsnodup,alpha=2)
plot(ahull.obj,add=TRUE,col="red",wpoints=FALSE)
And the result:
这篇关于围绕一组点绘制曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!