0
\begin{figure}[h]
\centering

<<echo=FALSE>>=

for(i in 2:38){
print(my_plot_function(i))
}

@

\end{figure}

This is my code, but what is happening is that when I compile my PDF I only get the first two plots that fit on the first page and I do not get the rest of the plots. I would like to have all of the plots on separate pages.

And how would I go about adding captions to each individual plot in the for loop.

Dylan Dijk
  • 277
  • 1
  • 6
  • 1
    The title of this Q does not (fully) match its contents. Anyway - Why are you forcing all plots into one `figure` environment? And if you want your plot "`[h]`ere", why are you using a `figure` at all? – CL. Jun 23 '20 at 09:30
  • sorry just learning at the moment, any help would be useful. – Dylan Dijk Jun 23 '20 at 09:31
  • No worries, we're all learning. ;-) But what if you just delete the first two and the last line? – CL. Jun 23 '20 at 09:33
  • Is this part of a larger document with additional text and so on? Or do you just want to export figures to PDF? – milanmlft Jun 23 '20 at 09:39
  • Then the title of my PDF comes right at the end, but the plots are on separate pages which is nice. So I just somehow need to retain the structure. – Dylan Dijk Jun 23 '20 at 09:41
  • 2
    @DylanDijk Can you try to provide a [reproducible example](https://stackoverflow.com/q/5963269/2706569) with some plots and a title? – CL. Jun 23 '20 at 09:42
  • @milanmlft Yes I want to have additional text. I can get the plots on separate pages if I follow CL instructions of deleting the first two and last line, but the plots come before the title. – Dylan Dijk Jun 23 '20 at 09:44
  • @CL. yes, ill make one now. – Dylan Dijk Jun 23 '20 at 09:47
  • Do your plots need a `\caption{...}`? – samcarter_is_at_topanswers.xyz Jun 23 '20 at 09:48
  • No I don't, I have a title for each which is sufficient. But just want the structure of the PDF to be retained. – Dylan Dijk Jun 23 '20 at 09:59
  • I understand what I did wrong now, I positioned `\maketitle` incorrectly. Very stupid from me, sorry for wasting your guys time. I really appreciate the help. – Dylan Dijk Jun 23 '20 at 10:05
  • So it now works if I remove `\begin{figure}` and `\end{figure}`. But if I wanted to organise the layout of the plots how do I go about doing that. For example if I wanted the first plot on a different page to the title, can I simply just insert a page break. – Dylan Dijk Jun 23 '20 at 10:10
  • `\clearpage` should do the trick. – CL. Jun 23 '20 at 11:13
  • @samcarter_is_at_topanswers.xyz if I wanted to have captions for plots made within the loop how would I do this. Is there a way to have latex code within the chunk. – Dylan Dijk Jun 24 '20 at 13:36
  • @DylanDijk Not tested, as there is no [mre] in the question, but you could probably just move the `figure` environment inside the loop (with `\begin{figure}[p]` to display them on separate pages) and then use `\caption{...}` – samcarter_is_at_topanswers.xyz Jun 24 '20 at 13:39
  • `<>= for(i in 2:38){ \begin{figure}[p] print(my_scatter_plot_function(i)) print(my_box_plot_pergroup_function(i)) } @` I tried this but it does not recognise \. I do not understand how you can enter latex code into an R loop. – Dylan Dijk Jun 24 '20 at 13:53
  • 1
    @DylanDijk I think I have an answer for you. But for the future, please try to provide a [reproducible example](https://stackoverflow.com/q/5963269/2706569) and *do not* change the question after you posted it. (You are welcome to *refine* your question, but do not turn it into *another* question.) – CL. Jun 24 '20 at 20:00

1 Answers1

2
\documentclass{article}
\begin{document}

<<echo = FALSE, fig.cap = c("First, Second, Third"), results = "asis">>=
  library(ggplot2)
  for (i in 1:3) {
    print(qplot(x = 1, y = i))
    cat("Arbitrary \\LaTeX code! \\clearpage")
  }
@
  
\end{document}

You can use the chunk option fig.cap to add captions to your plots. Note that this will wrap your figure in a figure environment, turning it into a float.

Use the chunk option results="asis" to be able to print arbitrary text (including LaTeX markup) into your document using cat().

CL.
  • 14,577
  • 5
  • 46
  • 73