graph_example2d<-function(n=50,k=3,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",pars=list(R=R))
e2<-spatgraph(pp2d,"kmnn",pars=list(k=k))
e3<-spatgraph(pp2d,"MST")
A<-sgcluster(e2)
par(mfrow=c(1,3))
plot(pp2d,main=paste("Geometric,R =",R));plot_graph(pp2d,e1,add=TRUE)
plot_graph(pp2d,e2,main=paste("Mutual k-nn, k =",k))
plot_clusters(pp2d,A,add=TRUE)
plot_graph(pp2d,e3,main="Minimum spanning tree")
};graph_example2d()
graph_example3d<-function(n=200)
{
library(rgl)
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",pars=list(x=0,y=0,z=0))
plot_graph(pp3d,e,pointsize=2,main="Radial spanning tree",linecolor="plum")
};
graph_example3d()
Run the code above in your browser using DataLab