File: plot.venn.R

package info (click to toggle)
gplots 3.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,580 kB
  • sloc: makefile: 2
file content (314 lines) | stat: -rw-r--r-- 8,669 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314

plot.venn <- function(x, y, ...,
                      small=0.7,
                      showSetLogicLabel=FALSE,
                      simplify=FALSE
                      )
  {
    drawVennDiagram(
                    data=x,
                    small=small,
                    showSetLogicLabel=showSetLogicLabel,
                    simplify=simplify
                    )
  }

## data should be a matrix.
##   - The first column of the matrix is the
##     count of the number of objects with the specified pattern.
##   - The second and subsequent columns contain 0-1 indicators
##     giving the pattern of group membership


drawVennDiagram <-function(data,
                           small=0.7,
                           showSetLogicLabel=FALSE,
                           simplify=FALSE)
  {
	numCircles<-NA
	data.colnames<-NULL
	data.rownames<-NULL

	if(is.matrix(data)) {
		numCircles<-ncol(data)-1
		data.colnames<-colnames(data)[2:(ncol(data))]
		# Order is reverted since later indexing starts with
		# the "lowest bit" and that is expected at the left
		data.rownames<-rownames(data)
	}
	else {
		if (is.list(data)) {
			stop("gplots.drawVennDiagram: This internal function is used wrongly. ",
                             "Please call the function 'venn' with the same arguments, instead.\n")
		}

		warning("drawVennDiagram: Testing only, presuming first argument to specify",
		     "the number of circles to draw.\n")
		numCircles<-data
	}


	m<-(0:(-1+2^numCircles))

	if (! is.matrix(data)) {
		##cat("prepare randomised data\n")
		data<-t(sapply(X=m,FUN=function(v){
			l<-baseOf(v,2,numCircles)
			#print(l)
			return(l)
		}))

		#print(data)

		#data.names<-apply(data,1,function(X){
		#	return(paste(X),collapse="")
		#})
		for(i in m) {
			n<-paste(data[i+1,],collapse="")
			if (is.null(data.rownames)) {
				data.rownames<-n
			}
			else {
				data.rownames<-c(data.rownames,n)
			}
		}
		#print(data.rownames)
		data<-cbind(sample(1:100,size=2^numCircles,replace=TRUE),data)
		#print(data)
		rownames(data)<-data.rownames
		data.colnames<-LETTERS[1:numCircles]
		colnames(data)<-c("num",data.colnames)
	}


	plot.new()
	h<-400
	plot.window(c(0,h), c(0,h), ylab="", xlab="")

	if ((2 <= numCircles && numCircles <= 3) || (4 == numCircles && simplify)) {


		circle <- function(x,y=NULL,r=1) {
			elps=cbind(r*cos(seq(0,2*pi,len=1000)), r*sin(seq(0,2*pi,len=1000)));
			if (!is.null(y)) {
				if (length(x) != length(y))
				  stop("circle: both x and y need to be of same length")
				if (is.matrix(x) && ncol(x)>1)
				  stop("circle: if y is not NULL, then x must not be a matrix")
				x<-cbind(x,y)
			}
			for(i in 1:nrow(x)) {
				ax<-elps[,1]+rep(x[i,1],1000)
				ay<-elps[,2]+rep(x[i,2],1000)
				polygon(ax,ay)
			}
		}

		#nolongerreguired#require(grid)

		##cat("drawing circles\n")
		# draw circles with radius 1.7 equally distributed
		# with centers on a circle of radius 1

		degrees<-2*pi/numCircles*(1:numCircles)

		# scaling factor
		s<-1/8*h
		# radius for circles
		r<-3/12*h

		x<-sapply(degrees,FUN=sin)*s + 0.5*h
		y<-sapply(degrees,FUN=cos)*s + 0.5*h

		##cat("filling data\n")
		circle(x,y,r)

		distFromZero<-rep(NA,2^numCircles)
		degrees<-rep(NA,2^numCircles)

		degrees[(2^numCircles)]<-0
		distFromZero[(2^numCircles)]<-0

		for (i in 0:(numCircles-1)) {
			distFromZero[2^i+1] <- r
			degrees[2^i+1] <- 2*pi/numCircles*i
			d<-degrees[2^i+1]

			#print(data.colnames)

			text(
				# starting from the lowest bit, hence reading
				# lables from the right
				label=data.colnames[numCircles - i],
				x=sin(d)*5/12*h+0.5*h,
				y=cos(d)*5/12*h+0.5*h
			)

		}

		if (4==numCircles) {
			for (i in 0:(numCircles-1)) {
				# Current set bit plus the bit left of it and the bit right of it
				distFromZero[2^i
						+2^((i+numCircles-1)%%numCircles)
						+2^((i+1)%%numCircles)+1] <- 2/12*h
				distFromZero[2^i+1] <- 3.5/12*h
				degrees[2^i
						+2^((i+numCircles-1)%%numCircles)
						+2^((i+1)%%numCircles)+1] <- degrees[2^i+1]
			}
		}

				#degrees[2^i+1] + degrees[2^((i+1)%%numCircles)+1])/2

		if (3 <=numCircles) {
			for (i in 0:(numCircles-1)) {
				distFromZero[(2^i+2^((i+1)%%numCircles))+1]<- 2.2/12*h
				distFromZero[2^i+1] <- 3/12*h
				if (i == (numCircles-1)) {
					degrees[(2^i+2^((i+1)%%numCircles))+1] <- (
						degrees[2^i+1] + 2*pi+ degrees[1+1])/2
				}
				else {
					degrees[(2^i+2^((i+1)%%numCircles))+1] <- (
						degrees[2^i+1] + degrees[2^((i+1)%%numCircles)+1])/2
				}
			}
		}

		for(i in 1:2^numCircles) {
			n<-paste(baseOf((i-1),2,numCircles),collapse="")
			v<-data[n,1]
			d<-degrees[i]
			if (1 == length(d) && is.na(d)) {
				if (v>0) warning("Not shown: ",n," contains ",v,"\n")
			}
			else {
				l<-distFromZero[i]
				x<-sin(d)*l+0.5*h
				y<-cos(d)*l+0.5*h
				#cat("i=",i," x=",x," y=",y," label=",n,"\n")
				l<-v
				if (showSetLogicLabel) l<-paste(n,"\n",v,sep="")
				text(label=l,x=x,y=y)
			}
		}

	}
	else if ( (4 == numCircles && !simplify) || numCircles <= 5 ) {

		# Function to turn and move ellipses/circles
		relocate_elp <- function(e, alpha, x, y){
			phi=(alpha/180)*pi;
			xr=e[,1]*cos(phi)+e[,2]*sin(phi)
			yr=-e[,1]*sin(phi)+e[,2]*cos(phi)
			xr=x+xr;
			yr=y+yr;
			return(cbind(xr, yr))
		}

		lab<-function (identifier, data, showLabel=showSetLogicLabel) {
			r<-data[identifier,1]
			if (showLabel) {
				return(paste(identifier,r,sep="\n"))
			}
			else {
				return(r)
			}
		}

	    if (4 == numCircles) {
	        elps=cbind(162*cos(seq(0,2*pi,len=1000)), 108*sin(seq(0,2*pi,len=1000)));

		#plot(c(0, 400), c(0, 400), type="n", axes=F, ylab="", xlab="");
		polygon(relocate_elp(elps, 45,130,170));
		polygon(relocate_elp(elps, 45,200,200));
		polygon(relocate_elp(elps,135,200,200));
		polygon(relocate_elp(elps,135,270,170));

		text( 35, 315, data.colnames[1],cex=1.5)
		text(138, 347, data.colnames[2],cex=1.5)
		text(262, 347, data.colnames[3],cex=1.5)
		text(365, 315, data.colnames[4],cex=1.5)

	        elps <- cbind(130*cos(seq(0,2*pi,len=1000)),
			80*sin(seq(0,2*pi,len=1000)))

		text( 35, 250, lab("1000",data));
		text(140, 315, lab("0100",data));
		text(260, 315, lab("0010",data));
		text(365, 250, lab("0001",data));

		text( 90, 280, lab("1100",data), cex=small)
		text( 95, 110, lab("1010",data) )
		text(200,  50, lab("1001",data), cex=small)
		text(200, 290, lab("0110",data))
		text(300, 110, lab("0101",data))
		text(310, 280, lab("0011",data), cex=small)

		text(130, 230, lab("1110",data))
		text(245,  75, lab("1101",data),cex=small)
		text(155,  75, lab("1011",data),cex=small)
		text(270, 230, lab("0111",data))

		text(200,150,lab("1111",data))
	    }
	    else if (5 == numCircles) {

	        elps <- cbind(150*cos(seq(0,2*pi,len=1000)),
			60*sin(seq(0,2*pi,len=1000)))

		polygon(relocate_elp(elps, 90,200, 250))
		polygon(relocate_elp(elps, 162,250, 220))
		polygon(relocate_elp(elps, 234,250, 150))
		polygon(relocate_elp(elps, 306,180, 125))
		polygon(relocate_elp(elps, 378,145, 200))

		text( 20, 295, data.colnames[1],cex=1.5)
		text(140, 380, data.colnames[2],cex=1.5)
		text(350, 318, data.colnames[3],cex=1.5)
		text(350,   2, data.colnames[4],cex=1.5)
		text( 50,  10, data.colnames[5],cex=1.5)

		text( 61, 228, lab("10000",data));
		text(194, 329, lab("01000",data));
		text(321, 245, lab("00100",data));
		text(290,  81, lab("00010",data));
		text(132,  69, lab("00001",data));

		text(146, 250, lab("11000",data), cex=small)
		text(123, 188, lab("10100",data), cex=small)
		text(275, 152, lab("10010",data), cex=small)
		text(137, 146, lab("10001",data), cex=small)
		text(243, 268, lab("01100",data), cex=small)
		text(175, 267, lab("01010",data), cex=small)
		text(187, 117, lab("01001",data), cex=small)
		text(286, 188, lab("00110",data), cex=small)
		text(267, 235, lab("00101",data), cex=small)
		text(228, 105, lab("00011",data), cex=small)

		text(148, 210, lab("11100",data),cex=small)
		text(159, 253, lab("11010",data),cex=small)
		text(171, 141, lab("11001",data),cex=small)
		text(281, 175, lab("10110",data),cex=small)
		text(143, 163, lab("10101",data),cex=small)
		text(252, 145, lab("10011",data),cex=small)
		text(205, 255, lab("01110",data),cex=small)
		text(254, 243, lab("01101",data),cex=small)
		text(211, 118, lab("01011",data),cex=small)
		text(267, 211, lab("00111",data),cex=small)

		text(170, 231,lab("11110",data),cex=small)
		text(158, 169,lab("11101",data),cex=small)
		text(212, 139,lab("11011",data),cex=small)
		text(263, 180,lab("10111",data),cex=small)
		text(239, 232,lab("01111",data),cex=small)

		text(204,190,lab("11111",data))
	    }
	}
	else {
		stop(paste("Venn diagrams for ",numCircles," dimensions are not yet supported.\n"))
	}

}