ggplot and legend clipping

1,834 views
Skip to first unread message

Ben Bolker

unread,
Feb 11, 2012, 9:10:49 PM2/11/12
to kn...@googlegroups.com

I'm really enjoying knitr but ... having persistent difficulties
getting ggplot graphs with text that is correctly sized and unclipped.
In the attached Rnw file I get the longest category name clipped off
in the legend. Various messing around with fig.width, out.width,
fig.align, ... options hasn't helped me, possibly because I don't
understand the underlying logic clearly enough.

The directly-generated PDF from the third chunk ("tmp.pdf") is OK,
but the PDF generated from the second chunk ("unnamed-chunk-2.pdf") is
clipped -- so it's not just the passage through pdflatex that does
something to it ...

should I try messing with resize.width, resize.height ... ?

Ben Bolker

knitggclip.Rnw

Yihui Xie

unread,
Feb 11, 2012, 9:45:11 PM2/11/12
to Ben Bolker, kn...@googlegroups.com
What do you mean by clipping in this case? Is the legend partly
"chopped off", or part of the texts extend into the margin of the
page? With your current setting, I can only observe the latter, and
the reason is you set out.width=1.2\textwidth so that the figure is
wider than the page width. If you want it to stay inside the margin,
you need to set out.width to be smaller or equal to 1\textwidth.

Regards,
Yihui
--
Yihui Xie <xiey...@gmail.com>
Phone: 515-294-2465 Web: http://yihui.name
Department of Statistics, Iowa State University
2215 Snedecor Hall, Ames, IA

Ben Bolker

unread,
Feb 12, 2012, 11:54:49 AM2/12/12
to Yihui Xie, kn...@googlegroups.com
On 12-02-11 09:45 PM, Yihui Xie wrote:
> What do you mean by clipping in this case? Is the legend partly
> "chopped off", or part of the texts extend into the margin of the
> page? With your current setting, I can only observe the latter, and
> the reason is you set out.width=1.2\textwidth so that the figure is
> wider than the page width. If you want it to stay inside the margin,
> you need to set out.width to be smaller or equal to 1\textwidth.
>
> Regards,
> Yihui
> --
> Yihui Xie <xiey...@gmail.com>
> Phone: 515-294-2465 Web: http://yihui.name
> Department of Statistics, Iowa State University
> 2215 Snedecor Hall, Ames, IA

Thanks for your quick response (and sorry for my slow one). I had
tried a variety of different out.width settings (\textwidth,
0.7\textwidth, etc.) and the longest legend label is still clipped.
Oddly enough, when viewing it with evince (a Gnome PDF viewer), if I
highlight the legend text the end of the label becomes visible; in
acrobat reader, I can highlight and see a bar that extends the full
width of the text, but the cut-off text is not visible.

Ben Bolker

Yihui Xie

unread,
Feb 12, 2012, 12:16:38 PM2/12/12
to Ben Bolker, kn...@googlegroups.com
This is odd. Anyway, let's figure out if it is a problem of the PDF
viewer or knitr. Here is what I get from your Rnw document:
https://github.com/downloads/yihui/knitr/knitggclip.pdf I can see all
the legend labels in Adobe Reader.

Regards,
Yihui
--
Yihui Xie <xiey...@gmail.com>
Phone: 515-294-2465 Web: http://yihui.name
Department of Statistics, Iowa State University
2215 Snedecor Hall, Ames, IA

Ben Bolker

unread,
Feb 12, 2012, 12:43:44 PM2/12/12
to Yihui Xie, kn...@googlegroups.com
On 12-02-12 12:16 PM, Yihui Xie wrote:
> This is odd. Anyway, let's figure out if it is a problem of the PDF
> viewer or knitr. Here is what I get from your Rnw document:
> https://github.com/downloads/yihui/knitr/knitggclip.pdf I can see all
> the legend labels in Adobe Reader.
>
> Regards,
> Yihui
> --
> Yihui Xie <xiey...@gmail.com>
> Phone: 515-294-2465 Web: http://yihui.name
> Department of Statistics, Iowa State University
> 2215 Snedecor Hall, Ames, IA
>

This is indeed weird, and I'm beginning to suspect some kind of
interaction *somewhere* along the toolchain: knitr -> ggplot -> R -> PDF
driver -> pdflatex ?

I can see your PDF just fine (in acrobat reader or evince), so I don't
think it's the viewer.

Here are my three variants:

Full PDF file -- clipped

http://www.math.mcmaster.ca/~bolker/misc/knitggclip.pdf

PDF figure only, as generated from within knitr (first chunk) -- clipped

http://www.math.mcmaster.ca/~bolker/misc/knitggclipfig.pdf

PDF figure, as explicitly saved via pdf() (second chunk) -- *not* clipped

http://www.math.mcmaster.ca/~bolker/misc/knitggclipfig2.pdf

This is under R-devel, knitr 0.2, ggplot 0.9.0, on Ubuntu 10.04 -- I
haven't tried other combinations yet. I tried dev=cairo_pdf, didn't
seem to change anything.

I guess that this suggests something about the specific procedure by
which knitr captures PDF output ...

???
Ben

Yihui Xie

unread,
Feb 12, 2012, 12:58:14 PM2/12/12
to Ben Bolker, kn...@googlegroups.com
Did you run knitr in an interactive or non-interactive session? If you
have not tried the former, please try it because these two different
sessions use different devices to record plots, and I believe the
former one is more stable.

Also I noticed you are using a development version of R, and this
might make a difference. I'm using 2.14.1.

Regards,
Yihui
--
Yihui Xie <xiey...@gmail.com>
Phone: 515-294-2465 Web: http://yihui.name
Department of Statistics, Iowa State University
2215 Snedecor Hall, Ames, IA

Ben Bolker

unread,
Feb 12, 2012, 3:49:10 PM2/12/12
to Yihui Xie, kn...@googlegroups.com
On 12-02-12 12:58 PM, Yihui Xie wrote:
> Did you run knitr in an interactive or non-interactive session? If you
> have not tried the former, please try it because these two different
> sessions use different devices to record plots, and I believe the
> former one is more stable.

Running in a non-interactive session fixed the problem. Thanks. I
don't know whether this is worth putting on an "issue list" somewhere --
presumably it constitutes a bug *somewhere* in *someone's* code, but
it's not clear to me whose responsibility it is ...

thanks very much -- your solution to embedding functions is working great.

cheers
Ben Bolker

Yihui Xie

unread,
Feb 12, 2012, 3:58:51 PM2/12/12
to Ben Bolker, kn...@googlegroups.com
My gut feeling is this is still related to an old issue:
https://github.com/yihui/knitr/issues/9

For some reason, pdf(file = NULL) is not stable in your devel version
of R. At least I know Prof Ripley made a change to it a few days ago
to fix the bug:
https://bugs.r-project.org/bugzilla3/show_bug.cgi?id=14808 So I
recommend to either roll back to the stable version of 2.14.1, or
check out the latest source of R and rebuild it.

Regards,
Yihui
--
Yihui Xie <xiey...@gmail.com>
Phone: 515-294-2465 Web: http://yihui.name
Department of Statistics, Iowa State University
2215 Snedecor Hall, Ames, IA

Ben Bolker

unread,
Feb 12, 2012, 4:01:07 PM2/12/12
to Yihui Xie, kn...@googlegroups.com
On 12-02-12 03:58 PM, Yihui Xie wrote:
> My gut feeling is this is still related to an old issue:
> https://github.com/yihui/knitr/issues/9
>
> For some reason, pdf(file = NULL) is not stable in your devel version
> of R. At least I know Prof Ripley made a change to it a few days ago
> to fix the bug:
> https://bugs.r-project.org/bugzilla3/show_bug.cgi?id=14808 So I
> recommend to either roll back to the stable version of 2.14.1, or
> check out the latest source of R and rebuild it.

Thanks very much. For now I will just use the workaround of building
non-interactively whenever I care about the details of the plots ... I
have been having a little trouble building the latest R-devel, probably
something boneheaded I'm doing, but I don't want to take the time to fix
it right now so I will wait and see if this gets resolved. (Unless it
would be very useful to you to have this followed up, in which case I'll
try to make some time to do so.)

cheers
Ben Bolker

Reply all
Reply to author
Forward
0 new messages