Question: How Can I Generate A Venn Diagram In R For A Given Csv File
gravatar for Payal
7.8 years ago by
Payal40 wrote:


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 • 19k views
ADD COMMENTlink modified 7.8 years ago by henryvuong790 • written 7.8 years ago by Payal40

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.

ADD REPLYlink written 7.8 years ago by Nari880
gravatar for Alex Reynolds
7.8 years ago by
Alex Reynolds31k
Seattle, WA USA
Alex Reynolds31k wrote:

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).

ADD COMMENTlink modified 7.8 years ago • written 7.8 years ago by Alex Reynolds31k

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.

ADD REPLYlink modified 7.8 years ago • written 7.8 years ago by Josh Herr5.7k

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.

ADD REPLYlink written 7.8 years ago by Ian5.7k

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

ADD REPLYlink modified 7.8 years ago • written 7.8 years ago by marcin.bazyliszek0

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

ADD REPLYlink written 7.8 years ago by Alex Reynolds31k
gravatar for henryvuong
7.8 years ago by
henryvuong790 wrote:

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

ADD COMMENTlink written 7.8 years ago by henryvuong790

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.

ADD REPLYlink written 7.8 years ago by Alex Reynolds31k

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

ADD REPLYlink written 24 months ago by ar.silambu920

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

ADD REPLYlink written 5 weeks ago by Bioinformatician_in_trouble30
gravatar for zx8754
7.8 years ago by
zx87549.9k wrote:

Change following lines:

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


hw <- (A == 1)
hm <- (B == 1)
hr <- (C == 1)
ADD COMMENTlink written 7.8 years ago by zx87549.9k
Please log in to add an answer.


Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1618 users visited in the last hour