I want to plot a grid and base plot in the same plot in a pdf
output. I see how to add grid plus base in several places including here: Combining grid.table and base package plots in R figure and R put together traditional plot and ggplot2
I also want to control the widths of the plots using a proportion or relative width. I thought I had it with the code below unto I tried to output to a pdf and then it prints a blank page before the plot. How can I make it not plot a blank page? I'm not married to any package I would just like the answer to be generalizable (simple is a plus; I feel like my code below could be simplified).
library(ggplot2); library(grid); library(gridBase)
pdf("test.pdf")
grid::grid.newpage()
grid::pushViewport(grid::viewport(layout = grid::grid.layout(1, ncol=2,
widths = grid::unit(c(.6, .4), "npc"))))
#Draw base plot
grid::pushViewport(grid::viewport(layout.pos.col = 1, width = grid::unit(.8, "npc")))
graphics::plot.new()
graphics::par(fig = gridBase::gridFIG(), mar=c(1, 1, 1, 1), new = TRUE)
plot(1:10)
grid::popViewport()
#Draw ggplot
grid::pushViewport(grid::viewport(layout.pos.col = 2, width = grid::unit(.2, "npc")))
print( ggplot(mtcars, aes(mpg, hp)) + geom_point(), newpage = FALSE)
grid::popViewport()
dev.off()
Plot looks like: