を使用して複数のプロットをプロットし、 を使用しggplot2
て配置しようとしていgrid.arrange()
ます。私が抱えている正確な問題を説明している人を見つけることができたので、リンクの問題の説明から引用しました:
ggsave()
afterを使用する場合grid.arrange()
、つまり
grid.arrange(sgcir1,sgcir2,sgcir3,ncol=2,nrow=2) ggsave("sgcirNIR.jpg")
グリッド プロットは保存しませんが、最後の個々の ggplot を保存します。または同様のものgrid.arrange()
を使用し
て表示されたプロットを実際に保存する方法はありますか? ggsave()
古い方法を使用する以外
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
ただし、 linkから取得した次のコードで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()?