2017-08-26 152 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

這正是我的意思 –

相關問題