[Rd] [FORGED] [R] R cairo_pdf function does not respect plotting boundaries

Paul Murrell p@u| @end|ng |rom @t@t@@uck|@nd@@c@nz
Tue Mar 5 01:58:26 CET 2019


(cc'ed to r-devel where further discussion should probably take place)

Thanks Lee.  I see that problem.

There is a "+ 1" in the Cairo device code for setting the clipping region

Remove the "+ 1" and the problem goes away (for your example at least).

The comment on the line above that code suggests that the "+ 1" was 
modelled on the X11 device code, but X11 deals in integer pixels and 
Cairo (at the API level) does not, so it would seem that the "+ 1" is 
just unnecessary.

However, I have a slight nagging worry that we have been here before, so 
I would like to do some more testing before committing that change.


On 1/03/19 8:13 AM, Lee Steven Kelvin wrote:
> Hello all,
> When producing a plot in R using the cairo_pdf device, the resultant plot does not respect the plotting boundaries. Lines and shaded regions will spill over the lower x-axis and the right-side y-axis (sides 1 and 4). I would like to know if it is possible to fix this behaviour when using 'cairo_pdf' in R?
> As an example, see the image at this web link: https://i.stack.imgur.com/0lfZd.png
> This image is a screenshot of a PDF file constructed using the following minimum working example code:
> cairo_pdf(file="test.pdf", width=0.5, height=0.5)
> par("mar"=c(0.25,0.25,0.25,0.25))
> plot(NA, xlim=c(0,1), ylim=c(0,1), axes=FALSE)
> polygon(x=c(-1,-1,2,2), y=c(-1,2,2,-1), density=5, col="green3", lwd=10)
> abline(h=0.25, col="red", lwd=5)
> abline(h=0.75, col="hotpink", lwd=5, lend=1)
> abline(v=0.25, col="blue", lwd=5)
> abline(v=0.75, col="cyan", lwd=5, lend=1)
> box()
> dev.off()
> Here I'm plotting a shaded region in green using 'polygon', with boundaries that lie outside the plot. I'm also drawing two sets of horizontal/vertical lines using 'abline'. The first in each pair uses standard rounded line caps, whilst the second in each pair uses butt line caps.
> As you can see, the shading lines and the default rounded-end ablines all extend beyond the plotting region along the lower and right-hand side axes. Only when using 'lend=1' am I able to contain the ablines to the plotting region. I know of no such fix for the shading lines however.
> I would naively expect the R plotting region to be respected, and for it to be impossible to plot outside of this region unless explicitly specified by the user.
> I have tested this on the other cairo devices (SVG and PS), and also reproduce the same behaviour, indicating that this is an issue with the cairo graphics API, or its implementation within R.
> This behaviour does not occur when using the standard R 'pdf' graphics device. I would switch to 'pdf' in general, however, 'cairo_pdf' has several advantages over 'pdf', notably, reduced output file sizes on occasion and support for a larger array of UTF-8 characters, so ideally I would prefer to use cairo_pdf.
> I should note that I have also posted this message on StackOverflow at this web link: https://stackoverflow.com/questions/54892809/r-cairo-pdf-function-does-not-respect-plotting-boundaries
> Thank you in advance for any insights into this issue.
> Sincerely,
> Lee Kelvin
> --
> Dr Lee Kelvin
> Department of Physics
> UC Davis
> One Shields Avenue
> Davis, CA 95616
> Ph: +1 (530) 752-1500
> Fax: +1 (530) 752-4717
> 	[[alternative HTML version deleted]]
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

Dr Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
New Zealand
64 9 3737599 x85392
paul using stat.auckland.ac.nz

More information about the R-devel mailing list