Venn Diagrams with R?

Are there any other packages for doing Venn diagrams in R besides the limma package?

Anyone got tips?

Here's some notes on doing Venn diagrams with the limma packages.

Answers


Duncan Murdoch has a venn package, which is not on CRAN. (hat tip to Gabor Grothendieck)

You can also read about it in the "Journal of Statistical Software".


List of Venn Diagram packages:


There is Vennerable package on R-forge.

source("http://bioconductor.org/biocLite.R")
biocLite(c("graph", "RBGL", "gtools", "xtable"))
install.packages("Vennerable", repos="http://R-Forge.R-project.org")


The venn function in the gplots package is also useful if you need to create Venn Diagram of 4/5 sets.


I use two custom functions that to the trick. My implementation of venndia plots the venn diagram and returns lists of overlaps between A and B (and C). See the code below.

With these, you can

vd <- venndia(A=LETTERS[1:15], B=LETTERS[5:20], getdata=TRUE)

which will both plot and return the data. you can switch off returning the data by doing

venndia(A=LETTERS[1:15], B=LETTERS[5:20])

since getdata is FALSE by default. /Daniel

circle <- function(x, y, r, ...) {
    ang <- seq(0, 2*pi, length = 100)
    xx <- x + r * cos(ang)
    yy <- y + r * sin(ang)
    polygon(xx, yy, ...)
}

venndia <- function(A, B, C, getdata=FALSE, ...){
    cMissing <- missing(C)
    if(cMissing){ C <- c() }

    unionAB <- union(A, B)
    unionAC <- union(A, C)
    unionBC <- union(B, C)
    uniqueA <- setdiff(A, unionBC)
    uniqueB <- setdiff(B, unionAC)
    uniqueC <- setdiff(C, unionAB)
    intersAB <- setdiff(intersect(A, B), C)
    intersAC <- setdiff(intersect(A, C), B)
    intersBC <- setdiff(intersect(B, C), A)
    intersABC <- intersect(intersect(A, B), intersect(B, C))

    nA <- length(uniqueA)   
    nB <- length(uniqueB)
    nC <- length(uniqueC)

    nAB <- length(intersAB)
    nAC <- length(intersAC)
    nBC <- length(intersBC)

    nABC <- length(intersABC)   

    par(mar=c(2, 2, 0, 0))
    plot(-10, -10, ylim=c(0, 9), xlim=c(0, 9), axes=FALSE, ...)
    circle(x=3, y=6, r=3, col=rgb(1,0,0,.5), border=NA)
    circle(x=6, y=6, r=3, col=rgb(0,.5,.1,.5), border=NA)
    circle(x=4.5, y=3, r=3, col=rgb(0,0,1,.5), border=NA)

    text( x=c(1.2, 7.7, 4.5), y=c(7.8, 7.8, 0.8), c("A", "B", "C"), cex=3, col="gray90" )

    text(
        x=c(2, 7, 4.5, 4.5, 3, 6, 4.5), 
        y=c(7, 7, 2  , 7  , 4, 4, 5), 
        c(nA, nB, nC, nAB, nAC, nBC, nABC), 
        cex=2
    )

    if(getdata){
        list(A=uniqueA, B=uniqueB, C=uniqueC, 
            AB=intersAB , AC=intersAC , BC=intersBC , 
            ABC=intersABC
        )
    }
}

This comes very late but it might useful for others searching for an answer: VennDiagram, on CRAN here.

It allows multiple sets (four sets for venn, 3 sets for Euler diagrams), customizable colours and fonts, simple syntax and and best of all the size of the circles is proportional to the size of the data sets (at least when comparing 2 data sets). To install:

install.packages("VennDiagram")
library(VennDiagram)

For those using bioconductor packages and working with genomic coordinates, recently vennDiagram was implemented in the package ChIPpeakAnno (version 2.5.12) and allows pretty intersections of Genomic coordinates of, for instance, Chip-seq peaks. For early adopters you might need to install the development package.

peaks1 = RangedData(IRanges(start = c(967654, 2010897, 2496704),
    end = c(967754, 2010997, 2496804), names = c("Site1", "Site2", "Site3")),
    space = c("1", "2", "3"), strand=as.integer(1),feature=c("a","b","f"))

peaks2 = RangedData(IRanges(start = c(967659, 2010898,2496700,3075866,3123260),
    end = c(967869, 2011108, 2496920, 3076166, 3123470),
    names = c("t1", "t2", "t3", "t4", "t5")),
    space = c("1", "2", "3", "1", "2"), strand = c(1, 1, -1,-1,1), feature=c("a","b","c","d","a"))

makeVennDiagram(RangedDataList(peaks1,peaks2, peaks1, peaks2), NameOfPeaks=c("TF1", "TF2","TF3", "TF4"),
     totalTest=100,useFeature=TRUE, main="Venn Diagram",
    col = "transparent",fill = c("cornflowerblue", "green", "yellow", "darkorchid1"),
    alpha = 0.50,label.col = c("orange", "white", "darkorchid4", "white", "white", "white", "white", "white", "darkblue", "white", "white", "white", "white", "darkgreen", "white"), cat.col = c("darkblue", "darkgreen", "orange", "darkorchid4"))

Here is reference to another version for 3-variable data: http://elliotnoma.wordpress.com/2011/02/09/venn-diagram/

The code is also available in the package colorfulVennPlot: http://cran.r-project.org/web/packages/colorfulVennPlot/index.html


I would recommend the package VennDiagram: http://cran.r-project.org/web/packages/VennDiagram/VennDiagram.pdf

On pake 19 you will find 10 pakes of very good examples (both advanced and simplified ones). As of yet I have not found anything that it can't do that I need it to do.


Need Your Help

How does local_assigns work in Rails?

ruby-on-rails views

I've been googling around about this and can't find the right path. I'm working on a Rails app that is using a method called local_assigns. This appears to be something in Rails or a gem, and not

What is the difference between `raise "foo"` and `raise Exception.new("foo")`?

ruby exception exception-handling

What is the difference - technical, philosophical, conceptual, or otherwise - between