43

.Rmd ファイルがあり、関数 pandoc を使用して .docx ファイルを作成しようとしています。

最終的な解像度が 504x504 ピクセル (つまり、72dpi の 7x7 インチ) の図が必要です。残念ながら、デフォルトの 72 dpi は品質が低すぎるため、最終的な解像度を変更せずに 150 dpi に増やしたいと考えています (したがって、.docx ファイル内で既に正しいサイズになっています)。オプションの fig.width と fig.height=7 を保持し、dpi=150 に設定すると、必要な品質が得られますが、最終的な解像度が上がり、図が .docx マージンからはみ出します。引数 out.width と out.height で遊んでみましたが、それらを含めると、最終的な .docx には何もプロットされません。

アイデア?

.Rmd コードの例:

My title
-------------------------

*(this report was produced on: `r as.character(Sys.Date())`)*  

That's my plot

```{r echo=FALSE}
    plot(0,0,type="n",xlim=c(0,500), ylim=c(-12,0), las=1)
    color  <-  rainbow(500)
    text(380,-1,"Test",pos=4)
    lseq   <-  seq(-6,-2,length.out=500)
    for(j in seq_along(lseq)) {
        lines(c(400,450), rep(lseq[j], 2), col=color[j])
    }
    polygon(c(400,450,450,400), c(-6,-6,-2,-2), lwd=1.2)
```

.docx への変換

library(knitr)
library(markdown)
knit("example.Rmd")  # produces the md file
pandoc("example.md", format = "docx") #prodces the .docx file

フィギュアを再スケーリングしようとしても、うまくいきません。下:

My title
-------------------------

*(this report was produced on: `r as.character(Sys.Date())`)*  

That's my plot

```{r echo=FALSE, dpi=150, fig.width=7, fig.height=7, out.width=504, out.height=504}
    plot(0,0,type="n",xlim=c(0,500), ylim=c(-12,0), las=1)
    color  <-  rainbow(500)
    text(380,-1,"Test",pos=4)
    lseq   <-  seq(-6,-2,length.out=500)
    for(j in seq_along(lseq)) {
        lines(c(400,450), rep(lseq[j], 2), col=color[j])
    }
    polygon(c(400,450,450,400), c(-6,-6,-2,-2), lwd=1.2)
```
4

3 に答える 3

31

この質問がされてから、ソフトウェアが改善された可能性が最も高いです。プロットの解像度を上げる方法を探して、この質問にたどり着きました。OPの元のアプローチは、すぐに使えることがわかりました。

そのため、チャンクのパラメーターを設定するとdpi=300(dpi=150十分に明確な違いが得られなかったため)、Word 内の画像の物理サイズを変更することなく、はるかに高品質の画像が生成されました。

```{r, echo=FALSE, dpi=300, fig.width=7, fig.height=7}
plot(0,0,type="n",xlim=c(0,500), ylim=c(-12,0), las=1)
color  <-  rainbow(500)
text(380,-1,"Test",pos=4)
lseq   <-  seq(-6,-2,length.out=500)
for(j in seq_along(lseq)) {
    lines(c(400,450), rep(lseq[j], 2), col=color[j])
}
polygon(c(400,450,450,400), c(-6,-6,-2,-2), lwd=1.2)
```

ただし、「fig.align、out.width、out.height、out.extra は Word 出力ではサポートされていません」という警告とともに、イメージの生成を完全に設定out.widthおよび削除します。out.height

于 2016-03-14T04:27:05.300 に答える
13

これは、knitr に組み込まれている出力タイプの動的カスタマイズ機能を利用する絶好の機会です。これは、両方の出力ターゲットでテストされました...

````{r img-setup, include=FALSE, cache=FALSE}
out.format <- knitr::opts_knit$get("out.format")
img_template <- switch( out.format,
                     word = list("img-params"=list(fig.width=6,
                                                   fig.height=6,
                                                   dpi=150)),
                     {
                       # default
                       list("img-params"=list( dpi=150,
                                               fig.width=6,
                                               fig.height=6,
                                               out.width="504px",
                                               out.height="504px"))
                     } )

knitr::opts_template$set( img_template )
````

生成されるすべての画像に img_template を使用したくない場合は、set 関数を呼び出さずに、使用するチャンクのパラメーターに追加opts.label="img_template"するか、チャンクのパラメーターを明示的に指定して img_template をオーバーライドできます。 .

于 2014-12-05T17:20:27.583 に答える