### Examples for triMat Source: https://cran.r-project.org/web/packages/deldir/refman/deldir.html This example demonstrates the usage of the triMat function with a dataset that previously caused a bug, illustrating how to plot the Delaunay triangulation. ```R # These are the data used by Jay Call to illustrate the bug # that appeared in a previous incarnation of triMat. xy <- data.frame( x = c(0.048,0.412,0.174,0.472,0.607,0.565,0.005,0.237,0.810,0.023), y = c(0.512,0.928,0.955,0.739,0.946,0.134,0.468,0.965,0.631,0.782) ) dxy <- deldir(xy) M <- triMat(dxy) plot(dxy,wlines="triang",num=TRUE,axes=FALSE,cmpnt_col=c(1,1,1,1,2,1)) # The triangle with vertices {4,5,8} was listed in the output of # the previous (buggy) version of triMat(). It is NOT a Delaunay # triangle and hence should NOT be listed. ``` -------------------------------- ### Duncan Murdoch's Examples Source: https://cran.r-project.org/web/packages/deldir/refman/deldir.html Examples provided by Duncan Murdoch, showcasing handling of various input formats. ```R # Duncan Murdoch's examples. The deldir() function was not # handling such examples correctly until Duncan kindly set # me on the right path. set.seed(123) dd6 <- deldir(rnorm(32),rnorm(32),rnorm(32)) # x <- cbind(x = 1:10, junk = 11:20) y <- 21:30 z <- 31:40 d7 <- deldir(x=x, y=y, z=z) # # print(d7$summary) reveals that x is 1:10, y is 21:30 # and z is 31:40; x[,"junk"] is ignored as it should be. x <- cbind(x = 1:10, "rnorm(10)" = 11:20) y <- 21:30 z <- 41:50 d8 <- deldir(x=x, y=y, z=rnorm(10)) # # print(d8$summary) reveals that x is 1:10, y is 21:30 and z is a # vector of standard normal values. Even though x has a column with # the name of the z argument i.e. "rnorm(10)" (!!!) the specified # value of z takes precedence over this column (and, as per the usual # R syntax) over the object named "z" in the global environment. ``` -------------------------------- ### Example of lawSummary usage Source: https://cran.r-project.org/web/packages/deldir/refman/deldir.html This example demonstrates how to use the `lawSummary` function to produce a Lewis-Aboav-Weaire summary of a tessellation. It includes plotting the different layers of tiles. ```R # A random pattern: set.seed(42) xy1 <- data.frame(x=runif(400,0,20),y=runif(400,0,20)) dxy1 <- deldir(xy1) ldxy1 <- lawSummary(dxy1) tl1 <- tile.list(dxy1) plot(0,0,type="n",xlim=c(-2,35),ylim=c(0,20),asp=1,xlab="x",ylab="y",bty="l") plot(tl1,showpoints=FALSE,add=TRUE) points(xy1[attr(ldxy1,"i1"),],pch=20,col="yellow") points(xy1[attr(ldxy1,"i2"),],pch=20,col="blue") points(xy1[attr(ldxy1,"i3"),],pch=20,col="green") points(xy1[attr(ldxy1,"i.kept"),],pch=20,col="red") legend("right",pch=20,col=c("yellow","blue","green","red"), legend=c("layer 1","layer 2","layer 3","interior")) ``` -------------------------------- ### Examples for tileArea() Source: https://cran.r-project.org/web/packages/deldir/refman/deldir.html Illustrates how to use the `tileArea()` function to calculate the area of a Dirichlet tile. It shows examples with a rectangular window and demonstrates calculating areas for multiple tiles in a list. ```R set.seed(42) x <- runif(20) y <- runif(20) z <- deldir(x,y,rw=c(0,1,0,1)) w <- tile.list(z) with(w[[1]],tileArea(x,y,rw=z$rw)) sapply(w,function(x,rw){tileArea(x$x,x$y,attr(w,"rw"))}) x <- c(0.613102,0.429294,0.386023,0.271880,0.387249,0.455900,0.486101) y <- c(0.531978,0.609665,0.597780,0.421738,0.270596,0.262953,0.271532) # The vertices of the Dirichlet tile for point 6. tileArea(x,y,rw=c(0,1,0,1)) tileArea(x,y,rw=c(-1,2,-3,4)) # Same as above. ``` -------------------------------- ### Examples Source: https://cran.r-project.org/web/packages/deldir/refman/deldir.html Examples of using the divchain and deldir functions. ```R set.seed(42) x <- runif(50) y <- runif(50) z <- factor(kmeans(cbind(x,y),centers=4)$cluster) dc1 <- divchain(x,y,z,rw=c(0,1,0,1)) dxy <- deldir(x,y,z=z,rw=c(0,1,0,1)) dc2 <- divchain(dxy) ``` -------------------------------- ### Example of deldir usage Source: https://cran.r-project.org/web/packages/deldir/refman/deldir.html This example demonstrates how to use the deldir function to compute Voronoi tessellations and handle duplicated points. ```R set.seed(42) xy <- data.frame(x=runif(20),y=runif(20)) # Lots of duplicated points. xy <- rbind(xy,xy[sample(1:20,20,TRUE),]) # Scramble. ii <- sample(1:40,40) x <- xy$x[ii] y <- xy$y[ii] # Unduplicate! iii <- !duplicatedxy(x,y) xu <- x[iii] yu <- y[iii] # The i-th tile is determined by (xu[i],yu[i]): dxy <- deldir(xu,yu) ``` -------------------------------- ### Loading the niProperties dataset Source: https://cran.r-project.org/web/packages/deldir/refman/deldir.html Example of how to load the 'niProperties' dataset, which contains locations of properties in Northern Ireland. ```R data("niProperties") ``` -------------------------------- ### Examples for which.tile Source: https://cran.r-project.org/web/packages/deldir/refman/deldir.html Illustrates finding the Voronoi tile containing a specific point. ```R set.seed(42) x <- runif(20,0,100) y <- runif(20,0,100) dxy <- deldir(x,y) txy <- tile.list(dxy) i <- which.tile(30,50,txy) # The value of i here is 14. plot(txy,showpoints=FALSE) text(x,y,labels=1:length(txy),col="red") points(30,50,pch=20,col="blue") ``` -------------------------------- ### Examples of using tileInfo Source: https://cran.r-project.org/web/packages/deldir/refman/deldir.html Demonstrates how to use the tileInfo function with and without clipping, and with the polyclip package for complex clipping regions. ```R set.seed(42) x <- runif(20) y <- runif(20) dxy <- deldir(x,y,rw=c(0,1,0,1)) ixy1 <- tileInfo(dxy) ixy2 <- tileInfo(dxy,bndry=TRUE) if(require(polyclip)) { CP <- list(x=c(0.49,0.35,0.15,0.20,0.35,0.42, 0.43,0.62,0.46,0.63,0.82,0.79), y=c(0.78,0.86,0.79,0.54,0.58,0.70, 0.51,0.46,0.31,0.20,0.37,0.54)) ixy3 <- tileInfo(dxy,clipp=CP) # 10 tiles are retained; the third tile, # corresponding to point 6, is # subdivided into two parts. } ``` -------------------------------- ### Basic deldir and plot example Source: https://cran.r-project.org/web/packages/deldir/refman/deldir.html Demonstrates the basic usage of the deldir function to compute a tessellation and then plot it. ```R x <- c(2.3,3.0,7.0,1.0,3.0,8.0) + 0.5 y <- c(2.3,3.0,2.0,5.0,8.0,9.0) + 0.5 x <- c(x,1,10,10,1) y <- c(y,1,1,10,10) dxy <- deldir(x,y,rw=c(0,11,0,11)) plot(dxy) ``` -------------------------------- ### Examples for seaweed dataset Source: https://cran.r-project.org/web/packages/deldir/refman/deldir.html Demonstrates how to use the 'seaweed' dataset with deldir and tileInfo functions. ```R # data(seaweed) # It is unnecessary to use \code{data} since \code{seaweed} is # a "first class object". It is "lazily loaded". dsw <- deldir(seaweed) isw <- tileInfo(dsw) # Expand the width of the terminal window. options(width=120) isw tsw <- tile.list(dsw) plot(tsw,labelPts=TRUE,col.lbls="red",cex=0.5,adj=0.5) ``` -------------------------------- ### Examples for triang.list Source: https://cran.r-project.org/web/packages/deldir/refman/deldir.html Demonstrates the usage of the triang.list function, including a case with no triangles. ```R set.seed(42) x <- runif(20) y <- runif(20) z <- sample(1:100,20) d1 <- deldir(x,y,z=z) t1 <- triang.list(d1) # A "triangulation" with no triangles! d2 <- deldir(x=1:10,y=11:20) plot(d2) t2 <- triang.list(d2) plot(t2,showrect=TRUE,rectcol="blue") # Pretty boring! ``` -------------------------------- ### Examples for tileInfo function Source: https://cran.r-project.org/web/packages/deldir/refman/deldir.html Demonstrates the usage of the tileInfo function with different arguments and scenarios, including clipping and boundary handling. ```R set.seed(179) x <- runif(100) y <- runif(100) dxy <- deldir(x,y,rw=c(0,1,0,1)) ixy1 <- tileInfo(dxy) print(ixy1) ixy2 <- tileInfo(dxy,bndry=TRUE) print(ixy2) if(require(polyclip)) { CP <- list(x=c(0.49,0.35,0.15,0.20,0.35,0.42, 0.43,0.62,0.46,0.63,0.82,0.79), y=c(0.78,0.86,0.79,0.54,0.58,0.70, 0.51,0.46,0.31,0.20,0.37,0.54)) ixy3 <- tileInfo(dxy,clipp=CP) options(width=120) # And enlarge the console window. print(ixy3) # 33 tiles are retained. print(ixy3$perimeters$perComps) # The tiles for points 9 and 94 have # been split into two components. } ``` -------------------------------- ### Plotting the niProperties dataset Source: https://cran.r-project.org/web/packages/deldir/refman/deldir.html Example of plotting the 'niProperties' dataset to visualize the property locations. ```R plot(niProperties) ``` -------------------------------- ### Demonstrate that neighbours can have discontiguous clipped tiles Source: https://cran.r-project.org/web/packages/deldir/refman/deldir.html This example demonstrates that neighbors can have discontiguous clipped tiles, using the polyclip package. ```R if(require(polyclip)) { x <- c(0.38,0.44,0.04,0.97,0.43,0.96,0.89,0.64,0.97,0.62,0.33,0.35, 0.40,0.78,0.04,0.75,0.68,0.17,0.26,0.51) y <- c(0.68,0.98,0.76,0.57,0.85,0.19,0.27,0.83,0.69,0.24,0.04,0.14, 0.22,0.48,0.20,0.72,0.01,0.38,0.51,0.00) CP <- list(x=c(0.72,0.93,0.76,0.61,-0.03,-0.04,0.41), y=c(0.46,0.76,0.94,1.03,1.01,0.37,0.31)) dxy <- deldir(x,y,rw=c(0,1,0,1)) TL <- tile.list(dxy) plot(TL,labelPts=TRUE) plot(TL[16],clipp=CP,fillcol="orange",labelPts=TRUE,add=TRUE) polygon(CP,border="red") nbrs <- getNbrs(dxy,interior=CP) # Tiles are clipped to CP. # Note that point 14 is a neighbour of point 16, even though their # clipped tiles do not meet. } ``` -------------------------------- ### Examples for tile.centroids function Source: https://cran.r-project.org/web/packages/deldir/refman/deldir.html Demonstrates the usage of the tile.centroids function to compute centroids of Dirichlet tiles. ```R set.seed(42) x <- runif(20) y <- runif(20) d <- deldir(x,y) l <- tile.list(d) g <- tile.centroids(l) plot(l,close=TRUE) points(g,pch=20,col="red") ``` -------------------------------- ### Artificial Example of Tags Source: https://cran.r-project.org/web/packages/deldir/refman/deldir.html Demonstrates the use of the 'z' argument for tags with different data structures. ```R # Artificial example of tags. set.seed(42) trees1 <- sample(c("spruce","birch","poplar","shoe"),20,TRUE) trees2 <- sample(c("fir","maple","larch","palm"),20,TRUE) egDat <- data.frame(x=runif(20),y=runif(20),species=trees1) tagNm <- "species" species <- trees2 dd1 <- deldir(egDat) # No tags. dd2 <- deldir(egDat,z=species) # Uses trees1 as the tags. dd3 <- deldir(egDat,z="species") # Same as dd2. dd4 <- deldir(egDat,z=tagNm) # Same as dd2 and dd3. spec <- species dd5 <- deldir(egDat,z=spec) # Uses trees2 as the tags. ``` -------------------------------- ### Examples for toyPattern Source: https://cran.r-project.org/web/packages/deldir/refman/deldir.html This snippet shows how to use the 'toyPattern' object with the deldir function, demonstrating its use with both factor and numeric tags. ```R dtoy <- deldir(toyPattern) # "Tags" are the marks of the pattern. set.seed(42) dtoy.nt <- deldir(toyPattern,z=round(runif(59),2)) # Tags are numeric. ``` -------------------------------- ### Examples for deldir with solute plume data Source: https://cran.r-project.org/web/packages/deldir/refman/deldir.html Shows how to use the deldir function with the volTriPoints dataset. ```R dvtp <- deldir(volTriPoints) plot(dvtp) ``` -------------------------------- ### Complicated example from He Huang Source: https://cran.r-project.org/web/packages/deldir/refman/deldir.html A complex example involving linguistic distances, data preparation, tessellation, and plotting with line widths proportional to distances. ```R # Complicated example from He Huang: # "Linguistic distances". vldm <- c(308.298557,592.555483,284.256926,141.421356,449.719913, 733.976839,591.141269,282.842712,1.414214,732.562625) ldm <- matrix(nrow=5,ncol=5) ldm[row(ldm) > col(ldm)] <- vldm ldm[row(ldm) <= col(ldm)] <- 0 ldm <- (ldm + t(ldm))/2 rownames(ldm) <- LETTERS[1:5] colnames(ldm) <- LETTERS[1:5] # Data to be triangulated. id <- c("A","B","C","D","E") x <- c(0.5,1,1,1.5,2) y <- c(5.5,3,7,6.5,5) dat_Huang <- data.frame(id = id, x = x, y = y) # Form the triangulation/tessellation. dH <- deldir(dat_Huang) # Plot the triangulation with line widths proportional # to "linguistic distances". all_col <- rainbow(100) i <- pmax(1,round(100*vldm/max(vldm))) use_col <- all_col[i] ind <- as.matrix(dH$delsgs[,c("ind1","ind2")]) lwv <- ldm[ind] lwv <- 10*lwv/max(lwv) plot(dH,wlines="triang",col=use_col,showpoints=FALSE, lw=lwv,asp=NA) with(dH,text(x,y,id,cex=1.5)) ``` -------------------------------- ### Example of getNbrs usage Source: https://cran.r-project.org/web/packages/deldir/refman/deldir.html This example demonstrates how to use the getNbrs function to list Delaunay neighbors and shows how to correctly access list elements by name or identifier. ```R set.seed(42) x <- runif(60) y <- runif(60) dxy <- deldir(x,y,rw=c(0,1,0,1)) nbrs <- getNbrs(dxy,interior=c(0.2,0.8,0.2,0.8)) nbrs[["14"]] # Correct. nbrs[[14]] # Incorrect. names(nbrs)[14] # The result is 42. # Thus nbrs[[14]] actually gives the Delaunay neighbours of point 42. ``` -------------------------------- ### Examples for deldir() Source: https://cran.r-project.org/web/packages/deldir/refman/deldir.html Demonstrates various uses of the `deldir()` function, including basic usage, specifying a rectangular window, and clipping tiles with a polygon. It also shows how to use `tile.list()` to extract tile information and how to identify tiles on the border of a clipping region. ```R set.seed(42) x <- runif(20) y <- runif(20) z <- deldir(x,y) w <- tile.list(z) z <- deldir(x,y,rw=c(0,1,0,1)) w <- tile.list(z) z <- deldir(x,y,rw=c(0,1,0,1),dpl=list(ndx=2,ndy=2)) w <- tile.list(z) if(require(polyclip)) { CP <- list(x=c(0.49,0.35,0.15,0.20,0.35,0.42, 0.43,0.62,0.46,0.63,0.82,0.79), y=c(0.78,0.86,0.79,0.54,0.58,0.70, 0.51,0.46,0.31,0.20,0.37,0.54)) wc <- tile.list(z,clipp=CP) # 10 tiles are retained; the third tile, # corresponding to point 6, is # subdivided into two parts. # Determine the tiles on the border of a clipping region. # Example due to Huimin Wang. set.seed(112) x <- runif(100) y <- runif(100) dxy <- deldir(x,y) txy <- tile.list(dxy) chind <- chull(x,y) bdry <- list(x=x[chind],y=y[chind]) ctxy <- tile.list(dxy,clipp=bdry) inside <- lapply(ctxy,function(tile,bdry){insidePoly(tile$x,tile$y,bdry)}, bdry=bdry) border <- sapply(inside,function(x){any(!x) | any(attr(x,"on.boundary"))}) plot(ctxy[border],main="Border tiles") } ``` -------------------------------- ### Artificial Example of 'id' Argument Source: https://cran.r-project.org/web/packages/deldir/refman/deldir.html Demonstrates the use of the 'id' argument for unique identification. ```R # Artificial example of the use of the "id" argument. set.seed(42) x <- runif(50) y <- runif(50) ll <- expand.grid(a=letters[1:10],b=letters[1:10]) aa <- sample(paste0(ll[["a"]],ll[["b"]]),50) dxy.wid <- deldir(x,y,id=aa) ``` -------------------------------- ### Example Usage of deldir and plot.deldir Source: https://cran.r-project.org/web/packages/deldir/refman/deldir.html Demonstrates how to use the deldir function to compute Voronoi tessellations and then plot them using plot.deldir with various options for coloring, clipping, and labeling. ```R set.seed(42) x <- runif(20) y <- runif(20) z <- deldir(x,y,rw=c(0,1,0,1)) w <- tile.list(z) plot(w) ccc <- heat.colors(20) # Or topo.colors(20), or terrain.colors(20) # or cm.colors(20), or rainbow(20). plot(w,fillcol=ccc,close=TRUE) if(require(polyclip)) { CP <- list(x=c(0.49,0.35,0.15,0.20,0.35,0.42, 0.43,0.62,0.46,0.63,0.82,0.79), y=c(0.78,0.86,0.79,0.54,0.58,0.70, 0.51,0.46,0.31,0.20,0.37,0.54)) cul <- rainbow(10)[c(1,7,3:6,2,8:10)] # Rearranging colours to improve # the contrast between contiguous tiles. plot(w,clipp=CP,showpoints=FALSE,fillcol=cul) } plot(w,labelPts=TRUE,col.lbls="red") plot(w,labelPts=TRUE,col.lbls="red",cex=0.5) plot(w,showpoints=TRUE,labelPts=TRUE,col.pts="green",col.lbls="red") plot(w,axes=FALSE,xlab="",ylab="",close=TRUE) ``` -------------------------------- ### Centroidal Voronoi (Dirichlet) tessellation. Source: https://cran.r-project.org/web/packages/deldir/refman/deldir.html Example usage of the cvt function for calculating centroidal Voronoi tessellations. ```R ## Not run: # Takes too long. set.seed(42) x <- runif(20) y <- runif(20) dxy <- deldir(x,y,rw=c(0,1,0,1)) cxy1 <- cvt(dxy,verb=TRUE) plot(cxy1$tiles) with(cxy1$centroids,points(x,y,pch=20,col="red")) cxy2 <- cvt(dxy,stopcrit="m",verb=TRUE) plot(cxy2$tiles) with(cxy2$centroids,points(x,y,pch=20,col="red")) # Visually indistinguishable from the cxy1 plot. # But ... all.equal(cxy1$centroids,cxy2$centroids) # Not quite. cxy3 <- cvt(dxy,stopcrit="m",verb=TRUE,maxit=250) all.equal(cxy1$centroids,cxy3$centroids) # Close, but no cigar. cxy4 <- cvt(dxy,verb=TRUE,tol=1e-14) cxy5 <- cvt(dxy,stopcrit="m",verb=TRUE,maxit=600) all.equal(cxy4$centroids,cxy5$centroids) # TRUE # Takes a lot of iterations or a really small tolerance # to get "good" convergence. But this is almost surely # of no practical importance. txy <- tile.list(dxy) cxy6 <- cvt(txy) all.equal(cxy6$centroids,cxy1$centroids) # TRUE ## End(Not run) ``` -------------------------------- ### Example Usage of deldir Source: https://cran.r-project.org/web/packages/deldir/refman/deldir.html This code snippet demonstrates how to use the deldir function to create a Delaunay triangulation and Voronoi tessellation, and then visualize the results with different layers and points highlighted. ```R set.seed(115) x <- c(rep(1:20,10),rep((1:20)+0.5,10)) y <- c(rep(1:10,each=20),rep((1:10)+0.5,each=20))*sqrt(3) a <- runif(400,0,2*pi) b <- runif(400,-1,1) x <- x+0.1*cos(a)*b y <- y+0.1*sin(a)*b xy2 <- data.frame(x,y) dxy2 <- deldir(xy2) ldxy2 <- lawSummary(dxy2) tl2 <- tile.list(dxy2) plot(0,0,type="n",xlim=c(-2,35),ylim=c(0,20),asp=1,xlab="x",ylab="y",bty="l") plot(tl2,showpoints=FALSE,add=TRUE) points(xy2[attr(ldxy2,"i1"),],pch=20,col="yellow") points(xy2[attr(ldxy2,"i2"),],pch=20,col="blue") points(xy2[attr(ldxy2,"i3"),],pch=20,col="green") points(xy2[attr(ldxy2,"i.kept"),],pch=20,col="red") legend("right",pch=20,col=c("yellow","blue","green","red"), legend=c("layer 1","layer 2","layer 3","interior")) ```