# eulerr under the hood In eulerr: Area-Proportional Euler and Venn Diagrams with Ellipses

knitr::opts_chunk$set( echo = FALSE, warning = FALSE, message = FALSE, fig.height = 3, fig.width = 3, collapse = TRUE, comment = "#>" ) library(grid) library(RConics) library(eulerr) library(lattice) pal <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7") ellipse_arc <- function(saxes = c(1, 1), loc = c(0, 0), theta = 0, n = 200, rng = c(0, 2*pi)) { b <- min(saxes[1], saxes[2]) a <- max(saxes[1], saxes[2]) d2 <- (a - b) * (a + b) if (length(rng) == 1) phi <- rng - theta else phi <- seq(rng[1], rng[2], len = n) - theta sp <- sin(phi) cp <- cos(phi) r <- a * b/sqrt((saxes[2] * cp)^2 + (saxes[1] * sp)^2) P <- matrix(nrow = n, ncol = 2) P[, 1] <- r * cp P[, 2] <- r * sp if (theta != 0) { P <- P %*% matrix(c(cos(theta), sin(theta), -sin(theta), cos(theta)), byrow = TRUE, nrow = 2, ncol = 2) } P <- P + matrix(loc[1:2], nrow = nrow(P), ncol = 2, byrow = TRUE) P } set.seed(1)  ## Introduction eulerr relies on an extensive machinery to turn user input into a pretty Euler diagram. Little of this requires any tinkering from the user. To make that happen, however, eulerr needs to make several well-formed decisions about the design of the diagram on behalf of the user, which is not a trivial task. This document outlines the implementation of eulerr from input to output. It is designed to be an evolving documentation on the innards of the program. ## Input Euler diagrams present relationships between sets, wherefore the data must describe these relationships, either directly or indirectly. eulerr allows several alternatives for this data, namely, • intersections and relative complements $$A \setminus B = 3 \quad B \setminus A = 2 \quad A \cap B=1,$$ • unions and identities $$A=4 \quad B=3 \quad A \cap B=1,$$ • a matrix of binary (or boolean) indices, $$\begin{bmatrix} \mathbf{A} & \mathbf{B}\ 1 & 0 \ 1 & 0 \ 1 & 0 \ 1 & 1 \ 0 & 1 \ 0 & 1, \end{bmatrix}$$ • a list of sample spaces $$\begin{array}{l} A = {a,\,b,\,c,\,d}\ B = {a,\,e,\,f,} \end{array},$$ • or a two- or three-way table $AA^\mathsf{c}$ ---------------- --- ----------------$B$1 2$A^\mathsf{c}$3 0 As an additional feature for the matrix form, the user may supply a factor variable with which to split the data set before fitting the diagram, which sometimes improves diagrams where the set relationships vary across categories. Whichever type of input is provided, eulerr will translate it to the first and second types, intersections and relative complements and unions and identities, which will be used in the steps to come. The Euler diagram is then fit in two steps: first, an initial layout is formed with circles using only the sets' pairwise relationships. Second, this layout is fine-tuned taking all$2^N-1$intersections into consideration. ## Initial layout For our initial layout, we adopt a constrained version of multi-dimensional scaling (MDS) that has been adapted from venn.js [@Frederickson_2016], which in turn is a modification of an algorithm used in venneuler [@wilkinson_exact_2012]. In it, we consider only the pairwise intersections between sets, attempting to position their respective shapes so as to minimize the difference between the separation between their centers required to obtain an optimal overlap and the actual overlap of the shapes in the diagram. This problem is unfortunately intractable for ellipses, being that there is an infinite number of ways by which we can position two ellipses to obtain a given overlap. Thus, we restrict ourselves to circles in our initial layout, for which we can use the circle--circle overlap formula to numerically find the required distance,$d, for each pairwise relationship. \begin{aligned} O_{ij} = & r_i^2\arccos\left(\frac{d_{ij}^2 + r_i^2 - r_j^2}{2d_{ij}r_i}\right) + r_j^2\arccos\left(\frac{d_{ij}^2 + r_j^2 - r_i^2}{2d_{ij}r_j}\right) -\ &\quad \frac{1}{2}\sqrt{(-d_{ij} + r_i + r_j)(d_{ij} + r_i - r_j)(d_{ij} - r_i + r_j)(d_{ij} + r_i + r_j)}, \end{aligned} wherer_i$and$r_j$are the radii of the circles representing the$i$^th^ and$j$^th^ sets respectively,$O_{ij}$their overlap, and$d_{ij}$their separation. r$), radii ($r_i,r_j$), and area of overlap ($O_{ij}$)."} c0 <- ellipseToConicMatrix(c(1, 1), c(0, 0), 0) c1 <- ellipseToConicMatrix(c(0.7, 0.7), c(1.2, 0), 0) pp <- intersectConicConic(c0, c1)

theta0 <- atan2(pp[2, 1], pp[1, 1]) theta1 <- atan2(pp[2, 2], pp[1, 2])

phi0 <- atan2(pp[2, 1], pp[1, 1] - 1.2) phi1 <- atan2(pp[2, 2], pp[1, 2] - 1.2)

seg <- rbind(ellipse_arc(c(0.7, 0.7), c(1.2, 0), 0, n = 50, c(-pi, phi0)), ellipse_arc(c(1, 1), c(0, 0), 0, n = 100, c(theta0, theta1)), ellipse_arc(c(0.7, 0.7), c(1.2, 0), 0, n = 50, c(phi1, pi)))

ospot <- c(mean(seg[, 1]), mean(seg[, 2]))

xyplot(1~1, xlim = c(-1.1, 2), ylim = c(-1.5, 1.1), asp = "iso", xlab = NULL, ylab = NULL, scales = list(draw = FALSE), par.settings = list(axis.line = list(col = "transparent")), panel = function() { panel.polygon(seg, col = "steelblue1", alpha = 0.5, border = "transparent") grid::grid.circle(0, 0, r = 1, default.units = "native", gp = gpar(fill = "transparent")) grid::grid.circle(1.2, 0, r = 0.7, default.units = "native", gp = gpar(fill = "transparent")) pBrackets::grid.brackets(1.2, -1.2, 0, -1.2, h = 0.05) pBrackets::grid.brackets(-1, 0, 0, 0, h = 0.05) pBrackets::grid.brackets(1.2, 0, 1.9, 0, h = 0.05) panel.lines(c(0, 0), c(0, -1.2), lty = 2, col = "grey65") panel.lines(c(1.2, 1.2), c(0, -1.2), lty = 2, col = "grey65") panel.text(0.6, unit(-1.3, "native"), labels = expression(italic(d[ij])), pos = 1) panel.text(ospot[1], ospot[2], labels = expression(italic(O[ij]))) panel.text(-0.5, 0.1, labels = expression(italic(r[i])), pos = 3, default.units = "native") panel.text(1.55, 0.1, labels = expression(italic(r[j])), pos = 3, default.units = "native") panel.points(c(0, 1.2), c(0, 0), pch = 21, col = 1, cex = 1, fill = "white") })

Setting $r_i = \sqrt{F_i/\pi}$, where $F_i$ is
the size of the $i$^th^ set, we are able to obtain $d$ numerically using
the squared difference between $O$ and the desired overlap
as loss function,

$$\mathcal{L}(d_{ij}) = \left(O_{ij} - (F_i \cap F_j) \right)^2, \quad \text{for } i < j \leq n,$$

which we optimize using optimize()^[According to the documentation,
optimize() consists of a "combination of golden section search and successive
parabolic interpolation." from **stats**.]

For a two-set combination, this is all we need to plot an exact diagram, given
that we now have the two circles' radii and separation and may place the circles
arbitrarily as long as their separation, $d$, remains the same. This is not,
however, the case with more than two sets.

With three or more sets, the circles need to be arranged so that they interfere
minimally with one another. In some cases, the set configuration allows this to
be accomplished flawlessly, but often, compromises must me made. As is often the
case in this context, this turns out to be another optimization problem. It can
be tackled in many ways; **eulerr**'s approach is based on a method developed by
@Frederickson_2015, which the author describes as constrained multi-dimensional
scaling.

The algorithm tries to position the circles so that the separation between each
pair of circles matches the separation required from the separation equation.
If the two sets are disjoint, however, the algorithm is indifferent to the
relative locations of those circles as long as they do not intersect. The
equivalent applies to subset sets: as long as the circle representing the
smaller set remains within the larger circle, their locations are free to vary.
In all other cases, the loss function is the residual sums of squares of the
optimal separation of circles, $d$, that we found in the overlap equation, and
the actual distance in the layout we are currently exploring.

$$\mathcal{L}(h,k) = \displaystyle \ell{\sum_{1\leq i<j\leq N}} \begin{cases} 0 & F_i \cap F_j = \emptyset \text{ and } O_{ij} = 0\\ 0 & (F_i \subseteq F_j \text{ or } F_i \supseteq F_j) \text{ and } O_{ij}=0\\ \left(\left(h_i-h_j\right)^2+\left(k_i-k_j\right)^2-d_{ij}^2\right)^2 & \text{otherwise} \\ \end{cases}.$$

The analytical gradient is retrieved as usual by taking the derivative of the
loss function,

$$\vec{\nabla} f(h_i) = \sum_{j=1}^N \begin{cases} \vec{0} & F_i \cap F_j = \emptyset \text{ and } O_{ij} = 0\\ \vec{0} & (F_i \subseteq F_j \text{ or } F_i \supseteq F_j) \text{ and } O_{ij}=0\\ 4\left(h_i-h_j\right)\left(\left(h_i-h_j\right)^2+\left(k_i-k_j\right)^2-d_{ij}^2\right) & \text{otherwise}, \\ \end{cases}$$

where $\vec{\nabla} f(k_i)$ is found as in the gradient with $h_i$ swapped for
$k_i$ (and vice versa).

The Hessian for our loss function is given next. However, because the current
release of R suffers from a bug^[The current development version of R features a
fix for this bug; **eulerr** will be updated to use the analytical Hessian as
soon as it is introduced in a stable version of R.] causing the analytical
Hessian to be updated improperly, the current release of **eulerr** instead
relies on the numerical approximation of the Hessian offered by the optimizer.

$$\small \mathbf{H}(h,k) = \ell{\sum_{1\leq i<j\leq N}} \begin{bmatrix} 4\left(\left(h_i-h_j\right)^2+\left(k_i-k_j\right)^2-d_{ij}^2\right)+8\left(h_i-h_j\right)^2 & \cdots & 8\left(h_i-h_j\right)\left(k_i-k_j\right)\\ \vdots & \ddots & \vdots \\ 8\left(k_i-k_j\right)\left(h_i-h_j\right) & \cdots & 4\left(\left(h_i-h_j\right)^2+\left(k_i-k_j\right)^2-d_{ij}^2\right)+8\left(k_i-k_j\right)^2 \end{bmatrix}.$$

Note that the constraints given in loss and gradients still apply to each
element of the Hessian and have been omitted for convenience only.

We optimize the loss function using the nonlinear optimizer nlm() from the R
core package **stats**. The underlying code for nlm was written by
@Schnabel_1985. It was ported to R by Saikat DebRoy and the R Core team
[@RCT_2017] from a previous FORTRAN to C translation by Richard H. Jones.
nlm() consists of a system of Newton-type algorithms and performs well for
difficult problems [@Nash_2014].

The initial layout outlined above will sometimes turn up perfect diagrams, but
only reliably so when the diagram is completely determined by its pairwise
intersections. More pertinently, we have not yet considered the higher-order
intersections in our algorithm and neither have we approached the problem of
using ellipses---as we set out to do.

## Final layout

We now need to account for all the sets' intersections and, consequently, all
the overlaps in the diagram. The goal is to map each area uniquely to a subset
of the data from the input and for this purpose we will use the sets'
intersections and the relative complements of these intersections, for which we
will use the shorthand $\omega$. We introduced this form in the *input* section,
but now define it rigorously.

For a family of *N* sets, $F = F_1, F_2, \dots, F_N$, and their $n=2^N-1$
intersections, we define $\omega$ as the intersections of these sets and their
relative complements, such that

\begin{aligned} \omega_{1} & = F_1 \setminus \bigcap_{i=2}^N F_i \\ \omega_{2} & = \bigcap_{i=1}^2 F_i \setminus \bigcap_{i=3}^{N} F_i\\ \vdots & = \vdots \\ \omega_n & = \bigcap_{i=1}^{N}F_i \end{aligned}

with

$$\sum_{i = 1}^n \omega_i = \bigcup_{j=1}^N F_i.$$

Analogously to $\omega$, we also introduce the $\&$-operator, such that

$$F_i \& F_j = (F_i \cap F_j)\setminus (F_i \cap F_j)^\textsf{c}.$$

The fitted diagram's area-equivalents for $\omega$ will be defined as $A$, so
that an exact diagram requires that $\omega_i = A_i$ for $i=1,2,\dots,2^N-1$,
where $N$ is the number of sets in the input.

In our initial configuration, we restricted ourselves to circles but now extend
ourselves also to ellipses. From now on, we abandon the practice of treating
circles separately---they are only a special case of ellipses, and, hence,
everything that applies to an ellipse does so equally for a circle.

### Intersecting ellipses

We now need the ellipses' points of intersections. **eulerr**'s approach to this
is outlined in [@Richter-Gebert_2011] and based in *projective*, as opposed to
*Euclidean*, geometry.

To collect all the intersection points, we naturally need only to consider two
ellipses at a time. The canonical form of an ellipse is given by

$$\frac{\left[ (x-h)\cos{\phi}+(y-k)\sin{\phi} \right]^2}{a^2}+ \frac{\left[(x-h) \sin{\phi}-(y-k) \cos{\phi}\right]^2}{b^2} = 1,$$

where $\phi$ is the counter-clockwise angle from the positive x-axis to the
semi-major axis $a$, $b$ is the semi-minor axis, and $h, k$ are the x- and
y-coordinates, respectively, of ellipse's center.

r
h <- 0.7
k <- 0.5
a <- 1
b <- 0.6
phi <- pi/5
n <- 200
ellipse <- RConics::ellipse(c(a, b), c(h, k), phi,  n = n)

xyplot(1 ~ 1, xlim = c(-0.3, 1.7), ylim = c(-0.3, 1.3), asp = "iso",
xlab = NULL, ylab = NULL,
par.settings = list(axis.line = list(col = "transparent")),
scales = list(draw = FALSE),
panel = function(x, y, ...) {
# grid
panel.refline(h = 0)
panel.refline(v = 0)

# rotation
arc <- ellipse_arc(c(0.2, 0.2), c(h, k), rng = c(0, phi))
theta <- ellipse_arc(c(a, b), c(h, k), phi, n = 1)

panel.lines(x = c(h, ellipse[1, 1]), y = c(k, ellipse[1, 2]), lty = 2,
col = "grey50")
panel.lines(x = c(h, theta[1, 1]), y = c(k, theta[1, 2]), lty = 2,
col = "grey50")
panel.lines(arc, col = "grey50")
panel.text(x = arc[nrow(arc)/2, 1] + 0.06, y = arc[nrow(arc)/2, 2] + 0.02,
labels = expression(italic(phi)))

# semiaxes
pBrackets::grid.brackets(h, k, ellipse[n/2, 1], ellipse[n/2, 2],
col = "grey50")
pBrackets::grid.brackets(h, k, ellipse[n/4, 1], ellipse[n/4, 2],
h = 0.04, col = "grey50")

panel.text(0.37, 0.12, labels = "a", font = 3)
panel.text(0.43, 0.68, labels = "b", font = 3)

# center
panel.text(x = h, y = k + 0.025, pos = 3, label = c("h,k"), font = 3)
panel.points(x = h, y = k, pch = 21, fill = "white", col = "black")

# ellipse
panel.lines(ellipse, col = 1)
})


However, because an ellipse is a conic^[The circle, parabola, and hyperbola are the other types of conics.] it can be represented in quadric form,

$$Ax^2 + Bxy + Cy^2 + Dx + Ey + F = 0$$

that in turn can be represented as a matrix,

$$\begin{bmatrix} A & B/2 & D/2 \ B/2 & C & E/2 \ D/2 & E/2 & F \end{bmatrix},$$

which is the form we need to intersect our ellipses. We now proceed to form three degenerate conics from a linear combination of the two ellipses we wish to intersect, split one of these degenerate conics into two lines, and intersect one of the ellipses with these lines, yielding 0 to 4 intersection points points.

C1 <- ellipseToConicMatrix(c(8, 2), c(0, 0), -pi/3)
C2 <- ellipseToConicMatrix(c(5, 2), c(1, -2), pi/5)

ellipses <- data.frame(rbind(ellipse(c(8, 2), c(0, 0), -pi/3),
ellipse(c(5, 2), c(1, -2), pi/5)))
colnames(ellipses) <- c("x", "y")

## Try the eulerr package in your browser

Any scripts or data that you put into this service are public.

eulerr documentation built on Sept. 6, 2021, 5:09 p.m.