wilkelab/cowplot

Legend right-padding in plot_grid with rel_widths on Ubuntu but not on MacOS

adamaltmejd opened this issue · 7 comments

Having a problem that I'm not really able to produce a reprex for. When building the plots for this site (http://adamaltmejd.se/covid/ repo: https://github.com/adamaltmejd/covid) locally on MacOS, it looks like it should. But the Github action running on ubuntu latest seems to use the legend widths from rel_widths = c(1,1) also when rel_widths = c(1, 1.5). See attached screenshots.

Screenshot 2020-05-04 at 14 26 29
Screenshot 2020-05-04 at 14 22 11

Just realized it is actually not due to rel_widths. Here is the plot built on Github with rel_widths = c(1,1)
asd

Looks like you're using a non-standard font. Try a different null device and see if that works:
https://wilkelab.org/cowplot/reference/set_null_device.html

If this helps, you can also consider implementing a null device using ragg, as it should have better cross-platform performance. (I should probably make that the default anyways.)

You can see the code here to implement a null device:

png_null_device <- function(width, height) {
grDevices::png(
filename = tempfile(pattern = "cowplot_null_plot", fileext = ".png"),
width = width, height = height,
units = "in", res = 96
)
grDevices::dev.control("enable")
}

I was using cowplot::set_null_device("pdf") because cowplot::set_null_device(cairo_null_device(width = 11.46, height = 6)) did not work. Throws error images cannot be created with other units than 'px' if dpi is not specified. But maybe I could try with a custom version setting dpi as well.

Edit: realized that set_null_device takes a function. But the error is still thrown just when the plot is built instead. Cairo needs dpi to be set when units are provided in inches.

Thank you! The following worked:

my_null_device <- function(width, height) {
    grDevices::png(
        filename = tempfile(pattern = "cowplot_null_plot", fileext = ".png"),
        width = width, height = height, type = "cairo",
        units = "in", res = 300
    )
    grDevices::dev.control("enable")
}
cowplot::set_null_device(my_null_device)

I may have to improve the documentation. The Cairo null device is meant to be used like this:

set_null_device(cairo_null_device)

Tried that too. Same error. It seems to lack a setting for resolution.

Ah, Ok, thanks for letting me know. I'll have to fix this. I never use the Cairo device.