Re: [R] Legend problem when exporting a plot to PDF

From: Joerg van den Hoff <>
Date: Tue, 29 Apr 2008 15:12:29 +0200

On Tue, Apr 29, 2008 at 01:49:01PM +0200, Philipp Pagel wrote:
> > When exporting to PDF a graph with a legend, in the final PDF, the
> > text is going beyond the legend box.
> > > dev2bitmap("test.pdf", type="pdfwrite", h=6, w=6)
> > The legend looks OK on the screen. I noticed that the size of the
> > legend box depends on the size of the screen window,
> As far as I remember, te problem has to do with different font handling
> in different devices. I'm sure someone more familiar wiht the internals
> will comment on this.
> The fix is easy: don't use dev2bitmap but open the desired target device
> before plotting. In your case:
> pdf(file='test.pdf', width=6, height=6)
> plot(...)
> legend(...)
> cu
> Philipp

I'm not sure, whether this is the way to go. at least until recently the `pdf' device of R had a few rough edges which surfaced by an then. In my experience using `dev2bitmap(type="pdfwrite", ...)' -- and thus ghostscript for pdf generation resulted in cleaner/better pdf output (actually, `dev2bitmap' sort of postprocessess output from the `pdf' device a bit).

concerning the original question: yes, the problem is there. I believe it is related simply to the fact that the same absolute font size used in the graphic window is used in the pdf output, too, which makes its _relative_ size dependent on the chosen size (widht/height) of the pdf output.

my workaround is to ensure that the graphic windows size is exactly the same as that used in the `dev2bitmap` call. e.g. the X11() device has defaults of width = 7, height = 7 (but you can enforce other values by open a new one with, e.g., X11(width = 6, height = 9). thus, first plot to graphic window, make sure that no interactive resizing is necessary to get acceptable display on the monitor (otherwise close it and open new X11() with better width/height values). then call dev2bitmap() with the same width/height settings. that should lead essentially to WYSIWYG (modulo font substitution, maybe).

joerg mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Tue 29 Apr 2008 - 13:38:48 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 29 Apr 2008 - 14:30:33 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive