将输出从gridExtra :: grid.arrange存储到对象中 [英] Store output from gridExtra::grid.arrange into an object
问题描述
我使用 gridExtra :: grid.arrange
将多个图放入一个图像中,并希望将组合图保存为可以返回的对象从函数内部作为返回对象列表的一部分。理想情况下,我希望在不打印plot对象的情况下执行此操作。
下面的代码创建两个图,将它们与 grid.arrange
,并尝试将结果保存到
x
中。但是, x
的计算结果为 NULL
并打印出图。 grid.arrange
的文档指向 arrangeGrob
,并建议使用关闭绘图。 plot = FALSE
,但是当我尝试这个时出错,因为 FALSE
不是grob对象。
对于我不理解的任何建议?
#正在开发中的$ R
#Windows 7(32位)
#ggplot2 1.0.0
#gridExtra 0.9.1
p1 < - ggplot(mtcars,aes(x = factor(cyl),y = mpg))+ geom_boxplot()
p2 < - ggplot(mtcars,aes(x = factor(cyl),y = wt))+ geom_boxplot()
x< ; - gridExtra :: grid.arrange(p1,p2)
x
根据评论,我添加了这个编辑。当我用 arrangeGrob
尝试它时,我根本没有输出。
> gridExtra :: arrangeGrob(p1,p2)
> print(gridExtra :: arrangeGrob(p1,p2))
错误:图中没有图层
> x< - gridExtra :: arrangeGrob(p1,p2)
> x
错误:在图中没有图层
由于您未加载 gridExtra
,因此编辑中的代码无法正常运行。
$ b $ library $ {$ b $ [$] $ g $ $ $ $ $ $ $ $ $ $ $ $
grid.draw(y)
编辑:从版本2.0.0开始,我对 grid
dependency below不再有效,因为 grid
现在被导入。
编辑:使用gridExtra版本> = 2.0.0,不需要附加任何包, :qplot(1,1)
x< - gridExtra :: arrangeGrob(p,p)
grid :: grid.draw(x)
I am placing multiple plots into one image using gridExtra::grid.arrange
and would like to have the option of saving the combined plot as an object that could be returned from within a function as part of a list of returned objects. Ideally, I would like to do this without printing the plot object.
The code below creates two plots, combines them with grid.arrange
, and attempts to save the result into x
. However, x
evaluates to NULL
and the plot is printed. The documentation for grid.arrange
points me to arrangeGrob
and suggests plotting can be turned off using plot=FALSE
, but I get an error when I try that because FALSE
is not a grob object.
Any suggestions for what I'm not understanding?
# R under development
# Windows 7 (32 bit)
# ggplot2 1.0.0
# gridExtra 0.9.1
p1 <- ggplot(mtcars, aes(x=factor(cyl), y=mpg)) + geom_boxplot()
p2 <- ggplot(mtcars, aes(x=factor(cyl), y=wt)) + geom_boxplot()
x <- gridExtra::grid.arrange(p1, p2)
x
Per the comments, I'm adding this edit. When I try it with arrangeGrob
, I get no output at all.
> gridExtra::arrangeGrob(p1, p2)
> print(gridExtra::arrangeGrob(p1, p2))
Error: No layers in plot
> x <- gridExtra::arrangeGrob(p1, p2)
> x
Error: No layers in plot
The code in your edit does not work properly since you didn't load gridExtra
.
library(gridExtra)
y <- arrangeGrob(p1, p2, ncol = 1)
class(y)
#[1] "gtable" "grob" "gDesc"
grid.draw(y)
Edit: since version 2.0.0, my comment about grid
dependency below is no longer valid, since grid
is now imported.
Edit: With gridExtra version >= 2.0.0, there is no need to attach either package,
p <- ggplot2::qplot(1,1)
x <- gridExtra::arrangeGrob(p, p)
grid::grid.draw(x)
这篇关于将输出从gridExtra :: grid.arrange存储到对象中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!