[R] ggsave() with width only

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Mon Sep 6 17:53:23 CEST 2021


On 06/09/2021 11:06 a.m., Ivan Calandra wrote:
> Yes Jeff, you are right. I hate manually editing figures too, but
> sometimes I find it's still the easiest way (e.g. when you submit your
> paper several times when journals have differing guidelines, or when you
> build figures from several (sub)plots + other images, or when you
> combine plots that a colleague has done in Python with your R plots). I
> have the impression that at some point, there is always something to
> edit by hand, no matter how much you've adjusted the graphical
> parameters and even if you use all possible tools available for ggplot2...
> 
> I have thought a lot about it and, as it is, I am not sure it would be
> worth the effort. I might be missing some arguments for it, but I would
> actually like someone to show me how it could look like - this might
> just be what I need to be convinced!

It's not much effort.  For example, the document below produces two PDF 
figures with different heights but the same width.    I called the 
document Untitled.Rmd, so the figures show up in 
Untitled_figures/figure-latex/fig1-1.pdf and 
Untitled_figures/figure-latex/fig2-1.pdf.

   ---
   title: "Untitled"
   author: "Duncan Murdoch"
   date: "06/09/2021"
   output:
     pdf_document:
       keep_tex: true
   ---

   ```{r setup, include=FALSE}
   knitr::opts_chunk$set(echo = TRUE)
   ```

   ```{r fig1, fig.width=2, echo=FALSE}
   library(ggplot2)
   ggplot(mtcars, aes(carb, gear)) +
     geom_point()
   ```

   ```{r fig2, fig.width=2, echo=FALSE}
   ggplot(mtcars, aes(carb, gear)) +
     geom_point() +
     coord_fixed()
   ```



More information about the R-help mailing list