Right now I am learning sweave to write a package vignette. I am using traditional R graphics. Strangely a legend that I added to a highlevel plot that works fine otherwise does not work when I Sweave the file. Here is a minimal example:
\documentclass{article}
\begin{document}
<<fig=TRUE>>=
plot(0.5, 0.5, xlim = c(0,1), ylim = c(0,1))
legend("bottomright", c("data", "summary", "curve", "conf. region"),
pch = c(2,1,NA,NA), lwd = c(NA,NA, 2,1))
@
\end{document}
The R code produces (when run R version 2.15) a single point and a legend consisting of two points, and two different types of lines:

In sweave the legend fails to be produced, I just see an empty box:

Is this a sweave bug, or am I overlooking something?
The problem
This does appear to be a bug, but it’s a problem with
pdf()and not withSweave()itself.To see what I mean, try this call to
pdf(). It produces the same defective plot displayed above:By contrast,
cairo_pdf()produces a plot that looks just fine:Solution 1: Use knitr.
If you are willing to make the switch to
knitr, fixing this is easy. Just adddev="cairo_pdf"to your code chunk header (and, if you like, drop thefig=TRUE), like this:Processing the code is then as simple as doing
library(knitr); knit("myScript.Rnw")in place of your current call toSweave("myScript.Rnw")Solution 2: Construct your own call to \includegraphics{}.
If you must stick with
Sweave(), doing something like this will get you around the problem: