Gplots Venn

Posted onby

We can draw a simple Venn diagram of the sets (two or more) by calling the function venn from the gplots. See the script below for example calls: See the script below for example calls. Author radiaj Posted on June 21, 2016 Categories gplots, R, VennDiagram Tags GeneLists, Genes, R, VennDiagram 1 Comment on Working with Venn Diagrams Search Recent Posts.

  1. Gplots Venn Example

The gplots package provides Venn diagrams for up to five sets. Its input is a table that is produced by another function. The function venn() calls one after the other and is the only one to be seen by the user. The venn() function accepts either a list of sets as an argument, or it takes a binary matrix, one column per set, indicating for every element, one per row, the membership with every set.

The names of columns or the list elements are the set names. To squeeze extra circles in, those circles need to become ellipses. This works for four sets and maybe even more impressively also for five.

Further reading
gplots

Plot venn diagram
Venndiagram.R
# Imagine you have more than two sets and you would want to find the overlapping elements in different sets
# and you would like to see the overlap using VennDiagram
require(VennDiagram)
library(gplots)
library(reshape2)
# We have three different dataframes with the customer-id as Key, and some additional fields
set1=data.frame(Key= c(100,200,300), place= c('NY','IS','AZ'))
set2=data.frame(Key= c(200,300,400), val2= c(12,12,53))
set3=data.frame(Key= c(200,500,600), val3= c(134,353,23))
# create a list with the key from the three sets
input=list(A= unique(set1$Key),
B= unique(set2$Key),
C= unique(set3$Key))
#1 View VennDiagram with the internal binary group labels
# binary group labels 100 - indicates present in A, not in B and not in C
venn(input, show.plot=FALSE)
# the various overlapping areas are called intersections
groupcount<- venn(input, show.plot=FALSE)
# view the customers in various intersections
print(groupcount)
#2 List the customers who are common in all the sets - choose the corresponding intersection
print('customers who are common in all the sets :')
attr(groupcount, 'intersections')$`A:B:C`
#3 Get the number of customers per intersection
print('number of customers per intersection :')
sapply(attr(groupcount, 'intersections'),length)
#4 View which customer belongs to which intersection
# there are two different ways to do this, using base function and using reshape2::melt
print('List of customerid with their corresponding intersection :')
customers= attr(groupcount, 'intersections')
# One method - using base functions
print('using base function')
data.frame(Pattern=rep(names(customers),sapply(customers,length)),customerid=unlist(customers))
# Use reshape2 and melt which has a method melt.list
print('using reshape2::melt')
CustPattern=reshape2::melt(customers)
CustPattern=plyr::rename(CustPattern, c('value'='Customerid', 'L1'='Intersection'))
print(CustPattern)

Gplots Venn Example

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment