Re: [FORGED] [R] R cairo_pdf function does not respect plotting boundaries

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

Re: [FORGED] [R] R cairo_pdf function does not respect plotting boundaries

Paul Murrell-2
Hi

(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
(https://github.com/wch/r-source/blob/ba600867f2a94e46cf9eb75dc8b37f12b08a4561/src/library/grDevices/src/cairo/cairoFns.c#L156)


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.

Paul

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
> USA
>
> Ph: +1 (530) 752-1500
> Fax: +1 (530) 752-4717
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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
Auckland
New Zealand
64 9 3737599 x85392
[hidden email]
http://www.stat.auckland.ac.nz/~paul/

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: R cairo_pdf function does not respect plotting boundaries

Paul Murrell-2
Hi

I have committed this fix to r-devel (r76226).

Please let me know if this does not fix things for you.

Paul

On 5/03/19 8:22 PM, Lee Steven Kelvin wrote:

> Hi Paul,
>
> Great, thank you for looking in to this, and I'm glad that you're able
> to reproduce it at your end too.
>
>  From your reply, I'm happy that it seems like the fix may be fairly
> trivial, but I understand the necessity for caution.
>
> If there's anything else I can do to help, please do let me know.
>
> Thank you again,
> Best,
> Lee
>
>
> On Monday, 4 March 2019, Paul Murrell <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     Hi
>
>     (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
>     (https://github.com/wch/r-source/blob/ba600867f2a94e46cf9eb75dc8b37f12b08a4561/src/library/grDevices/src/cairo/cairoFns.c#L156
>     <https://github.com/wch/r-source/blob/ba600867f2a94e46cf9eb75dc8b37f12b08a4561/src/library/grDevices/src/cairo/cairoFns.c#L156>)
>
>
>     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.
>
>     Paul
>
>     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
>         <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
>         <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
>         USA
>
>         Ph: +1 (530) 752-1500
>         Fax: +1 (530) 752-4717
>
>
>                  [[alternative HTML version deleted]]
>
>         ______________________________________________
>         [hidden email] <mailto:[hidden email]> mailing list
>         -- To UNSUBSCRIBE and more, see
>         https://stat.ethz.ch/mailman/listinfo/r-help
>         <https://stat.ethz.ch/mailman/listinfo/r-help>
>         PLEASE do read the posting guide
>         http://www.R-project.org/posting-guide.html
>         <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
>     Auckland
>     New Zealand
>     64 9 3737599 x85392
>     [hidden email] <mailto:[hidden email]>
>     http://www.stat.auckland.ac.nz/~paul/
>     <http://www.stat.auckland.ac.nz/~paul/>
>
>
>
> --
> Dr Lee Kelvin
> Department of Physics
> UC Davis
> One Shields Avenue
> Davis, CA 95616
> USA
>
> Ph: +1 (530) 752-1500
> Fax: +1 (530) 752-4717
>

--
Dr Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
Auckland
New Zealand
64 9 3737599 x85392
[hidden email]
http://www.stat.auckland.ac.nz/~paul/

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel