I L*_*ode 128 r ggplot2 gridextra
我正在尝试绘制多个绘图,使用ggplot2它们进行排列grid.arrange().由于我设法找到某人描述我遇到的确切问题,因此我引用了链接中的问题描述:
当我使用ggsave()后grid.arrange(),即
Run Code Online (Sandbox Code Playgroud)grid.arrange(sgcir1,sgcir2,sgcir3,ncol=2,nrow=2) ggsave("sgcirNIR.jpg")
我不保存网格图,而是保存最后一个ggplot.是否有任何方法可以通过grid.arrange()使用
ggsave()或类似的方式实际保存绘图?除了使用旧的方式
Run Code Online (Sandbox Code Playgroud)jpeg("sgcirNIR.jpg") grid.arrange(sgcir1,sgcir2,sgcir3,ncol=2,nrow=2) dev.off()
相同的链接提供以下解决方案:
require(grid)
require(gridExtra)
p <- arrangeGrob(qplot(1,1), textGrob("test"))
grid.draw(p) # interactive device
ggsave("saving.pdf", p) # need to specify what to save explicitly
Run Code Online (Sandbox Code Playgroud)
但是,我无法弄清楚如何使用以下代码ggsave()保存grid.arrange()调用的输出,该代码取自链接:
library(ggplot2)
library(gridExtra)
dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
p1 <- qplot(carat, price, data=dsamp, colour=clarity)
p2 <- qplot(carat, price, data=dsamp, colour=clarity, geom="path")
g_legend<-function(a.gplot){
tmp <- ggplot_gtable(ggplot_build(a.gplot))
leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
legend <- tmp$grobs[[leg]]
return(legend)}
legend <- g_legend(p1)
lwidth <- sum(legend$width)
## using grid.arrange for convenience
## could also manually push viewports
grid.arrange(arrangeGrob(p1 + theme(legend.position="none"),
p2 + theme(legend.position="none"),
main ="this is a title",
left = "This is my global Y-axis title"), legend,
widths=unit.c(unit(1, "npc") - lwidth, lwidth), nrow=1)
# What code to put here to save output of grid.arrange()?
Run Code Online (Sandbox Code Playgroud)
bap*_*ste 132
grid.arrange直接在设备上绘制.arrangeGrob另一方面,除了返回一个grob之外g,它不会绘制任何东西,你可以传递给它ggsave(file="whatever.pdf", g).
它与ggplot对象的工作方式不同,默认情况下,如果没有指定,最后一个图是保存的,ggplot2无形地跟踪最新的情节,我认为不grid.arrange应该把这个计数器私有打包.
use*_*4L2 41
我对babptiste的建议有些问题,但终于得到了它.这是你应该使用的:
# draw your plots
plot1 <- ggplot(...) # this specifies your first plot
plot2 <- ggplot(...) # this specifies your second plot
plot3 <- ggplot(...) # this specifies your third plot
#merge all three plots within one grid (and visualize this)
grid.arrange(plot1, plot2, plot3, nrow=3) #arranges plots within grid
#save
g <- arrangeGrob(plot1, plot2, plot3, nrow=3) #generates g
ggsave(file="whatever.pdf", g) #saves g
Run Code Online (Sandbox Code Playgroud)
这应该很好.
Joh*_*Bee 21
另一种将grid.arrange保存到pdf文件的简单方法是使用pdf():
pdf("filename.pdf", width = 8, height = 12) # Open a new pdf file
grid.arrange(plot1, plot2, plot3, nrow=3) # Write the grid.arrange in the file
dev.off() # Close the file
Run Code Online (Sandbox Code Playgroud)
它允许在排列中合并除ggplots之外的其他内容,例如表...
小智 7
您不需要使用arrangeGrob,您可以将grid.arrange 的结果直接分配给绘图并使用ggsave 保存:
p3 <- grid.arrange(p1,p2, nrow = 1)
ggsave("filename.jpg", p3)
Run Code Online (Sandbox Code Playgroud)
小智 6
我认为值得补充。我在上述问题上遇到了问题,ggsave产生了一个错误:“情节应该是ggplot2情节”
感谢这个答案:使用ggplot_build和ggplot_gtable后,用ggsave保存图形 我对上面的代码做了修改。
# draw your plots
plot1 <- ggplot(...) # this specifies your first plot
plot2 <- ggplot(...) # this specifies your second plot
plot3 <- ggplot(...) # this specifies your third plot
#merge all three plots within one grid (and visualize this)
grid.arrange(plot1, plot2, plot3, nrow=3) #arranges plots within grid
#save
ggsave <- ggplot2::ggsave; body(ggsave) <- body(ggplot2::ggsave)[-2]
Run Code Online (Sandbox Code Playgroud)
上面的行需要修复错误
g <- arrangeGrob(plot1, plot2, plot3, nrow=3) #generates g
ggsave(file="whatever.pdf", g) #saves g
Run Code Online (Sandbox Code Playgroud)
现在对我来说很好。
小智 5
尝试这个
ggsave("whatever.png", plot=grid.arrange(plot1, plot2, plot3, nrow=3), device=..., scale = ..., width =..., height = ..., units = "...", dpi = ...)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
78578 次 |
| 最近记录: |