Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

r - knitr .Rmd -> Word document: control details of figures

I'm producing a solutions manual for a book, using .Rmd files with the following YAML header:

---
title: "DDAR: Solutions and Hints for Exercises"
date: "`r Sys.Date()`"
output: 
  word_document:
    reference_docx: solutions-setup.docx
---

where I control the general layout of the document with the reference_docx to get an output Word document.

There will be many figures, and I'd like to set some global graphics parameters to give relatively tight bounding boxes and reasonable font sizes in the figures without having to tweak each one from what I see in a PDF document.

I tried the following, but the par() setting doesn't seem to have any effect:

{r setup, echo=FALSE} options(digits=4) par(mar=c(5,4,1,1)+.1)

Instead I get images like the following in my document with larger bounding boxes than I would like and with much larger font sizes than I would like.

sample image

I know how to control all this in .Rnw files produced with LaTeX, but I can't find how to do it in .Rmd -> Word. Is there a chunk hook I could use? I don't think that there is an out.width chunk option that re-scales a figure as in LaTeX.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

@scoa's answer shows how to use a hook to set some graphical parameters at the beginning of each chunk. This is necessary because "by default, knitr opens a new graphics device to record plots and close it after evaluating the code, so par() settings will be discarded", i.e. graphical parameters for later chunks cannot be set in an early setup-chunk but need to be set for each chunk separately.

If this behavior is not wanted, the package option global.par = TRUE can be used:

opts_knit$set(global.par = TRUE)

Finding the correct values for the margins is sometimes quite painful. In these cases, hook_pdfcrop can help. In all chunks where the option crop = TRUE, white margins will be removed. To apply this to all chunks, use

library(knitr)
knit_hooks$set(crop = hook_pdfcrop)
opts_chunk$set(crop = TRUE)

This works for docx output as well because "when the plot format is not PDF (e.g. PNG), the program convert in ImageMagick is used to trim the white margins" (from ?hook_pdfcrop).

Note that under some circumstances, cropping plots has the side effect of sometimes apparently different "zoom" factors of plots: This happens in cases where we start with identical sized elements on two plots but larger white margins around one of the plots. If then both are resized to a fixed output width after cropping, elements on the plot with larger margins look larger. However, this is not relevant for docx output because out.width/out.height cannot be used in that case.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...