2016-06-23 7 views
3

私はmetaforを使って各繰り返しでフォレストグラフを生成するRループを持っています。フォレストグラフにはサンプルあたり1行があり、繰り返しごとにサンプル数が異なるため、高さをかなり変える必要があります(現在は2.5〜8インチ)。Knitrは同じ塊で図形のサイズが異なります

私はのようないくつかのオプションを試しましたが、私が作成した各グラフィックは.pdfファイルの出力に同じ高さを持っています(単にファイルを正方形にするようです)。マージンの上下。

カスタムグラフィックデバイスhereのノートも見つかりましたが、チャンクの途中でグラフィックデバイスを変更する方法はわかりません。私は単に各ループの反復でopts_chunk$set(fig.width=fheight)を使用しようとしましたが、運はありませんでした。


MWE

\documentclass{article} 

\begin{document} 

<<Mwe, echo=FALSE, results = 'asis', message='FALSE', fig.width=7,warning='FALSE'>>= 

heights <- c(2.5, 8) 

for(counter in 1:length(heights)) { 
    opts_chunk$set(fig.height=heights[counter]) #This doesn't appear to change anything 
    par(fin=c(7, heights[counter]) #this makes the plot have the correct height, but I get a 2.5 inch high plot vertically centered in a 7 inch high pdf. 
    hist(rnorm(100)) 

    cat("Some long text which describes a lot of stuff about this graphic before making a new subsection for the next one") 
} 

@ 

\end{document} 

答えて

0
\documentclass{article} 

\begin{document} 

<<Mwe, echo=FALSE, results = 'asis', message = F, warning = F>>= 
library(knitr) 
library(magrittr) 

heights <- c(2.5, 8) 

captions <- c("Caption 1", "Caption 2") 

# Template for a plot chunk 
template <- "<<plot-chunk-{{i}}, fig.height = {{ph}}, fig.cap = '{{pc}}', echo = FALSE>>= 
    hist(rnorm(100)) 
@" 

# Knit the chunk and cat() the results using knit, knit_expand 
knitfun <- function(i) { 
    knit_expand(text = template, i = i, ph = heights[i], pc = captions[i]) %>% 
    knit(text = ., quiet = T) %>% 
    cat() 
} 

invisible(lapply(1:2, knitfun)) 


@ 

\end{document} 

私は同様の質問へのthis応答にこの回答を基づかよ。

関連する問題