Spatial Point Pattern Analysis

library(spatgraphs)
graph_example2d<-function(n=50,k=4,R=0.2)
  {
   pp2d<-list(x=runif(n),y=runif(n),n=n,window=list(x=c(0,1),y=c(0,1)))
   e1<-spatgraph(pp2d,"geometric",par=R)
   e2<-spatgraph(pp2d,"knn",par=k)
   e3<-spatgraph(pp2d,"MST")
   A<-spatcluster(e2)
   par(mfrow=c(1,3))
   plot(pp2d,main=paste("Geometric,R =",R))
   plot(e1,pp2d)
   plot(pp2d,main=paste("k-nn, k =",k))
   plot(e2,pp2d)
   plot(A,pp2d,pch=19)
   plot(pp2d, main="Minimum spanning tree")
   plot(e3,pp2d)
  }
  graph_example2d()
  
  #library(rgl)
  graph_example3d<-function(n=200)
  {
	w<-c(0,1)
	phi<-runif(n,0,pi);tau<-runif(n,0,2*pi);r<-runif(n)^0.33
	pp3d<-list(x=r*sin(tau)*cos(phi),y=r*cos(phi)*cos(tau),z=r*cos(phi),n=n,window=list(x=w,y=w,z=w))
	e<-spatgraph(pp3d,"RST",par=c(x=0,y=0,z=0))
	plot3d(pp3d,size=2, main="Radial spanning tree",col="black")
	plot(e,pp3d,col="plum")
  }
  #graph_example3d()
library(spatstat)
demo(spatstat)
Npt<-300
x<-y<-rep(0,Npt)
r<-1
for(i in 1:Npt){
	t<-runif(1)*2*pi
	r2<-r*(sin(t*3)+2)+rnorm(1)*0.3
	x[i]<-r2*cos(t)
	y[i]<-r2*sin(t)
}
	pp2d<-list(x=x,y=y,n=length(x),window=list(x=range(c(x,y)),y=range(c(x,y))))
	R<-0.2
	k<-2
   e1<-spatgraph(pp2d,"geometric",par=R)
   e2<-spatgraph(pp2d,"knn",par=k)
   e3<-spatgraph(pp2d,"MST")
      A<-spatcluster(e2)

   par(mfrow=c(1,3))
   plot(pp2d,main=paste("Geometric,R =",R))
   plot(e1,pp2d)
   plot(pp2d,main=paste("k-nn, k =",k))
   plot(e2,pp2d)
   plot(A,pp2d,pch=19)
   plot(pp2d, main="Minimum spanning tree")
   plot(e3,pp2d)