Skip to content

Instantly share code, notes, and snippets.

@dmarx
Last active January 12, 2017 22:15
Show Gist options
  • Select an option

  • Save dmarx/b18f08322b635a32f4b8ac3105d72ca5 to your computer and use it in GitHub Desktop.

Select an option

Save dmarx/b18f08322b635a32f4b8ac3105d72ca5 to your computer and use it in GitHub Desktop.
Rough method for drawing labels in intersections of a venn diagram drawn using R's `venneueler` package
#install.packages('venneuler')
library(venneuler)
venn_intersection_text = function(venn, classes, label, adjustment=0.5, xadj=0, yadj=0 ){
# fits a line between the centers of two classes and draws label text at the midpoint of that line + adjustment
xv = adjustment*venn$centers[classes[1],1] + (1-adjustment)*venn$centers[classes[2],1] + xadj
yv = adjustment*venn$centers[classes[1],2] + (1-adjustment)*venn$centers[classes[2],2] + yadj
text(x=xv, y=yv, labels=label)
}
##########
## Demo ##
##########
if(FALSE){
library(MASS)
library(igraph)
venn_classes = c("A", "B","A&B")
venn_weights = c(10,50,5)
v = venneuler(venn_classes, venn_weights, labels=as.character(venn_weights) )
#par(mfrow=c(2,3))
#m <- rbind(1:3, c(4,5,5))
#layout(m)
#layout.show(4)
par(mfrow=c(2,1))
v$labels = as.character(venn_weights[1:2])
plot(v)
venn_intersection_text(v, c("A","B"), venn_weights[3], 0.7)
g = graph.data.frame(data.frame(src=1, tgt=2), directed=FALSE)
V(g)$name = venn_weights[1:2]
V(g)$size = venn_weights[1:2]*2.5
E(g)$label = 5
plot(g, layout = matrix(c(40,0,0,0),2))
m <- rbind(c(1,2), c(3,3))
layout(m)
v$labels[1] = ""
plot(v)
venn_intersection_text(v, c("A","B"), fractions(venn_weights[3]/venn_weights[2]), 0.7)
venn_intersection_text(v, c("A","B"), "←", 0.7, yadj=-.04)
v$labels = as.character(venn_weights[1:2])
v$labels[2] = ""
plot(v)
venn_intersection_text(v, c("A","B"), fractions(venn_weights[3]/venn_weights[1]), 0.7)
venn_intersection_text(v, c("A","B"), "→", 0.7, yadj=-.04)
readline(prompt="Press [enter] to continue")
g = graph.data.frame(matrix(c(1,2, 2,1), 2), directed=TRUE)
V(g)$name = venn_weights[1:2]
V(g)$size = venn_weights[1:2]*2.5
E(g)$label = as.character(fractions(c(venn_weights[3]/venn_weights[2], venn_weights[3]/venn_weights[1])))
plot(g, edge.curved=TRUE, layout = matrix(c(40,0,0,0),2))
par(mfrow=c(1,1))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment