Dear useRs, I produce several independent ggplot2 plots and I would like to save them to a fixed width (for publications), but the height (and therefore aspect ratio) is different from plot to plot. How can I save my plots with ggsave() supplying only a fixed width but without knowing the height nor the aspect ratio? If I specify the width only, the plots are truncated in width because the aspect ratio is not correct. Thank you for the tip! 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
I use an rmarkdown file to generate consistent output figures and tables for html or Word. I just use Rnw files directly if I am generating LaTeX. I do not use R files for building output... and I never use ggsave. So you might consider altering your approach to bypass the question entirely. On September 6, 2021 7:03:46 AM PDT, Ivan Calandra <calandra at rgzm.de> wrote:>Dear useRs, > >I produce several independent ggplot2 plots and I would like to save >them to a fixed width (for publications), but the height (and therefore >aspect ratio) is different from plot to plot. > >How can I save my plots with ggsave() supplying only a fixed width but >without knowing the height nor the aspect ratio? If I specify the width >only, the plots are truncated in width because the aspect ratio is not >correct. > >Thank you for the tip! >Ivan >-- Sent from my phone. Please excuse my brevity.
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) -- Regards, Adam Wysoki?ski On 9/6/21 16:03, Ivan Calandra wrote:> Dear useRs, > > I produce several independent ggplot2 plots and I would like to save > them to a fixed width (for publications), but the height (and therefore > aspect ratio) is different from plot to plot. > > How can I save my plots with ggsave() supplying only a fixed width but > without knowing the height nor the aspect ratio? If I specify the width > only, the plots are truncated in width because the aspect ratio is not > correct. > > Thank you for the tip! > Ivan >