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

Hello,

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 (http://www.ats.ucla.edu/stat/r/faq/venn.htm)

>library(limma)
>attach(hsb2)
>hw <- (A = 1)
>hm <- (B = 1)
>hr <- (C = 1)
>c3 <- cbind(hw, hm, hr)
>a <- vennCounts(c3)
>a


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
attr(,"class")
[1] "VennCounts


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

r statistics • 20k views
0
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.

15
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 eulergrid.pl:

<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:

\$ ./eulergrid.pl \
--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" \
--ctsCounts=65897,97624,173336,150753,91965


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

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

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

0
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

0
Entering edit mode

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

5
Entering edit mode
8.5 years ago
henryvuong ▴ 810

I like using Venny (http://bioinfogp.cnb.csic.es/tools/venny/) the most. It's very intuitive. I tried it with your example data.

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

0
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

0
Entering edit mode

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

3
Entering edit mode
8.5 years ago
zx8754 10k

Change following lines:

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


to:

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