Francesco Sarracino
2013-Jan-08 10:17 UTC
[R] how to label two figures in the same chunk independently with knitr
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? Thanks in advance for your kind help, f. -- Francesco Sarracino, Ph.D. https://sites.google.com/site/fsarracino/ [[alternative HTML version deleted]]
Ulises M. Alvarez
2013-Jan-08 13:46 UTC
[R] how to label two figures in the same chunk independently with knitr
On 01/08/2013 04:17 AM, Francesco Sarracino 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.Hi: You may use, main = " ", within each plot; or take a look at the LaTeX package, subcaption: http://en.wikibooks.org/wiki/LaTeX/Floats,_Figures_and_Captions#Subfloats -- Ulises M. Alvarez http://sophie.unam.mx/
Yihui Xie
2013-Jan-08 17:03 UTC
[R] how to label two figures in the same chunk independently with knitr
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? > Thanks in advance for your kind help, > f. > > > -- > Francesco Sarracino, Ph.D. > https://sites.google.com/site/fsarracino/ > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.