I'm very new to Latex, and I'm trying my way on creating a graph with a figure caption.
Now when I try to add fig.cap
in the chunk heading (second chunk) I get the error
Latex error: Not in outer par mode
My code
<<echo = FALSE>>=
source("analysis.R")
repoData <- readRDS("data/repoData.rds")
a4width<- 8.3
a4height<- 11.7
@
\begin{figure}[h]
<<echo = FALSE, fig.width= a4width, fig.height=0.35*a4height>>=
G2(repoData)
@
\end{figure}
## ---- G2 ----
G2 <- function(df) {
# For inflation graph
plot <- ggplot(df, aes(x = Month, y = Percent)) +
geom_line(colour = "firebrick") +
xlab("") +
ylab("Repo rate") +
theme_classic() +
theme(axis.title.y = element_text(vjust = 1))
return(plot)
}
Why does this happen and how can this be resolved?
You should omit the \begin{figure}
(not shown in your MWE) and \end{figure}
from your Sweave file; when you specify fig.cap
they are generated automatically by knitr
(and redundantly, in the case of your MWE, leading to the error).
If you need to specify other LaTeX figure options, see the "Plot" section of the knitr chunk options documentation: in particular, if you want to use position "h", use fig.pos="h"
in your chunk options, as indicated by
fig.pos
: (''; character) a character string for the figure position arrangement to be used in\begin{figure}[fig.pos]