Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7993265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:48:04+00:00 2026-06-04T13:48:04+00:00

Right now I am learning sweave to write a package vignette. I am using

  • 0

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:
correct legend

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

Is this a sweave bug, or am I overlooking something?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T13:48:06+00:00Added an answer on June 4, 2026 at 1:48 pm

    The problem

    This does appear to be a bug, but it’s a problem with pdf() and not with Sweave() itself.

    To see what I mean, try this call to pdf(). It produces the same defective plot displayed above:

    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()
    

    By contrast, cairo_pdf() produces a plot that looks just fine:

    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()
    

    Solution 1: Use knitr.

    If you are willing to make the switch to knitr, fixing this is easy. Just add dev="cairo_pdf" to your code chunk header (and, if you like, drop the fig=TRUE), like this:

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

    Processing the code is then as simple as doing library(knitr); knit("myScript.Rnw") in place of your current call to Sweave("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:

    <<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")
    @
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning Python right now and I want to write some script that will
I am learning how to create a website using ASP.NET & C#. Right now
Right now I am using learning to program for the iphone and I am
Right now, I'm learning Python and Javascript, and someone recently suggested to me that
I'm learning C++ right now using Bruce Eckel's Thinking in C++ and I'm in
I'm learning c++ right now and I'd like to start interfacing with a graphics
I am learning VML right now. And I noticed that each example contains two
I'm learning some AJAX right now and the jQuery function that is being used
I'm learning Ruby right now and I got stuck, maybe you guys can help
I'm learning this right now so the questions might be a little juvenile. Here's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.