Pull ggplot object name and pipe into ggsave

132 views
Skip to first unread message

Jeremy Hogeveen

unread,
Jul 13, 2017, 12:27:11 PM7/13/17
to ggplot2
Hi there,

I'm writing an analysis script with many plot objects and am currently going through the tedious task of inserting a ggsave("filename") line after each one, and then copying / pasting the ggplot object name into the <filename> field. However, it would be ideal if there was a function I could write to pull the name of the most recent ggplot object into a variable and pipe that into the ggsave function as a filename. 

Does anyone have any suggestions on how to do this? Any advice much appreciated!

Cheers,
Jeremy

Sam Albers

unread,
Jul 13, 2017, 12:30:14 PM7/13/17
to Jeremy Hogeveen, ggplot2
Hi Jeremy,

I've had great success using the methods outlined here: http://www.brodrigues.co/blog/2017-03-29-make-ggplot2-purrr/

If a reprex were present, folks might be able to provide specific help....

Sam

--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+unsubscribe@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jeremy Hogeveen

unread,
Jul 13, 2017, 12:48:55 PM7/13/17
to ggplot2, jeremy....@gmail.com
Thanks for the speedy reply Sam, I'll take a look at the link you shared!

My code uses a homemade function <scatterPlot> to build a ggplot object...

print(p.rbs3.proCost.ASD <- scatterPlot(dat,x,y,group,"xlab","ylab"))

This is done about 24 times throughout my analysis script. I'm now going through and adding the following to save the plots in a working folder...
 
  ggsave('./fig/working_files/p.rbs3.proCost.ASD.pdf')

This isn't such a big deal, but I can't help but think it would be more efficient / less error-prone if I could pull the object name from the last ggplot object into a variable <filename> so that I can insert the same ggsave line after each plot.



On Thursday, 13 July 2017 09:30:14 UTC-7, Sam wrote:
Hi Jeremy,

I've had great success using the methods outlined here: http://www.brodrigues.co/blog/2017-03-29-make-ggplot2-purrr/

If a reprex were present, folks might be able to provide specific help....

Sam
On Thu, Jul 13, 2017 at 9:27 AM, Jeremy Hogeveen <jeremy....@gmail.com> wrote:
Hi there,

I'm writing an analysis script with many plot objects and am currently going through the tedious task of inserting a ggsave("filename") line after each one, and then copying / pasting the ggplot object name into the <filename> field. However, it would be ideal if there was a function I could write to pull the name of the most recent ggplot object into a variable and pipe that into the ggsave function as a filename. 

Does anyone have any suggestions on how to do this? Any advice much appreciated!

Cheers,
Jeremy

--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com

More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.

Thierry Onkelinx

unread,
Jul 13, 2017, 4:58:05 PM7/13/17
to Jeremy Hogeveen, ggplot2
Dear Jeremy,

The easy solution is to write your script as an RMarkdown document. No need for ggsave() as ggplot() output will be saved automatically. The filename is taken from the chunkname (or unnamed in case no chunk name was set).

Best regards,

Thierry

ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest
team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
Kliniekstraat 25
1070 Anderlecht
Belgium

To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data. ~ Roger Brinner
The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey

--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com

More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages