2013-07-31 4 views
51

기본적으로 슬라이드에 맞지 않으며 다른 방법으로도 인쇄하지 않습니다.R markdown, knitr, pandoc, beamer로 플롯 크기 및 해상도

다음은 .Rmd : 입니다. 편집 : 모든 청크에서 plot()을 사용해야합니다. 두 번째 줄이 이제 인쇄됩니다. RStudio에서

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

열기 test.tex 클릭 "PDF를 컴파일"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) 
``` 

저장.

나는 Yihui의 documentation을 읽었으며 정말 확실한 것을 놓치지 않았 으면 좋겠습니다.

편집 Yihui의 제안을 통합 한 새로운 코드.

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

sessionInfo()

```{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) 
``` 
는 그가 자주 Pandoc 및 가격 인하에서 생산 빔 프로젝터 슬라이드에서 인물의 행동에 대해 질문을 생각합니다. 진짜 문제는 R Markdown이 기본적으로 PNG 이미지를 생성한다는 것입니다 ( knitr). 기본적으로 LaTeX에서 PNG 이미지의 크기를 올바르게 계산하기는 어렵습니다 (이유를 모르겠습니다). 그러나 PDF 이미지의 크기를 올바르게 맞추는 것은 상당히 쉽습니다.
```{r setup, include=FALSE} 
opts_chunk$set(dev = 'pdf') 
``` 

그런 다음 모든 이미지는 PDF 파일로 작성되며, 라텍스 행복 할 것이다 : 하나의 해결책은 첫 번째 덩어리에서 PDF에 기본 그래픽 장치를 다시 설정하는 것입니다.

두 번째 문제는 out.width/out.height의 LaTeX 단위로 HTML 단위를 혼합하는 것입니다. LaTeX와 HTML은 매우 다른 기술입니다. \maxwidth은 HTML로, 또는 200px은 LaTeX에서 작동하지 않아야합니다. 특히 Markdown을 LaTeX로 변환하려면 out.width/out.height (fig.width/fig.height을 사용하고 LaTeX에서 원래 크기로 사용하도록 설정하는 것이 좋습니다).

+0

그게 도움이 되긴하지만 아직은 아니야. 첫 번째 줄거리는 여전히 거대합니다. 두 번째 플롯은 작지만 매우 낮은 해상도입니다. 나는 (이 뜨개질을 한 후에) .tex 파일에 [이 유사한 질문] (http://stackoverflow.com/questions/13004001) 코드를 추가하려고했습니다. 그 너비를 제어하지만, 그것은 여전히 ​​슬라이드의 바닥에서 떨어진다.기본 그래픽을'print()'하는 것에 대해 미안합니다. 나는 우분투 - 세션 정보를 사용하고 있습니다. – nacnudus

+5

@nacnudus knitr을 다시 실행하기 전에'figure' 디렉토리를 제거하는 것이 좋습니다. LaTeX에 관한 많은 어두운 사실들이 있습니다. 그것은 기본적으로 PDF보다 PNG를 선호하므로'foo.png'와'foo.pdf'가 있다면'\ includegraphics {foo}'는'foo.png'를 사용합니다 –

15

그림 크기는 인치 단위로 지정되며 문서 출력 형식의 전역 옵션으로 포함될 수 있습니다. 예를 들면 :

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

그리고 그래픽 장치에서의 플롯의 크기는 청크 수준에서 증가 할 수 있습니다

```{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 
``` 

당신은 직접 플롯의 크기를 정의 할 out.widthout.height 인수를 사용할 수 있습니다 출력 파일에서 :

```{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

@theforestologologist 아마도'{r chunk-name, opt = val, opt = val2} '와 같은 첫 번째 옵션 앞에 쉼표가 필요할 것입니다. 그 방법은 나를 위해 작동합니다. 나는 그걸 편집 할 정도로 짜증이 나지 않습니다. – Frank

관련 문제