Re: [R] ggsave() with width only
Dear Adam, This would work indeed, but then the default aspect ratio (1.618) would be used. I could as well calculate the height from the width and aspect ratio. Unfortunately, this doesn't help me in my case (but as I said, I have found a workaround). Thank you again. 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 Le 20/09/2021 à 21:29, Adam Wysokiński via R-help a écrit : 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) __ R-help@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.
Re: [R] ggsave() with width only
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@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. __ R-help@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.
Re: [R] ggsave() with width only
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@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.
Re: [R] ggsave() with width only
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@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.
Re: [R] ggsave() with width only
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 __ R-help@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.
Re: [R] ggsave() with width only
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() ``` __ R-help@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.
Re: [R] ggsave() with width only
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! -- 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 06/09/2021 16:44, Jeff Newmiller wrote: I don't always use rmarkdown to write papers either, but you can capture figures from it. I avoid hand editing figures like the plague of irreproducibility. But sometimes you get stuck in an approach... I cannot answer your original post, but wanted to point out that it may not actually be necessary to answer it if you change your approach. On September 6, 2021 7:29:34 AM PDT, Ivan Calandra wrote: Thank you Jeff for your answer. I do use rmarkdown but I do not write papers completely with it. I do output a report in HTML but I also like to export the plots as PDF so that I can edit them (using Inkscape or similar) if and as needed. And because I like to have both the HTML report including plots and extra plots as PDF, I cannot use pdf(). That's why I use ggsave(). Or am I missing something? 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 06/09/2021 16:24, Jeff Newmiller wrote: 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 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 __ R-help@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.
Re: [R] ggsave() with width only
I don't always use rmarkdown to write papers either, but you can capture figures from it. I avoid hand editing figures like the plague of irreproducibility. But sometimes you get stuck in an approach... I cannot answer your original post, but wanted to point out that it may not actually be necessary to answer it if you change your approach. On September 6, 2021 7:29:34 AM PDT, Ivan Calandra wrote: >Thank you Jeff for your answer. > >I do use rmarkdown but I do not write papers completely with it. I do >output a report in HTML but I also like to export the plots as PDF so >that I can edit them (using Inkscape or similar) if and as needed. >And because I like to have both the HTML report including plots and >extra plots as PDF, I cannot use pdf(). That's why I use ggsave(). > >Or am I missing something? > >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 06/09/2021 16:24, Jeff Newmiller wrote: >> 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 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. __ R-help@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.
Re: [R] ggsave() with width only
Thank you Jeff for your answer. I do use rmarkdown but I do not write papers completely with it. I do output a report in HTML but I also like to export the plots as PDF so that I can edit them (using Inkscape or similar) if and as needed. And because I like to have both the HTML report including plots and extra plots as PDF, I cannot use pdf(). That's why I use ggsave(). Or am I missing something? 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 06/09/2021 16:24, Jeff Newmiller wrote: 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 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 __ R-help@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.
Re: [R] ggsave() with width only
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 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. __ R-help@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.
[R] ggsave() with width only
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 __ R-help@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.