0
votes

I'm working with an .Rnw file, trying to generate a plot. However, when I click Compile PDF, I get a blank pdf with just the figure caption and no plot. It looks like this: enter image description here

Here is the code:

\documentclass{article}

\begin{document}

<<fasfd, fig.height=10, fig.cap="first", fig.pos="t", echo=FALSE, fig.width=10>>=
barplot(table(mtcars$gear))
@

\end{document}

What am I doing wrong?

2
You tag your question with both knitr and sweave. Which one are you using? - Gregor Thomas
FWIW, saving your example to a file named "eg.Rnw" and then doing knitr::knit2pdf("eg.Rnw") gets a document that does display the plot. - Josh O'Brien
I'm using knitr, sorry for the confusion. - ytk
@JoshO'Brien okay that worked. Thanks! Any idea why it didn't work when I just clicked Compile PDF? - ytk
Nope, can't help you there, since that's an issue with an RStudio feature, and I don't tend to use RStudio. - Josh O'Brien

2 Answers

2
votes

Sweave does not provide so many options as knitr. To include plots with sweave you need to specify fig= TRUE

\documentclass{article}

\begin{document}
\SweaveOpts{concordance=TRUE}
Without $fig=TRUE$
<<fasfd, fig.height=5, fig.cap="first", fig.pos="t", echo=TRUE, fig.width=10>>=
barplot(table(mtcars$gear))
@
With $fig=TRUE$
<<fig=TRUE>>=
barplot(table(mtcars$gear))
@

A boxplot of the \emph{airquality} data:
\centering
<<fig=TRUE, echo=FALSE>>=
data(airquality)
boxplot(Ozone ~ Month, data = airquality)
@

\end{document}
1
votes

I never used knitr, but the syntax relay reminded me on sweave, in this case you need to correct the tag, for example:

<<label=fig1,fig=TRUE,echo=FALSE>>=

in total:

\documentclass{article}

\begin{document}
\SweaveOpts{concordance=TRUE}

<<label=fig1,fig=TRUE,echo=FALSE>>=

barplot(table(mtcars$gear))
@
\end{document}