R 中分割链的包,spatstat 包?

Package for Divide Chain of tesselations in R, spatstat package?

我正在尝试创建漂亮的聚类图。是否有一个包可以在点细分之间创建分界链?理想情况下,它适合在 ggplot.

中绘图

这是一些示例代码:

#DivideLineExample
library(spatstat)

W=owin(c(0,1),c(0,1))          # Set up the Window
p<-runifpoint(42, win=W)       # Get random points

ll=cbind(p$x,p$y)              # get lat/long for each point
zclust=kmeans(ll,centers=4)    # Cluster the points spatially into 4 clusters

K<-pp<-D<-list()
plot(W,main="Clustered Points")
for (i in 1:4){                   # this breaks up the points into separate ppp objects for each cluster
  K[[i]]=ll[zclust$cluster==i,]   
  pp[[i]]=as.ppp(K[[i]],W)
  plot(pp[[i]],col=i,add=TRUE,cex=1.5,pch=16)
  D[[i]]=dirichlet(pp[[i]])       # This performs the Dirichlet Tessellation and plots
  plot(D[[i]],col=i,add=TRUE)
}

输出如下: http://imgur.com/CCXeOEB

我要找的是这个: http://imgur.com/7nmtXjo

我知道一个算法exists

任何ideas/alternatives?

您可以在多边形测试中尝试点,例如 kirkpatrick 数据结构。在水平或垂直方向上划分多边形要容易得多。来源:http://www.personal.kent.edu/~rmuhamma/Compgeometry/MyCG/Voronoi/DivConqVor/divConqVor.htm

我已经编写了一个函数,我认为它可以满足您的要求:

divchain <- function (X) {
    stopifnot(is.ppp(X))
    if(!is.multitype(X)) {
        whinge <- paste(deparse(substitute(X)),
                        "must be a marked pattern with",
                        "factor valued marks.\n")
        stop(whinge)
    }
    X <- unique(X, rule = "deldir", warn = TRUE)
    w <- Window(X)
    require(deldir)
    dd <- deldir(X,z=marks(X),rw=c(w$xrange,w$yrange))
    if (is.null(dd)) 
        return(NULL)
    ddd <- dd$dirsgs
    sss <- dd$summary
    z   <- sss[["z"]]
    rslt <- list()
    nsgs <- nrow(ddd)
    K <- 0
    for (i in 1:nsgs) {
         i1 <- ddd[i,5]
         i2 <- ddd[i,6]
         c1 <- z[i1]
         c2 <- z[i2]
         if(c1 != c2) {
             K <- K+1
             rslt[[K]] <- unlist(ddd[i,1:4])
         }
    }
    class(rslt) <- "divchain"
    attr(rslt,"rw") <- dd$rw
    rslt
}

我也写过class的plot方法 "divchain":

plot.divchain <- function(x,add=FALSE,...){
    if(!add) {
        rw <- attr(x,"rw")
        plot(0,0,type="n",ann=FALSE,axes=FALSE,xlim=rw[1:2],ylim=rw[3:4])
        bty <- list(...)$bty
        box(bty=bty)
    }
    lapply(x,function(u){segments(u[1],u[2],u[3],u[4],...)})
    invisible()

}

例如:

require(spatstat)
set.seed(42)
X <- runifpoint(50)
z <- factor(kmeans(with(X,cbind(x,y)),centers=4)$cluster)
marks(X) <- z
dcX <- divchain(X)
plot(dirichlet(X),border="brown",main="")
plot(X,chars=20,cols=1:4,add=TRUE)
plot(dcX,add=TRUE,lwd=3)

让我知道这是否令人满意。抱歉,我无法在 ggplot 方面为您提供帮助;我不做 ggplot。