14
votes

I am trying to troubleshoot a noticeable difference in font size when comparing plots generated for a knitr document. HTML/markdown output appears to be what I would consider the correct size while the PDF/Latex output is approximately 2-4 pts larger (e.g., if I was expecting 12pt, the output is closer 14 or 16 pts). Note the issue seems to be limited to the plot/figure text and doesn't seem to manifest with other text in the output document.

The simplest way for me to demonstrate this is with the default content that RStudio (version 0.99.329) populates when a new *.Rmd file is created. A side-by-side comparison is available via this link. To my eye, the HTML version is properly sized and the PDF/Latex version is not.

To my knowledge, I don't have any custom settings within my .Rprofile or other locations that could be impacting this issue. I have tried disabling/enabling pdf crop and that does not seem to impact the issue either. I have duplicated the issue on a colleagues machine (also running OS X and the same version of RStudio).

Outside of the rmarkdown framework (i.e., just running R code) there does not appear to be such a difference between PDF and PNG output. For example, the following code produces output that is fairly similar:

library(ggplot2)
r <- ggplot(data = diamonds, 
    aes(x = carat, y = price, color = cut, group = cut))
r + geom_smooth(size = 2) + 
    ggtitle("Plant growth with\ndifferent treatments")

ggsave(file="test.pdf")
ggsave(file="test.png")

Given that this code above produces output plots that are fairly similar, I'm suspicious the issues I'm seeing are related to knitr or the rmarkdown/pandoc conversion process.

So, my main question is whether the level of differences I'm seeing between output formats is expected? Am I the only one? Is it unique to my system?

If it is expected behavior, how are folks reconciling the issue? For ggplots, I've been using theme_bw(8.5) to downscale the fonts for PDF/latex output. This works, but it adds another level of complexity when creating output for multiple platforms/uses from the same *.Rmd file ... one of the key benefits of rmarkdown.

Specifications of my setup and system

  • OS X 10.10.2, MacBook Pro (Retina, 15-inch, Early 2013)
  • RStudio Version 0.99.329
  • R version 3.1.2 (2014-10-31) -- "Pumpkin Helmet"
  • rmarkdown 0.5.1
  • pandoc 1.13.1
1
I have the same Issue. I think it has something to do with the different standard graphic devices for PDF and HTML-files ('dev: ('pdf' for LaTeX output and 'png' for HTML/markdown; character)', yihui.name/knitr/options ). If I manually set dev=pdf for html-files, it cannot be displayed any more, but the generated file shows the same font size as with pdf generation. If I manually set dev='png' for pdf-files, I get the error: unused argument (pdf=list(useDingbats=FALSE), whicht might have something to do with the dev.args-chunk-option. Here I think might also be the solution...Julian

1 Answers

3
votes

Have you tried this solution, which sets different output devices for different output formats? Here it is:

Solution to producing pdf and html output from a unique Markdown file by setting specific options to knitr in the Makefile:

$(PDF): $(SRC) Makefile
Rscript \
  -e "library(knitr)" \
  -e "opts_chunk[['set']](dev = 'pdf')" \
  -e "pat_gfm()" \
  -e "knit('$<', 'temp.md')"
$(PANDOC) temp.md -o $@
rm temp.md

This answer also has interesting ideas.

Here it is:

Try putting this code chunk at the beginning of the Rmd document.

```{r setup, cache=FALSE, include=FALSE}
library(knitr)
output <- opts_knit$get("rmarkdown.pandoc.to")
if (output=="html") opts_chunk$set(fig.width=11, fig.height=11)
if (output=="docx") opts_chunk$set(fig.width=6,  fig.height=6)
```

One of the package options returned by opts_knit$get() is markdown.pandoc.to. This is evidently set to "html", "docx", or "latex" depending on the chosen output format (HTML, Word, or PDF). So you can test that and set the chunk options fig.width and fig.height accordingly.