我有以下简化的用例。基本上我有一些ggplot2图,我想与使用基本图形库plot.new()
等生成的另一个图组合:
p1 <- generate_ggplot1(...)
p2 <- generate_ggplot2(...)
p3 <- generate_ggplot3(...)
# how to get hold of the plot output and make it available as
# p4 for arrangeGrob?
plot.new()
...
final <- gridExtra::arrangeGrob(p1, p2, p3, p4, layout_matrix = rbind(c(1,2), c(3,4)), widths=c(7,7), heights=c(7,7))
ggplot2::ggsave(filename=output.file,plot=final,width=14,height=14)
有什么选择呢?与将p4重写为原生ggplot2
答案 0 :(得分:5)
尝试this,
library(gridGraphics)
library(grid)
library(gridExtra)
library(ggplot2)
grab_grob <- function(...){
grid.echo(...)
grid.grab()
}
b <- grab_grob(function() plot(cars))
g <- ggplot()
grid.arrange(b, g)