2013-06-12 25 views
90

私はggplot2を使用して複数のプロットをプロットしようとしており、grid.arrange()を使用してそれらを配置しています。ファイルにgrid.arrange()プロットを保存する

私はgrid.arrange()ggsave()を使用する場合、すなわち

grid.arrange(sgcir1,sgcir2,sgcir3,ncol=2,nrow=2) 
ggsave("sgcirNIR.jpg") 

を私は、グリッドのプロットを保存しない:私は私が持っている正確な問題を説明する誰かが、私はlinkから問題の説明から引用している見つけることができたので 最後の個々のggplot。 で表示されるプロットを実際に保存する方法はありますか 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 

を、私は中grid.arrange()コールの出力を保存するためにggsave()を使用する方法を見つけ出すことはできませんlinkから取得され、次のコード、:

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()? 
+5

'png();を使用してください。 grid.arrange(); ggplot(); ggplot(); dev.off() ' – Andrie

+1

' print(ggplot()) 'はありません? –

+0

@DWinはい、おそらく!:-) – Andrie

答えて

91

grid.arrangeが上に直接描画しますデバイス。一方、arrangeGrobは、何も描画しませんが、ggsave(file="whatever.pdf", g)に渡すことができるgrob gを返します。

ggplotオブジェクトとは異なる動作をする理由は、デフォルトで最後のプロットが指定されていなければ保存されるため、ggplot2は最新のプロットを目に見えない状態に保ちます。grid.arrangeはこのカウンタを混乱させるはずですパッケージ専用です。

+0

ありがとう!これはまさに私が探していたものです。 –

+1

これを試してみると、gが正しいタイプではないというエラーが表示されます。 –

+0

@JackAidleyは、自己完結型の再現可能な例とsessionInfo()(新しいバージョンのRとパッケージがあることを確認してください)を使って、新しい質問をします。 – baptiste

24

私は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 

これはうまくいくはずです。

4

私はこれを追加する価値があると思いました。 私はggsaveがエラーを生成すると、上記の問題を持っていた:Saving a graph with ggsave after using ggplot_build and ggplot_gtable 私は上記のコードに修正を持っている: この回答へ

おかげ「プロットはggplot2プロットでなければなりません」。

# 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] 

g <- arrangeGrob(plot1, plot2, plot3, nrow=3) #generates g 
ggsave(file="whatever.pdf", g) #saves g 

エラーを修正するために必要な上記の行は、今では罰金私のために動作します。

+0

私はこれが必要でした。どうやらggplot2の開発版ではこのようなクラステストフォルトが削除されていますが、現在のCRANバージョン(2015-10-21)ではそうではありません。 –

+2

これはmarrangeGrobで動作しますが、arrangeGrobやgrid.arrangeでは動作しません。@ DaveX、上記のようにggsaveを変更する以外に何かをする必要がありましたか?ありがとう! – k13

14

PDFファイルにgrid.arrangeを保存するもう一つの簡単な方法は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 

それはテーブルのように、配置のggplots以外のものをマージすることができます...

関連する問題