Plot size and resolution with R markdown, knitr, pandoc, beamer
Asked Answered
M

2

74

Doesn't fit on the slide by default, doesn't even print by any other means.

Here's the .Rmd: Edit: it seems you have to use plot() in every chunk. Second plot now prints.

# 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)
```

Save that as test.Rmd Then compile to tex using beamer:

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

Open test.tex in RStudio and click "Compile PDF".

I've read Yihui's documentation and hope I haven't missed something really obvious.

Edit new code incorporating Yihui's suggestions.

```{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  
Microcurie answered 31/7, 2013 at 7:16 Comment(1)
I edited your post and removed print(x) since it is both meaningless and highly misleading. I hope you do not mind.Highpriced
H
57

I think that is a frequently asked question about the behavior of figures in beamer slides produced from Pandoc and markdown. The real problem is, R Markdown produces PNG images by default (from knitr), and it is hard to get the size of PNG images correct in LaTeX by default (I do not know why). It is fairly easy, however, to get the size of PDF images correct. One solution is to reset the default graphical device to PDF in your first chunk:

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

Then all the images will be written as PDF files, and LaTeX will be happy.

Your second problem is you are mixing up the HTML units with LaTeX units in out.width / out.height. LaTeX and HTML are very different technologies. You should not expect \maxwidth to work in HTML, or 200px in LaTeX. Especially when you want to convert Markdown to LaTeX, you'd better not set out.width / out.height (use fig.width / fig.height and let LaTeX use the original size).

Highpriced answered 31/7, 2013 at 7:49 Comment(4)
That helps, but it's not quite there yet. The first plot is still huge. The second plot is small, but at a very low resolution. I tried adding the code from this similar question straight into the .tex file (after knitting). That controls the width, but it still falls off the bottom of the slide. Sorry for print()ing base graphics. I use Ubuntu - session info coming right up.Microcurie
@Microcurie you'd better remove the figure directory before you run knitr again; there are a lot of dark facts about LaTeX, e.g. it prefers PNG over PDF by default, so if there are a foo.png and a foo.pdf, \includegraphics{foo} will use foo.pngHighpriced
Doesn't work for me ... The plot is still outside of the border of the pdfOverwrite
is there way to save pdf with defined dpi while rendering them in markdown?Winstonwinstonn
C
38

Figure sizes are specified in inches and can be included as a global option of the document output format. For example:

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

And the plot's size in the graphic device can be increased at the chunk level:

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

You can also use the out.width and out.height arguments to directly define the size of the plot in the output file:

```{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
```
Cachucha answered 28/12, 2016 at 23:27 Comment(4)
@theforestecologist Probably need a comma before the first option, like {r chunk-name, opt=val, opt=val2}. That way works for me. I'm not knitr-literate enough to edit that in, though.Cele
The settings in the yml header with fig_width and fig_weight does not seem to work with output: pdf_document. How can this be set?Bookrack
This solution is only for html outputs.Cachucha
out.width and out.height were good options for me. Thanks.Thorrlow

© 2022 - 2024 — McMap. All rights reserved.