How Can I Generate A Venn Diagram In R For A Given Csv File
Entering edit mode
8.5 years ago
Payal ▴ 40


AIM is to check common names between A and B or A and multiple variables

I have a CSV file which looks like this...

Names                    A             B           C          D   Sum
NPAS4                    1             1           1                3
FAM120C                  1             1                            2
ROBO4                                              1                1
PRODH                    1             1           1                3
AQP4                                               1          1     2

Now what i want to create a 3 Venn diagrams

  1. Venn diagram to see common between all 4 possible attributes E.g. (A, B, C, D)
  2. Venn diagram to see common between all 3 attributes E.g. [(A,B,C), (B,C,D).......]
  3. Venn diagram to see common between all 2 attributes E.g. [(A,B), (B,C).......]

The code written with the help of the website (

>hsb2 <- read.csv("my.csv")
>hw <- (A = 1)
>hm <- (B = 1)
>hr <- (C = 1)
>c3 <- cbind(hw, hm, hr)
>a <- vennCounts(c3)

output looks like this..

      hw hm hr Counts
[1,]  0  0  0      0
[2,]  0  0  1      0
[3,]  0  1  0      0
[4,]  0  1  1      0
[5,]  1  0  0      0
[6,]  1  0  1      0
[7,]  1  1  0      0
[8,]  1  1  1      1
[1] "VennCounts

Why its not giving the actual count like in the csv file (sum)?

Please Help!

r statistics • 20k views
Entering edit mode

Dear Payal, If you want venn diagram of any kind the '1' s and '0' might be confusing to create them. instead if you can convert your csv file such that '1' s replaced with respective id e.g NPAS4, FAM120C etc. and delete all '0' s; I can help you get what you want.

Entering edit mode
8.5 years ago

Because it's not always possible to use a Venn diagram (a circular one that could be made in R) to show overlaps between three or four sets, I'll suggest something a little different.

I came up with something I call an "Eulergrid" which shows a bar graph, where each bar is an element in the power set of intersected sets, and a grid of overlap cases underneath (e.g., for three sets: A, B, C, A ∩ B, B ∩ C, A ∩ C, A ∩ B ∩ C).

The bar graph shows the overlap cardinalities between set intersections contained in the power set. The grid shows the intersection between one and more sets, and is aligned to the value shown in the bar graph column. The bar graph is sorted by overlap cardinality, presented from left to right, from least to greatest cardinality. (I leave out visualizing the empty set, although strictly speaking this is also a valid subset.)

While an Eulergrid is admittedly less intuitive to read than a circular Venn diagram, it can always show all true overlaps between all the sets, and without adding distortion or visual errors from "impossible" Venn overlaps.

The R script used to make Eulergrids will scale up to however many sets you need to show intersections for, but it will create an exponentially wider figure as the total number of permutations of intersections increase as a power of 2 (three sets have eight power set subsets, intersections of four sets have sixteen subsets; five sets have thirty-two subsets, etc.).

To demonstrate, here's an example of what an Eulergrid figure looks like:


The green denotes the count for that subset. Yellow coloring, in the context of this figure, represents cell-specific cardinality, i.e. the counts that are unique to a single cell type or dataset.

As a way to read this, for example, 42% of the total element overlaps over these five cells types involve SKNSH in some way. Of all those overlaps, roughly half can be assigned to SKNSH alone.

Here's the R code for plotEulergrid.R:

<script src="&lt;a href=" alexpreynolds="" 5531166"="">alexpreynolds/5531166"></script>

Here's a Perl-based wrapper to this R script, called

<script src="&lt;a href=" alexpreynolds="" 5535721"="">alexpreynolds/5535721"></script>

Here's an example of calling the Perl wrapper on the command line, which was used to make the figure shown above:

$ ./ \
    --setNames=GM06990,HepG2,K562,SKNSH,TH1 \
    --plotTitle="Footprint__overlaps__for__multiple__cell__lines\n(FDR__0.001)" \
    --setCardinalities=212350,233552,270586,287731,240701,93351,64049,89860,110579,62852,96806,89476,62075,64644,90129,30893,51178,53416,29083,32041,51033,28922,28279,48629,27407,22805,23548,39400,22418,21029,17172 \
    --setTotal=689952 \
    --outputFilename=results/footprintOverlaps/overlaps.fdr0p001.112409.png \
    --offCellColor="gray80" \
    --onCellColor="springgreen4" \

The option --ctsCounts refers to the yellow coloring I describe up above, representing "cell-type-specific" counts.

Hopefully, this gives you some ideas or at least an understanding that Venn diagrams cannot always represent intersections between more than three sets (and sometimes not even between three sets).

Entering edit mode

This is awesome! I was going to suggest the R packages venneuler or VennDiagram from my past experience, but I plan to use your function now.

Entering edit mode

Impressive answer! I shall definitely give this a go as Venn diagrams are often not a satisfactory method of presentation; this gives a lot of detail.

Entering edit mode

Hey Alex, really elegant! Could you please feed us with some sample data for your R script? I would like to play with this a bit. Thanks! Marcin

Entering edit mode

Thanks, Marcin. Please see the example provided to get a feel for how it works.

Entering edit mode
8.5 years ago
henryvuong ▴ 810

I like using Venny ( the most. It's very intuitive. I tried it with your example data. example

Entering edit mode

It's very attractive, but note that it doesn't show counts in an area-proportional manner. For example, there are empty set intersections (with count 0) that have greater area than 2-hit set intersections. This is a problem with using Euler ("Venn") diagrams to present this type of data.

Entering edit mode

Sir how could we add generate more lists in the venny, because i am tried but it was not generated pls help me

Entering edit mode

As far as i know, it can handle only four lists.

Entering edit mode
8.5 years ago
zx8754 10k

Change following lines:

hw <- (A = 1)
hm <- (B = 1)
hr <- (C = 1)


hw <- (A == 1)
hm <- (B == 1)
hr <- (C == 1)

Login before adding your answer.

Traffic: 2137 users visited in the last hour
Help About
Access RSS

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6