# Caption in Sweave after an R plot

I have the following R chunk:

<<echo=FALSE>>=
par(mfrow=c(3,1))

#cas basal: no negra no fumadora sense prematurs ni hipertensi?
lwt.pred<-seq(80,250,1)
prob.pred<-0+model.final$coefficients[3]*lwt.pred prob.pred.log<-exp(prob.pred)/(1+exp(prob.pred)) plot(lwt.pred,prob.pred.log,type="l",ylim=c(0,1),ylab="Prob. infrapès",xlab="Pes de la mare",main="Ètnia") #cas negra prob.pred2<-model.final$coefficients[3]*lwt.pred+model.final$coefficients[5] prob.pred.log2<-exp(prob.pred2)/(1+exp(prob.pred2)) lines(lwt.pred,prob.pred.log2,type="l",col=3) plot(lwt.pred,prob.pred.log,type="l",ylim=c(0,1),ylab="Prob. infrapès",xlab="Pes de la mare",main="Parts prematurs") #cas parts prematurs prob.pred3<-model.final$coefficients[3]*lwt.pred+model.final$coefficients[2] prob.pred.log3<-exp(prob.pred3)/(1+exp(prob.pred3)) lines(lwt.pred,prob.pred.log3,type="l",col=2) plot(lwt.pred,prob.pred.log,type="l",ylim=c(0,1),ylab="Prob. infrapès",xlab="Pes de la mare",main="Hipertensió") #hipertensos prob.pred4<-model.final$coefficients[3]*lwt.pred+model.final\$coefficients[4]
prob.pred.log4<-exp(prob.pred4)/(1+exp(prob.pred4))
lines(lwt.pred,prob.pred.log4,type="l",col=6)

par=c(1,1)
@


Which outputs three plots, and I want to insert one single image caption under it. I don't know if I should do this inside the R chunk (using the xtable(,caption=) function) or in LaTeX (using \caption{}). However I think the former needs a given element with a name and so on while the latter must be inserted within a {figure} fragment. How can I solve this?

• In this form, form is this question relevant to LaTeX? It might be an idea to share the LaTeX code generated, and then ask your LaTeX specific question – daleif Dec 14 '18 at 14:12
• @zest16 Plese, next time post a small a but complete Sweave document and not only the R chunk. Please also make the R chunk as simple as possible and compilable as is, without forcing the staff to decipher the R language to find what is missing and what you intend to do exactly. Note also that this in this site there are many LaTeX experts, but not everyone use R, so next time at least left some clue that this is to make part of a LaTeX document. – Fran Dec 15 '18 at 18:58

Using knitr:

\documentclass{article}
\begin{document}
<<test,echo=FALSE,fig.cap="Three plots in one figure",fig.height=3>>=
par(mfrow=c(1,3))
x <- rnorm(100, 3, 1)
y <- rnorm(100, 1, 1)
plot(x,0.05*x^2)
plot(x,log1p(x^3))
plot(log1p(x),log1p(x^2+y))
@
\end{document}


Using Sweave (not recommended):

\documentclass{article}
\begin{document}
\begin{figure}
\centering
<<test,echo=FALSE,fig=T,,height=3>>=
par(mfrow=c(1,3))
x <- rnorm(100, 3, 1)
y <- rnorm(100, 1, 1)
plot(x,0.05*x^2)
plot(x,log1p(x^3))
plot(log1p(x),log1p(x^2+y))
@
\caption{Three plots in one figure}
\end{figure}
\end{document}