### Spacing circles in a 2-item Venn Diagram

Venn diagrams display the overlap of two set. One can adjust the size of the circles and their overlap so the areas of the circles and the overlap are proportional to the three values. The radii of the circles can be adjusted so their area is proportionate to their respective values. The placement of the circles can be adjusted so their area of overlap equals the value of the overlap. I show how to do this calculation.

Define X to be the size of the first set, Y to the size of the second set and Z to be the intersection of X and Y. I will perform the calculations in terms of the original units and then rescale all the parameters so they can be displayed.

We determine the radii for the two sets, r_{1 }and r_{2} , as the area of

X = π r_{1}^{2}

and the area of

Y = π r_{2}^{2}.

If Z = 0 ,then there is no overlap and the sets can be displayed separately.

If Z = X ,then X is contained within Y and similarly if Z = Y, then Y is contained with X.

In all other cases, the size of the overlap is determined by the distance between the centers of the circles. Assuming r_{1} > r_{2}, the circles can be separated by distances between r_{1} – r_{2} and r_{1} + r_{2}.

For any distance in the range, the overlap shown in green below is made up of two sections, one bordered by an arc centered at X and the other bordered by an arc centered at Y. The overlap area is the sum of these two sections.

My previous post on the geometry of a colorful Venn plot, showed how to calculate the angles θ_{1} and θ_{2} and distances d_{1} and d_{2} for circles separated by a given distance. These parameters will be used in the following discussion.

The lined section of in the graph is the contribution of the arc centered at C_{1}. It is the difference between the area bounding the cone extending from P_{1} to P_{2} minus the triangle bounded by C_{1}, P_{1} and P_{2}. The angle of the arc is 2 * θ_{1} where θ_{1} is expressed in radians.

The arc cone area = π r_{1}^{2} * 2 θ_{1}/ 2 π = r_{1}^{2} * θ_{1}

The area of the triangle is twice the area of the two right triangles sharing point P_{0}

2 * r_{1 }cos(θ_{1}) * r_{1} sin(θ_{1}) / 2 = r_{1}^{2 }sin(2 θ_{1}) / 2

The area of the lined area is the difference of these two areas and is therefore

r_{1}^{2} * (θ_{1} – sin(2 θ_{1}) / 2 )

The calculation is repeated for the arc centered at C_{2}, so the entire overlap area in green is

r_{1}^{2} * (θ_{1} – sin(2 θ_{1}) / 2 ) + r_{2}^{2} * (θ_{2} – sin(2 θ_{2}) / 2)

The following graphs shows how the overlap area decreases as the distance between the circles increases and how the angles θ_{1 }and θ_{1} change with distance.

In the right graph, as the distance between the circles increases, the overlap area decreases with most of the contribution at all distances coming from the smaller yellow circle. If the target overlap was 1.7, then the distance between the circles should be set at 0.8. At the smallest distance of r_{1} – r_{2} the entire yellow circle is in the overlap as indicated by θ_{2} equaling 180 degrees. As the distance increases, θ_{2} decreases to the point where there is no overlap, so the arc collapses to zero. By contrast, θ_{1} for the red circle is zero at the farthest distance since it’s arc makes no contribution to the overlap area at that point, it increases to a maximum when the angle subtends the radius of the yellow and then recedes to zero as the distance moves to r_{1} + r_{2}.

Unfortunately, there is no closed form solution to this problem of matching the distance to the target overlap. Fortunately, the difference between the overlap and the target size of the green area is a strictly monotonic decreasing function of the distance and can be solved using simple numerical methods.

The two best fitting angles, θ_{1} and θ_{2}, can also be used to calculate the two component distances making up the total distance between the circles:

d_{1} = r_{1} cos(θ_{1}) , d_{2} = r_{2} cos(θ_{2})

and the distance between the circles is

d = d_{1} + d_{2}.

In summary, if the size of sets X and Y are known as well as their intersection, a suitable positioning of two circles shows the size and overlap of the sets. Finally, the sizes of the circles are rescaled so that they can be plotted within the limits of the plot.

The routine to calculate the spacing is part of the colorfulVennPlot package in R:

http://cran.r-project.org/web/packages/colorfulVennPlot/index.html