パッケージを使用して、R から複数のプロット (ggplot2 経由) を単一の .tex ファイルに出力する for ループを作成しましたtikzDevice
。これにより、R から出力された .tex ファイル (「diagrams.tex」など) を指す単一のコマンドを使用して、latex ドキュメント内から複数の図を簡単に含めることができます\include{diagrams}
。
ただし、各 tikzpicture を\begin{figure}
環境でラップして、それぞれの図\caption{}
に と\label{}
.
質問: 出力された .tex ファイルに、(R ループからの) それぞれの ggplot 画像に対して、Figure ラッパー、キャプション、およびラベル ラテックス コマンドを直接含める方法はありますか?
以下は、3 つの ggplot を含むファイル「diagrams.tex」を生成する再現可能な R コードです。
require(ggplot2)
require(tikzDevice)
## Load example data frame
A1 = as.data.frame(rbind(c(4.0,1.5,6.1),
c(4.0,5.2,3.5),
c(4.0,3.4,4.3),
c(4.0,8.2,7.3),
c(4.0,2.9,6.3),
c(6.0,3.9,6.6),
c(6.0,1.5,6.1),
c(6.0,2.7,5.3),
c(6.0,2.9,7.4),
c(6.0,3.7,6.0),
c(8.0,3.9,4.2),
c(8.0,4.1,3.5),
c(8.0,3.7,5.8),
c(8.0,2.5,7.5),
c(8.0,4.1,3.5)))
names(A1) = c("state","rmaxpay","urate")
i = 2
## name output file
tikz( 'diagrams.tex' )
for (i in 2:4){ #begin LOOP
st = i*2
df = NULL
df = subset(A1, state == st , select = c(2:3))
print( # start print
ggplot(df, aes(rmaxpay,urate)) + geom_point()
) # end print
} #end LOOP
dev.off()