[R] how to label two figures in the same chunk independently with knitr

Yihui Xie xie at yihui.name
Tue Jan 8 18:03:14 CET 2013

All you mentioned are possible; knitr has very comprehensive support
to figures in LaTeX, and what you want in this case is subfigures
(\usepackage{subfig}); here is an example:
https://github.com/yihui/knitr-examples/blob/master/067-graphics-options.Rnw
(search for 'fig.subcap' for the relevant chunk)

And here is a preview: http://i.imgur.com/4lKpw.png

Regards,
Yihui
--
Yihui Xie <xieyihui at gmail.com>
Phone: 515-294-2465 Web: http://yihui.name
Department of Statistics, Iowa State University
2215 Snedecor Hall, Ames, IA

On Tue, Jan 8, 2013 at 4:17 AM, Francesco Sarracino
<f.sarracino at gmail.com> wrote:
> Dear R helpers,
>
> I am using knitr to run analysis with R and edit my document with Latex. I
> am wondering whether there is a way to include 2 or more pictures per chunk
> and being able to refer them in the text independently and eventually
> whether it is possible to give them different captions. Let me give you an
> example.Rnw:
>
> \documentclass{article}
> \title{Example}
> \author{FS}
> \begin{document}
> \maketitle
>
> I put some text here. I want  to plot to charts in the same figure and
> label them independently.
> <<stat, echo = FALSE, results = 'hide'>>=
> ii <- 2000:2011
> xx <- rnorm(12,0,1)
> yy <- rnorm(12,0,1)
> pm <- data.frame(ii,zz,yy
> @
>
> Now I generate the two pictures and put them into the same chunk with the
> option out.width set to .49 so that knitr places the two charts side by
> side:
> <<fig:example, echo = FALSE, out.width=".49\\linewidth", fig.cap="this is
> an example>>=
> plot(ii,xx, type = "l")
> plot(ii,yy, type = "l", lty = 2)
> @
>
> Finally, I want the reader to look at the figure on the left.
> \end{document}
>
> How can I do this? If I refer to  \ref{fig:example} I will get the number
> of the figure, but of the chart on the left.
> Eventually, is it possible to have separate captions for each chart?
> f.
>
>
> --
> Francesco Sarracino, Ph.D.
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help