Last active
May 23, 2019 01:28
-
-
Save anfederico/6fe8fefb783061881ddfaae1c3837d55 to your computer and use it in GitHub Desktop.
Venn diagram written in ggplot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(ggplot2) | |
library(ggforce) | |
ggvenn <- function(a, b, ga, gb, title="") { | |
# Calculate area | |
x.a <- length(a) | |
x.b <- length(b) | |
x.u <- length(intersect(a, b)) | |
# Normalize area between 0-1 | |
nx.a <- x.a/(x.a+x.b) | |
nx.b <- x.b/(x.a+x.b) | |
# Circle radius and diameter | |
r.a <- sqrt(nx.a/pi); d.a <- r.a*2 | |
r.b <- sqrt(nx.b/pi); d.b <- r.b*2 | |
ol <- ifelse(x.a < x.b, d.a*x.u/x.a, d.b*x.u/x.b) | |
data.frame(x=c(-r.b-r.a+ol, 0), y=c(0, 0), | |
groups=c(paste(ga, ' (', x.a, ')', sep=''), | |
paste(gb, ' (', x.b, ')', sep=''))) %>% | |
ggplot(aes(x0=x, y0=y, r=c(r.a, r.b), fill=groups)) + | |
ggtitle(title) + | |
geom_circle(alpha=0.3, size = 0.5) + | |
coord_fixed() + | |
theme_void() + | |
theme(plot.title = element_text(hjust = 0.5)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment