Dear Friends,
I had three list A, B and C having 700, 800 and 1200 elements in each and I wanted to draw a three way Venn diagram along with the list of elements in each part of diagram. The diagram mostly looks like in the figure.
The diagram shows three blocks A,B and C overlapping each other. The different overlapping areas are named with prefix "x". I programmed it in R to calculate the items list in xA,xB,xC,xAB.xBC,xAC,xABC and their counts. The program code is like this
# read the data from a text file (tab delimited) where column length may be different as a matrix (if column length is same you can read it is as dataframe)
a=read.table("test.txt",header=T,sep="\t")
a_mat=as.matrix(a)
# name the columns as A,B,C
A=a_mat[,1]
B=a_mat[,2]
C=a_mat[,3]
#Calculate the Venn diagram components using set theory rules of R
xA=setdiff(setdiff(A,B),C)
xB=setdiff(setdiff(B,C),A)
xC=setdiff(setdiff(C,B),A)
xAB=setdiff(intersect(A,B),C)
xBC=setdiff(intersect(B,C),A)
xAC=setdiff(intersect(A,C),B)
xABC=intersect(A,intersect(B,C))
# write the results in a text file .. (problem : the output is one column long list with seven headers, I didn't know how to create seven different columns at a time in this case, but it served my purpose)
write(c("\nxA",xA,"\nxB",xB,"\nxC",xC,"\nxAB",xAB,"\nxBC",xBC,"\nxAC",xAC,"\nxABC",xABC),file="result.txt");
# for quick testing the program you can define three list as
# define 3 sets
A=c(1,2,3,4,5,6,7,8)
B=c(3,4,5,6,7,8,9,10)
C=c(4,5,6,7,8,9,10,11,12,13)
# to count elements in each set say in xA, you can use length() function
LxA=length(xA) # calculate count of elements in list xA.
--------------ALL code at a complete program-----------------
A=c(1,2,3,4,5,6,7,8)
B=c(3,4,5,6,7,8,9,10)
C=c(4,5,6,7,8,9,10,11,12,13)
xA=setdiff(setdiff(A,B),C)
xB=setdiff(setdiff(B,C),A)
xC=setdiff(setdiff(C,B),A)
xAB=setdiff(intersect(A,B),C)
xBC=setdiff(intersect(B,C),A)
xAC=setdiff(intersect(A,C),B)
xABC=intersect(A,intersect(B,C))
write(c("\nxA",xA,"\nxB",xB,"\nxC",xC,"\nxAB",xAB,"\nxBC",xBC,"\nxAC",xAC,"\nxABC",xABC),file="result.txt");
----------------------code ends----------------------------------------
Online programs that can draw Venn diagrams but wouldn't worked in my cases.
1)http://bioinfogp.cnb.csic.es/tools/venny/index.html - It won't tell you the list of elements in each category
2) http://sablab.net/venn.php - Haven't checked the program
3)http://www.pangloss.com/seidel/Protocols/venn.cgi - Nice program but wont work with long list
Have fun...
I am a Bioinformatician at UMMC Medical Center Jackson MS
Great thanx Ranjit. And do you know how to display elements, instead of just the counts, within each circle of the Venn diagram?
ReplyDeleteOh this code really made my day! So happy to have found your post! Thanks!
ReplyDelete