2013-07-31 10 views
51

デフォルトではスライドに収まらず、他の手段でも印刷されません。R markdown、knitr、pandoc、beamerでのプロットサイズと解像度

ここは.Rmd:です。編集:すべてのチャンクでplot()を使用する必要があるようです。 2番目のプロットが印刷されます。 test.Rmd として次にビーマーを使用してのtexするためにコンパイルすることを

# Plot should show at high resolution 

```{r echo=FALSE, comment = ""} 
# load some data 
require(plyr) 
rbi <- ddply(baseball, .(year), summarise, 
    mean_rbi = mean(rbi, na.rm = TRUE)) 
``` 

```{r} 
# plot 
plot(mean_rbi ~ year, type = "l", data = rbi) 
``` 

# Second attempt 
```{r, fig.width = 2, fig.height = 2} 
plot(mean_rbi ~ year, type = "l", data = rbi) 
``` 

# Third attempt 
```{r, out.width = 2, out.height = 2} 
plot(mean_rbi ~ year, type = "l", data = rbi) 
``` 

# Fourth attempt 
```{r, out.width = '200px', out.height = '200px'} 
plot(mean_rbi ~ year, type = "l", data = rbi) 
``` 

# Fifth attempt 
```{r, out.width = '\\maxwidth'} 
plot(mean_rbi ~ year, type = "l", data = rbi) 
``` 

割引:

knit("test.Rmd") 
system("pandoc -s -t beamer --slide-level 1 test.md -o test.tex") 

RStudioで開くtest.texや "PDFをコンパイル" をクリックします。

私はYihuiのdocumentationを読んで、本当に明白な何かを見逃していないことを願っています。

編集 Yihuiの提案を組み込んだ新しいコード。

```{r setup, include=FALSE} 
opts_chunk$set(dev = 'pdf') 
``` 

# Plot should show at high resolution 

```{r echo=FALSE, comment = ""} 
# load some data 
require(plyr) 
rbi <- ddply(baseball, .(year), summarise, 
    mean_rbi = mean(rbi, na.rm = TRUE)) 
``` 

```{r} 
# plot 
plot(mean_rbi ~ year, type = "l", data = rbi) 
``` 

# Second attempt 
```{r, fig.width = 4, fig.height = 4} 
plot(mean_rbi ~ year, type = "l", data = rbi) 
``` 

sessionInfo()

R version 3.0.1 (16/05/2013) 
Platform: x86_64-pc-linux-gnu (64-bit) 

Local: 
[1] LC_CTYPE = en_US.UTF-8 LC_NUMERIC = C LC_TIME = C LC_COLLATE = C   
[5] LC_MONETARY=C  LC_MESSAGES=C  LC_PAPER=C   LC_NAME=C   
[9] LC_ADDRESS=C   LC_TELEPHONE=C  LC_MEASUREMENT=C  LC_IDENTIFICATION=C 

attached base packages: 
[1] stats  graphics grDevices utils  datasets methods base  

other attached packages: 
[1] plyr_1.8  markdown_0.6 knitr_1.2  rCharts_0.3.51 slidify_0.3.52 

loaded via a namespace (and not attached): 
[1] RJSONIO_1.0-3 codetools_0.2-8 digest_0.6.3 evaluate_0.4.3 formatR_0.8  
[6] grid_3.0.1  lattice_0.20-15 stringr_0.6.2 tools_3.0.1  whisker_0.3-2 
[11] yaml_2.1.7 
+1

私はあなたの投稿を編集し、 'print(x)'を削除しました。それは意味がなく、誤解を招く恐れがあるからです。気を悪くしないで下さい。 –

答えて

36

私はPandocとマークダウンから生成ビーマースライドの数字の行動についてよく聞かれる質問だと思います。本当の問題は、R MarkdownがデフォルトでPNG画像を生成することです(knitrから)、LaTeXでPNG画像のサイズをデフォルト値にするのは難しいです(理由はわかりません)。しかし、PDF画像のサイズを正確にするのはかなり簡単です。一つの解決策は、あなたの最初のチャンクにPDFにデフォルトのグラフィックデバイスをリセットすることです:

```{r setup, include=FALSE} 
opts_chunk$set(dev = 'pdf') 
``` 

その後、すべての画像は、PDFファイルとして書き込まれ、LaTeXのは幸せになります。

2番目の問題は、LaTeX単位をHTML単位と混ぜ合わせてout.width/out.heightということです。 LaTeXとHTMLは非常に異なる技術です。 \maxwidthがHTMLで動作するとは限りません.LaTeXでは200pxとなります。特にMarkdownをLaTeXに変換する場合は、out.width/out.heightfig.width/fig.heightを使用し、LaTeXに元のサイズを使用させてください)を設定しない方がよいでしょう。

+0

それは助けますが、それはまだかなりです。最初のプロットは依然として巨大です。 2番目のプロットは小さく、非常に低い解像度です。私はコードを[類似の質問](http://stackoverflow.com/questions/13004001)から.texファイル(編み込み後)に直接追加しようとしました。これは幅を制御しますが、それでもスライドの底から落ちます。ベースグラフィックスを 'print()'して申し訳ありません。私はUbuntuセッション情報を使用しています。 – nacnudus

+5

@nacnudusもう一度knitrを実行する前に 'figure'ディレクトリを削除してください。 LaTeXについての多くの暗い事実があります。デフォルトではPDFよりPNGを優先しているので、 'foo.png'と' foo.pdf'があれば '\ includegraphics {foo}'は 'foo.png'を使用します –

15

図サイズはインチで指定し、ドキュメント出力形式のグローバルオプションとして含めることができます。あなたが直接プロットのサイズを定義するout.widthout.height引数を使用することができます

```{r, fig.width=14, fig.height=12}   #Expand the plot width to 14 inches 

ggplot(aes(x=mycolumn1, y=mycolumn2)) +  #specify the x and y aesthetic 
geom_line(size=2) +       #makes the line thicker 
theme_grey(base_size = 25)     #increases the size of the font 
``` 

:例えば:

--- 
title: "My Document" 
output: 
    html_document: 
    fig_width: 6 
    fig_height: 4 
--- 

とグラフィックデバイスのプロットのサイズは、チャンクレベルで増加させることができます

```{r, out.width="200px", out.height="200px"} #Expand the plot width to 200 pixels 

ggplot(aes(x=mycolumn1, y=mycolumn2)) +  #specify the x and y aesthetic 
geom_line(size=2) +       #makes the line thicker 
theme_grey(base_size = 25)     #increases the size of the font 
``` 
+0

チャンクレベル" fig.width "私のために働かない。 – theforestecologist

+0

@ theforestecologistおそらく '{r chunk-name、opt = val、opt = val2}'のように最初のオプションの前にコンマが必要でしょう。その方法は私のために働く。私はそれを編集するのに十分な知識しか持っていません。 – Frank

関連する問題