Legend problem when exporting a plot to PDF

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Legend problem when exporting a plot to PDF

Julien Roux-3
Hi list,

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, which is not the case for other graphical parts (text of
the legend, title, axis text...)
Any tip on how to deal with this?

Another problem I encounter, which may be linked to this is:
When I want to export PDFs to a given size with dev2bitmap (let's say
h=3 and w=3), the size of the text of x-labels, y-labels and the x-axis
annotations is not adjusted. The margins also keep the same size. Then
if the size is too small, all the text fields overlap and the result is
not nice.
How to change this? I guess it's possible since the on the screen (x11)
the margins are adjusted relative to the size of the plot.

If you know one good tutorial to help creating ready-to-publish
graphics, I would appreciate the reference.
Thanks for your help
Julien

PS:
R version 2.6.2 (2008-02-08)
i386-apple-darwin8.10.1

--
Julien Roux, PhD student
http://www.unil.ch/dee/page22707.html
Department of Ecology and Evolution
Biophore, University of Lausanne, 1015 Lausanne, Switzerland
tel: +41 21 692 4221    fax: +41 21 692 4165

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Legend problem when exporting a plot to PDF

Philipp Pagel-5

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

cu
        Philipp

--
Dr. Philipp Pagel
Lehrstuhl für Genomorientierte Bioinformatik
Technische Universität München
Wissenschaftszentrum Weihenstephan
85350 Freising, Germany
 
 and
 
Institut für Bioinformatik und Systembiologie / MIPS
Helmholtz Zentrum München -
Deutsches Forschungszentrum für Gesundheit und Umwelt
Ingolstädter Landstrasse 1
85764 Neuherberg, Germany
http://mips.gsf.de/staff/pagel

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Legend problem when exporting a plot to PDF

Prof Brian Ripley
In reply to this post by Julien Roux-3
Copying between devices (as in dev2bitmap) is not a good way to produce
high-quality graphs, and especially not if you change the size.

What is wrong with

pdf("test.pdf", width=6, height=6)
< re-run the plot commands>
dev.off()

?


On Tue, 29 Apr 2008, Julien Roux wrote:

> Hi list,
>
> 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, which is not the case for other graphical parts (text of the legend,
> title, axis text...)
> Any tip on how to deal with this?
>
> Another problem I encounter, which may be linked to this is:
> When I want to export PDFs to a given size with dev2bitmap (let's say h=3 and
> w=3), the size of the text of x-labels, y-labels and the x-axis annotations
> is not adjusted. The margins also keep the same size. Then if the size is too
> small, all the text fields overlap and the result is not nice.
> How to change this? I guess it's possible since the on the screen (x11) the
> margins are adjusted relative to the size of the plot.
>
> If you know one good tutorial to help creating ready-to-publish graphics, I
> would appreciate the reference.
> Thanks for your help
> Julien
>
> PS:
> R version 2.6.2 (2008-02-08)
> i386-apple-darwin8.10.1

With R version 2.7.0 you have other possibilities, including
dev.copy2pdf() and quartz(type="pdf").


> --
> Julien Roux, PhD student
> http://www.unil.ch/dee/page22707.html
> Department of Ecology and Evolution
> Biophore, University of Lausanne, 1015 Lausanne, Switzerland
> tel: +41 21 692 4221    fax: +41 21 692 4165
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

--
Brian D. Ripley,                  [hidden email]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Legend problem when exporting a plot to PDF

Joerg van den Hoff-2
In reply to this post by Philipp Pagel-5
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(...)
> dev.off()
>
> 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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.