knitr code chunk option fig.keep = \" all \"不会产生统一的边距

时间:2017-08-29 22:30:01

标签: r knitr r-markdown

我注意到当一个块使用fig.keep="all"产生多个图形时,边距并不完全相同。 result pdf document证明了这一现象。我看到的问题是偶数和奇数图像具有不同的边距。输出中的图片:

fourfold grid with 3 inch images

我找到了similar issue with graphs in for loops并且没有答案。

这是MRE。我是否错过了knitr指令中的内容?我经常这样做。如果输出为pdf或png,我会注意到相同的保证金变化。

--- 
title: "knitr fig.keep=\"all\" does not produce uniform margins"
author: Paul Johnson
output:
  pdf_document:
    fig_caption: yes
    highlight: haddock
    keep_tex: no
    latex_engine: pdflatex
    pandoc_args: --listings
    toc: yes
    toc_depth: 2
header-includes:
- \usepackage{listings}
- \usepackage{xcolor}
- \usepackage{amsmath}
- \usepackage{amssymb}
- \usepackage{fancybox}
- \usepackage{calc}
- \usepackage{subfig}
tables: yes
keywords: guides, report, pandoc, sweave, knitr
---


```{r setup, include=FALSE}
##This Invisible Chunk is required in all CRMDA documents
outdir <- paste0("tmpout")
if (!file.exists(outdir)) dir.create(outdir, recursive=TRUE)
knitr::opts_chunk$set(echo=TRUE, comment=NA,
                      fig.path=paste0(outdir, "/p-"))
options(width=70)
```

This does the usual R thing of creating a blank plot and then writing on it.

```{r chunk71, eval=F, echo=TRUE, warning=FALSE, collapse=TRUE}
plot(c(0, 1), c(0, 1), xlim=c(0,1), ylim=c(0,1), type="n", ann=FALSE, axes=F)
rect(0, 0, 1, 1, col="light grey", border="grey")
axis(1, tck=0.01, pos=0, cex.axis=0.6, padj=-3, lwd=0.8,
     at=seq(0, 1, by=0.2), labels=c("", seq(0.2,0.8, by=0.2), ""))
axis(2, tck=0.01, pos=0, cex.axis=0.6, padj=3, lwd=0.3,
     at=seq(0, 1, by=0.2), labels=c("", seq(0.2,0.8, by=0.2), ""))
mtext(expression(x), side=1, line=0.5, at=.6, cex=.6)
mtext(expression(y), side=2, line=0.5, at=.6, cex=.6)
mtext(c("Min x", "Max x"), side=1, line=-0.5, at=c(0.05, 0.95), cex=.6)
mtext(c("Min y", "Max y"), side=2, line=-0.5, at=c(0.05, 0.95), cex=.6)
lines(c(.6, .6, 0), c(0, .6, .6), lty="dashed")
text(.6, .6, expression(paste("The location ",
                              group("(",list(x[i] == .6, y[i] == .6),")"))), pos=3, cex=.7)
points(.6, .6, pch=16)
```

We might be in teaching mode and we need to demonstrate the
effect of each successive R function in the process of creating the
figure. I ask for each intermediate plot by inserting fig.keep="all""

\begin{lstlisting}
`r ''````{r chunk76, ref.label='chunk71', echo=F, fig=T, include=F, fig.keep="all", collapse=T, fig.width=4, fig.height=4}
`r ''````
\end{lstlisting}

```{r chunk76, ref.label='chunk71', echo=F, fig=T, include=F, fig.keep="all", collapse=T, fig.width=4, fig.height=4, dev=c('pdf', 'png')}
```

A quick check of the tmpout directory shows that this code created several
graphs. Observe:

```{r chunk76b}
list.files("tmpout", pattern="p-chunk76.*pdf")
```

The problem I see is that the output figures are not uniform in their margins. Some have the large white space, some do not. I need to remedy this.

The margins are large on even numbered graphs and small on odd numbered. 

\begin{figure}

\caption{Four snapshots not all same margins}

\subfloat[graph 4]{
\includegraphics[width=3in]{tmpout/p-chunk76-4.pdf}
} \subfloat[graph 8]{
\includegraphics[width=3in]{tmpout/p-chunk76-8.pdf}
}

\subfloat[graph 9]{
\includegraphics[width=3in]{tmpout/p-chunk76-9.pdf}
}\subfloat[graph 11]{
\includegraphics[width=3in]{tmpout/p-chunk76-11.pdf}
}
\end{figure}




\begin{figure}

\caption{Checking the odd-even pattern}

\subfloat[graph 3]{
\includegraphics[width=3in]{tmpout/p-chunk76-3.pdf}
} \subfloat[graph 8]{
\includegraphics[width=3in]{tmpout/p-chunk76-8.pdf}
}

\subfloat[graph 10]{
\includegraphics[width=3in]{tmpout/p-chunk76-10.pdf}
}\subfloat[graph 11]{
\includegraphics[width=3in]{tmpout/p-chunk76-11.pdf}
}
\end{figure}


# Session Info

```{r session}
warnings()
sessionInfo()
```

Available under
[Created Commons license 3.0 <img src="http://crmda.dept.ku.edu/images/cc-3.0.png" alt="CC BY"
style="width: 75px;height: 20px;"/>](http://creativecommons.org/licenses/by/3.0/)

0 个答案:

没有答案