2017-08-26 147 views
2

我有以下简化的用例。基本上我有一些GGPLOT2情节,我想与其他正在使用的基本图形库产生的结合plot.new()等:如何通过arrangeGrob获取基本图形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

我不知道是否有可能与'gridextra',但检查'gridbase'; [在R数字窗口中结合base和ggplot图形](https://stackoverflow.com/questions/14124373/combine-base-and-ggplot-graphics-in-r-figure-window/14125565#14125565),[生成的图表通过'plot'和'ggplot'并排](https://stackoverflow.com/questions/13021863/plots-generated-by-plot-and-ggplot-side-by-side) – Henrik

回答

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) 
+0

非常感谢您的支持回答!我明白'gridExtra :: arrangeGrob',是不是可以直接使用这个API而不是'grid'?否则为了完整性,我怎样才能实现我的完整的OP使用情况考虑布局和最终输出到'ggsave'? –

+0

我真的不知道你的意思。您可以使用'arrangeGrob'而不是'grid.arrange'来存储组合grob。 – baptiste

+0

这正是我的意思 –

相关问题