Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Venn diagrams in multiple figure [duplicate]

Tags:

r

venn-diagram

is there a way to have the following venn diagrams as a 3rd figure after the two histograms below?? I'd like to have the two venn diagram one on top of the other

library(VennDiagram)

plus.venn<-draw.pairwise.venn(368, 1171, 149) #venn diagram 1

minus.venn<-draw.pairwise.venn(349, 1335, 173) #venn diagram 2

par(mfrow=c(1,3))

a<-sample(1:10000,3000)

b<-sample(5000:15000,3000)

hist(a)

hist(b)
like image 711
user3240953 Avatar asked Dec 25 '22 13:12

user3240953


1 Answers

Expand upon MrFlicks and my comments above.

You can combine base and grid graphics using the gridBase package. However, if you are not constrained to use base R graphics you may find it easier to produce all your plots using a grid based graphics package and combine them using the gridExtra package.

Your data

library(VennDiagram)

plus.venn <- draw.pairwise.venn(368, 1171, 149) #venn diagram 1
minus.venn <-draw.pairwise.venn(349, 1335, 173) #venn diagram 2
a <-sample(1:10000,3000)
b <-sample(5000:15000,3000)

Arranging base and grid plots

library(grid)
library(gridBase)

# Layout of plots - 4 plots of equal size
layout(matrix(1:4, 2, byrow = TRUE))

# First & second base plot
hist(a)
hist(b)

# Grid regions of current base plot (ie from frame)
frame()
vps <- baseViewports()
pushViewport(vps$inner, vps$figure, vps$plot)
grid.draw(plus.venn)
popViewport(3)

# fourth
frame()
vps <- baseViewports()
pushViewport(vps$inner, vps$figure, vps$plot)  
grid.draw(minus.venn)
popViewport(3)

Which produces

enter image description here

Or using ggplot2 to produce your histograms and combining using grid.arrange

library(ggplot2)
library(gridExtra)

grid.arrange(qplot(a, geom="histogram") + theme_classic(),
             qplot(b, geom="histogram") + theme_classic(),
             grobTree(plus.venn),
             grobTree(minus.venn),
             ncol=2)

Which produces

enter image description here

You can change the layout of either method to what you want.

like image 188
user20650 Avatar answered Jan 12 '23 09:01

user20650