【问题标题】:Package for Divide Chain of tesselations in R, spatstat package?R 中镶嵌分割链的包,spatstat 包?
【发布时间】:2016-02-25 18:29:28
【问题描述】:

我正在尝试创建漂亮的聚类点图形。是否有一个包可以在点的镶嵌之间创建分界链?理想情况下,它适合在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

有什么想法/选择吗?

【问题讨论】:

    标签: r ggplot2 voronoi spatstat dirichlet


    【解决方案1】:

    我写了一个函数,我认为它会做你想做的事:

    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
    }
    

    我还为“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。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 我不确定你指的是什么进程。请解释。我没有任何功能性多边形可以在多边形测试中做点,我正在寻找一个包或解决方法来为每个镶嵌创建多边形。
      • IMO 你过度设计了它。只需计算所有站点的 vd 并将其与您的集群进行比较。我错过了什么吗?
      • 问题是我正在使用网格数据集,所以如果我这样做,我只会得到不准确且不符合出版质量的丑陋方块。
      猜你喜欢
      • 2015-05-16
      • 2019-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-07
      • 1970-01-01
      • 1970-01-01
      • 2021-07-14
      相关资源
      最近更新 更多