Using legend () in Sweave: is this a mistake?

Now I'm learning sweave to write a vignette. I use traditional R graphics. Oddly enough, the legend I added to a high-level plot that works fine, otherwise it 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 (when R version 2.15 is launched) creates one point and a legend consisting of two points and two different types of lines: correct legend

The legend doesn’t work in sweave, I just see an empty box: no legend

Is it a sweave mistake, or am I missing something?

+3
source share
1 answer

Problem

This seems like a mistake, but it is a problem with pdf(), not with Sweave().

, , pdf(). , :

pdf("pdfPlot.pdf")
    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))
dev.off()

, cairo_pdf() , :

cairo_pdf("cairo_pdfPlot.pdf")
    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))
dev.off()

1: knitr.

knitr, . dev="cairo_pdf" (, , fig=TRUE), :

<<dev="cairo_pdf">>=
...
...
@

, library(knitr); knit("myScript.Rnw") Sweave("myScript.Rnw")

2. \includegraphics {}.

Sweave(), - :

<<results=tex, term=FALSE, echo=FALSE>>=
cairo_pdf("myPlot.pdf", width=5)
    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))
dev.off()
cat("\\includegraphics{myPlot.pdf}\n\n")
@
+6

All Articles