I have a grob
object (in my case it's euler
plot) and a ggplot
object, and I want to place one on top of another, for example:
library(eulerr)
library(ggplot2)
df <- data.frame(a=sample(100),b=sample(50:149), c=sample(20:119))
venn <- euler(list(
A=df$a,
B=df$b[1:50],
C=df$c
), shape='ellipse')
p_v <- plot(venn, quantities = T, fills=c('red','green','blue'))
p_g <- ggplot(df, aes(x=a,y=b)) + geom_point()
# Now I want somehow to draw p_v on top of p_g
p_g + p_v
Should produce something like this:
I tried using ggplotify
for example but couldn't find a way to get rid of white rectangle that was drawn as a canvas for the second plot...
You could use
annotation_custom
:If you want this to work with log axis scales, you will need to use
grid
to directly drawp_v
overp_g
. You will first need to put it in a grobtree so that you can specify its position and dimensions:If you want this as a single R object, you can do:
So that
obj
is now agrob
of your whole picture.One further way to do this would be using
geom_grob
from packageggpmisc
: