Search code examples
rggplot2gridinsets

ggplot2 add two plots in grid.arrange with an inset in the second one in R


I'm able to print an inset and to create a grid from plots in ggplots. But I'm not able to create a grid with a plot on the left and 2 plots on the right one full size and the other in "inset".

a_plot <- ggplot(cars, aes(speed, dist)) + geom_line()

#The inset 
print(a_plot);print(a_plot, vp = vp)

enter image description here

# the Grid
lay <- rbind(c(1,1,1,2,2,2),
             c(1,1,1,2,2,2),
             c(1,1,1,2,2,2),
             c(1,1,1,2,2,2))
grid.arrange(a_plot, a_plot,layout_matrix = lay)

enter image description here

But I would like to have this:

enter image description here

How can I do this?

This doesn't work

grid.arrange(a_plot, a_plot,print(a_plot, vp = vp),layout_matrix = lay)

I tried this and it didn't work either.


Solution

  • a_plot <- ggplot(cars, aes(speed, dist)) + geom_line()
    b_plot <- a_plot + annotation_custom(grob = rectGrob(), 
                          xmin = 15, xmax = Inf, ymin=-Inf, ymax=25)
    
    
    grid.arrange(a_plot, b_plot, ncol=2)
    

    enter image description here