Bug 14926 - composite legend not correctly added when pdf() device used
composite legend not correctly added when pdf() device used
Status: CLOSED FIXED
Product: R
Classification: Unclassified
Component: Graphics
R 2.15.0
All All
: P5 minor
Assigned To: R-core
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2012-05-24 08:17 UTC by Philipp Doebler
Modified: 2012-06-03 00:31 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Philipp Doebler 2012-05-24 08:17:27 UTC
Adding a legend to a plot usually works fine using the following command:

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

Note that the above legend contains points and lines. But when the pdf() device is used, something goes wrong:

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

The bug appears when mixing points and lines in the legend, i.e. the following works fine:

pdf("pdfPlot2.pdf")
 plot(0.5, 0.5, xlim = c(0,1), ylim = c(0,1))
 legend("bottomright", c("data", "summary"), 
        pch = c(2,1))
 dev.off()
Comment 1 Duncan Murdoch 2012-06-02 18:19:37 UTC
In one viewer, the first plot looks fine; another one (Adobe Reader 7.0.0 on Windows) complains about an error ("Unrecognized token '1.#R' was found.") in the file.  So I suspect there's something wrong in it, but I don't know how to find it...
Comment 2 Duncan Murdoch 2012-06-02 20:02:48 UTC
After a bit of looking, it appears that what is happening is that the NA line widths are being embedded in the output PDF.  Some viewers handle that properly, others give an error.

Reading the docs, I don't see anything to indicate that the pdf() device claims to accept NA as a line width, so I'd call this user error.  The documented way to have the line omitted is to use lty=0, e.g.

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(1,1, 2,1), lty=c(0,0,1,1))

and this works in a simple test.  However, it's true that lwd=NA works in some of the other devices, so I'll look into implementing it in the pdf() device.
Comment 3 Duncan Murdoch 2012-06-03 00:31:35 UTC
I fixed this in legend(), so setting lwd to NA should work in all devices for legends.  Outside of legend(), devices remain as inconsistent as ever in how they handle lwd.