-2

Given the source data for a Venn diagram, e.g. A=10, B=15, C=12, A+B=5, B+C=3, A+C=2, A+B+C=1, I need to draw a Venn diagram with the circle sizes proportional to A,B, and C, and their overlap proportional to the A+B, B+C, and A+C. The graph does not need to be perfectly matching the data, but be as close to it as possible (I prefer simpler computation method). It must correctly represent cases of no overlap and when one set is a proper subset of another set. How would I compute the correct positioning and radius of the circles for a given canvas size (width/height). I was able to find mathematics for two-circle Venn. Has anyone done the 3 circle calculation?

P.S. The example numbers above were random, and might be invalid.

Yuri Astrakhan
  • 8,808
  • 6
  • 63
  • 97
  • 1
    Your question is not clear. Do you mean you want the *areas* of the various regions of the Venn diagram to be proportional to the values given? Also, you may want to look [here](https://stackoverflow.com/questions/19841535/python-matplotlib-venn-diagram) for a similar question limited to Python. Finally, if I recall correctly, it is usually not possible to get the areas of a 3-circle Venn diagram to correspond to given values--there are not enough degrees of freedom in the diagram. – Rory Daulton Jan 05 '19 at 18:16
  • @RoryDaulton yes, i want the area of the regions to be proportional to the values, or at least as close to it as possible, including the cases of zero overlap and when one is a full subset of another. – Yuri Astrakhan Jan 05 '19 at 18:23
  • P.S. I updated the question, please remove -1 if it is no longer unclear. – Yuri Astrakhan Jan 05 '19 at 18:27
  • 1
    Your question is indeed more clear now. But the requirement "be as close to it as possible" is still vague. Please make that more precise. For example, you could require that the inner values be exact (the intersections or 2 or three sets) but the other values could be off. Also, the question as it stands seems to be more about mathematics than about practical computer programming. Could you explain how this is a programming problem? And have you tried any code of your own? – Rory Daulton Jan 05 '19 at 18:46
  • This is actually a very much a programming question - I am trying to implement it using Vega-js language, so I need just the algorithm - the coding i can do on my own. Close as possible is ambiguous on purpose - I do not know which calculation method is simpler, and what restrictions there are, so any answer will really work. – Yuri Astrakhan Jan 05 '19 at 19:12
  • The line between an algorithm and math is not very precise. Here, I need an algorithm - convertible to a program, not an abstract math question. – Yuri Astrakhan Jan 05 '19 at 20:01

2 Answers2

1

The distance between circles A, B must be the solution to the two-circle Venn problem with intersection area equal to (A+B) + (A+B+C) (or simply (A+B) if by your definition it includes (A+B+C)). Similarly for B, C the intersection is (B+C) + (A+B+C), and likewise for C, A.

Solve these independently with the algorithm you have found, and you get three distances equal to the side lengths of the triangle joining the centers of the three circles. Constructing the triangle and hence drawing the circles is then an easy task with some high-school trigonometry.

The solution is unique, and only valid if the intersection values themselves are valid.

meowgoesthedog
  • 14,670
  • 4
  • 27
  • 40
1

There are 8 regions defined by a 3-circle Venn diagram. If we define set A as including binary numbers 0 though 7 that have the 1-bit set, B as those with the 2-bit set, and C ans those with the 4-bit set, we get

A = {1, 3, 5, 7}; B = {2, 3, 6, 7}; C = {4, 5, 6, 7}

Each one of those numbers defined a region in the diagram, with 0 representing the region outside the circles and inside the universal set: i.e. A' ∩ B' ∩ C'.

You know how to do the 2-circle problem. So solve that for A and B (using the sizes of A, B, and A ∩ B), B and C, and A and C. that gives you distances between the circle centers and the sizes of the circle. Use the three distances to draw a triangle for those circle centers, then draw the circles around those centers. If that makes the exterior region 0have the wrong size, you can shrink or expand the entire 3-circle setup to get that right as well.

That makes all regions correct--except for region 7, the intersection of all three sets. That size will be set from all the others--you have no choice here. Therefore, it will probably not have the size you desire. You will need to experiment to see if the size of that region is usually close enough to what you want. My brief research implies that there is no way to use circles in your diagram and always get the sizes of all eight regions. If you use ellipses or some other more-general shape instead, this should be possible, but you seem to want circles.

Note that if you solve the 2-circle problem correctly, the situation of disjoint circles and of subset circles will automatically be handled. For example, if A and B are disjoint, then regions 3 and 7 are empty, and your solution will make the two circles to not overlap. They will probably touch, if you use the obvious algorithm from your linked site, but without overlap. Similarly, if one set is a subset of another, one circle will be inside the other, though they will probably touch. If you do not want the touching, the algorithm to avoid that should be easy, unless of course you have the situation where two of your three sets are equal.

Rory Daulton
  • 21,934
  • 6
  • 42
  • 50
  • sigh, turns out the formulas in the paper i linked to has no closed form solution, so now i have to figure out an approximation without using numerical method. That sounds like a math problem, so i posted it on https://math.stackexchange.com/questions/3063264/possible-closed-form-approximation-of-a-trigonometrical-expression – Yuri Astrakhan Jan 05 '19 at 23:08
  • @Yurik: Why don't you use a numeric method? The formula for the area of the overlap of two circles of given size is a straightforward, differentiable function of the distance between the centers of the circles. The Newton-Raphson method would work well, especially if it is modified to handle the singular points where the circles become disjoint or completely overlapping. Only a few iterations would be needed--how many depends on the final precision you require. Do you need help with that algorithm? That would be a very appropriate question for this site, I would think. – Rory Daulton Jan 05 '19 at 23:16
  • @Yurik: I should add that the function distance-to-overlap-area is nearly linear (see the green curve in the right graph in your linked we page), so Newton-Raphson would work very quickly. But if your needed precision is low, bisection would be easier and should work quickly enough. – Rory Daulton Jan 05 '19 at 23:21
  • I cannot use any kind of a looping in Vega-js (visualization grammar) - in a way it is similar to Excel - it is a declarative language, so I have to simplify it to a few formulas. Do you think it would be possible to do it with just a few cells in excel? – Yuri Astrakhan Jan 06 '19 at 00:18
  • 1
    @Yes, it could be done with complicated formulas in a few cells in Excel. Each cell could do one iteration of Newton-Raphson, and we do just 3 or so iterations. If I have time tomorrow I can try to set up those Excel formulas. – Rory Daulton Jan 06 '19 at 00:23
  • Hi Rory, please let me know if you figure out a way to do it in a few (e.g. under 10) "excel lines". Thanks!!! – Yuri Astrakhan Jan 07 '19 at 17:47