[R] ggsave() with width only

Adam Wysokiński @d@m@wy@ok|n@k| @end|ng |rom e@em@||
Mon Sep 20 21:29:01 CEST 2021


Dear Ivan,
I think you don't need to provide the aspect ratio, as this should work 
as well:

save_plot("/tmp/plot.png", p, base_width = 5, base_height = NULL)

-- 
Regards,
Adam Wysokiński

On 9/20/21 16:09, Ivan Calandra wrote:
> Dear Adam,
> 
> The function cowplot::save_plot() actually doesn't help in my case 
> because I need to know the aspect ratio (which I don't in advance). If I 
> knew the aspect ratio, I could calculate the height from the width or 
> vice-versa, and then I could use ggplot2::ggsave().
> 
> I have found a workaround using the package patchwork: I put the plots 
> together into one plot, that I save on an A4 page.
> 
> Best,
> Ivan
> 
> -- 
> Dr. Ivan Calandra
> Imaging lab
> RGZM - MONREPOS Archaeological Research Centre
> Schloss Monrepos
> 56567 Neuwied, Germany
> +49 (0) 2631 9772-243
> https://www.researchgate.net/profile/Ivan_Calandra
> 
> On 14/09/2021 9:17, Ivan Calandra wrote:
>> Thank you Adam!
>>
>> I'm a bit surprised that an extra package is needed for this, but why 
>> not!
>>
>> Best,
>> Ivan
>>
>> -- 
>> Dr. Ivan Calandra
>> Imaging lab
>> RGZM - MONREPOS Archaeological Research Centre
>> Schloss Monrepos
>> 56567 Neuwied, Germany
>> +49 (0) 2631 9772-243
>> https://www.researchgate.net/profile/Ivan_Calandra
>>
>> On 13/09/2021 15:40, Adam Wysokiński wrote:
>>> Hi,
>>> Instead of ggsave(), use save_plot() from the "cowplot" package:
>>>
>>> library(ggplot2)
>>> library(cowplot)
>>> x <- 1:10
>>> y <- x^2
>>> df <- data.frame(x, y)
>>> p <- ggplot(df, aes(x, y)) + geom_point()
>>> save_plot("/tmp/plot.png", p, base_aspect_ratio = 1, base_width = 5, 
>>> base_height = NULL)
>>>
> 
> ______________________________________________
> 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.



More information about the R-help mailing list