Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
66 lines (49 sloc) 2.11 KB
\usepackage{float} % for fig.pos='H'
\usepackage{rotfloat} % for sidewaysfigure
%\usepackage{subfig} % for subfigure
\usepackage{subcaption} % an alternative package for sub figures
\newcommand{\subfloat}[2][need a sub-caption]{\subcaptionbox{#1}{#2}}
\title{Graphics Options in knitr}
\author{Yihui Xie}
<<setup, include=FALSE>>=
opts_chunk$set(fig.width=4, fig.height=3, par=TRUE, out.width='2in', fig.pos='H')
knit_hooks$set(par = function(before, options, envir) {
if (before) par(mar = c(4, 4, .1, .1))
These options are vectorized for multiple plots per chunk: \Sexpr{knitr:::.recyle.opts}.
When the plot hook is called, the i-th elements of these options will be
applied to the i-th plot.
Figure captions. Look at Figure \ref{fig:fig-cap1} and \ref{fig:fig-cap2}.
<<fig-cap, fig.cap=c('One plot.', 'Another plot.')>>=
par(bg=rgb(runif(1), runif(1), runif(1)))
plot(rnorm(10), pch=19)
Different out.width and out.height.
<<fig-out, ref.label='fig-cap', out.width=c('1in', '.4\\linewidth')>>=
Different out.extra (rotate by different degrees).
<<fig-extra, ref.label='fig-cap', out.extra=sprintf('angle=%d', c(30,120))>>=
Short options will be recycled: out.width below is of length 1 but there are
two plots.
<<fig-width, ref.label='fig-cap', out.width='1in'>>=
Use subfigures for each plot. Look at Figure \ref{fig:fig-sub1} and \ref
{fig:fig-sub2}. By default, knitr uses the \textbf{subfig} package, but it
has a few shortcomings, so some people prefer the \textbf{subcaption}
package. In that case, we can simply redefine the \verb|\subfloat| command;
see the preamble of this document for an example. We are actually using the
\textbf{subcaption} package in this document.
<<fig-sub, ref.label='fig-cap', fig.cap='two plots', fig.subcap=c('one plot', 'the other one'), out.width='.49\\linewidth'>>=
The second figure uses sidewaysfigure (i.e. Figure \ref{fig:fig-margin2}).
<<fig-margin, ref.label='fig-cap', fig.cap=c('ordinary figure', 'sideways figure'), fig.env=c('figure', 'sidewaysfigure')>>=