Voronoi diagrams in R with deldir

Package

deldir

Author

Rolf Turner

Voronoi diagram with deldir

A Voronoi diagram (or Thiessen polygons) can be created in base R with the functions provided by deldir package. Given two vectors you will need to calculate the Delaunay triangulation with the deldir function and the Dirichlet (Voronoi) tessellation with the tile.list function. Then, you can plot the resulting object with the plot.tile.list S3 method.

# install.packages("deldir")
library(deldir)

# Data
set.seed(1)
x <- runif(50)
y <- runif(50)

# Calculate Voronoi Tesselation and tiles
tesselation <- deldir(x, y)
tiles <- tile.list(tesselation)

plot(tiles, pch = 19)

Voronoi diagram in R with deldir

If you want you can add numbers instead of the default points, setting number = TRUE.

# install.packages("deldir")
library(deldir)

# Data
set.seed(1)
x <- runif(50)
y <- runif(50)

# Calculate Voronoi Tesselation and tiles
tesselation <- deldir(x, y)
tiles <- tile.list(tesselation)

plot(tiles, pch = 19,
     number = TRUE)

Voronoi diagram with numbers in R

In addition, it is possible to remove the points setting the argument showpoints to FALSE.

# install.packages("deldir")
library(deldir)

# Data
set.seed(1)
x <- runif(50)
y <- runif(50)

# Calculate Voronoi Tesselation and tiles
tesselation <- deldir(x, y)
tiles <- tile.list(tesselation)

plot(tiles,
     showpoints = FALSE)

deldir package Voronoi without points

The edges of the tiles can be closed with a rectangle setting close = TRUE.

# install.packages("deldir")
library(deldir)

# Data
set.seed(1)
x <- runif(50)
y <- runif(50)

# Calculate Voronoi Tesselation and tiles
tesselation <- deldir(x, y)
tiles <- tile.list(tesselation)

plot(tiles,
     close = TRUE)

Closed Voronoi plot in R

Fill and border colors

You can pass a color palette to the fillcol argument of the plotting function to add colors to the tiles. Note that you can also change the color or the points with col.pts or the color of the numbers with col.num. The latter in case number = TRUE.

Voronoi tessellation colors in R

# install.packages("deldir")
library(deldir)

# Data
set.seed(1)
x <- runif(50)
y <- runif(50)

# Calculate Voronoi Tesselation and tiles
tesselation <- deldir(x, y)
tiles <- tile.list(tesselation)

plot(tiles, pch = 19,
     fillcol = hcl.colors(50, "Purple-Yellow"))

The border argument allows changing the color of the border of the tiles, as shown in the example below.

Voronoi tile border color in R

# install.packages("deldir")
library(deldir)

# Data
set.seed(1)
x <- runif(50)
y <- runif(50)

# Calculate Voronoi Tesselation and tiles
tesselation <- deldir(x, y)
tiles <- tile.list(tesselation)

plot(tiles, pch = 19,
     border = "white",
     fillcol = hcl.colors(50, "Sunset"))

Clip shape

The function also provides a clipping method with clipp. The argument needs a list with the components x and y giving the coordinates of the polygon to which the tessellation should be clipped. In the following example we are clipping it to a circle.

# install.packages("deldir")
library(deldir)

# Data
set.seed(1)
x <- runif(50)
y <- runif(50)

# Calculate Voronoi Tesselation and tiles
tesselation <- deldir(x, y)
tiles <- tile.list(tesselation)

# Circle
s <- seq(0, 2 * pi, length.out = 3000)
circle <- list(x = 0.5 * (1 + cos(s)),
               y = 0.5 * (1 + sin(s)))

plot(tiles, pch = 19,
     col.pts = "white",
     border = "white",
     fillcol = hcl.colors(50, "viridis"),
     clipp = circle)

Circular Voronoi clipping in R with deldir

ggplot2

Elegant Graphics for Data Analysis

Buy on Amazon
Better Data Visualizations

A Guide for Scholars, Researchers, and Wonks

Buy on Amazon
Storytelling with Data

A Data Visualization Guide for Business Professionals

Buy on Amazon
Data Sketches

A journey of imagination, exploration, and beautiful data visualizations

Buy on Amazon

See also