Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Save multiple svg/png/tif plots

Tags:

plot

r

I am currently using pdf() to save multiple plots on several pages.

I change page simply by plot.new().

Can I easily get svg() and png() to do the same? Currently only the last plot is saved in the file.

If I cannot have it in the same file, can I have them autogenerate files like: output.png, output2.png.

like image 395
Ole Tange Avatar asked Dec 20 '22 23:12

Ole Tange


1 Answers

If you look at the help pages ?png and ?svg you will see that the default file names are "Rplot%03d.png" and "Rplot%03d.svg" respectively. The %03d part of those names means that each time a new plot is created it will automatically open a new file and that part of the file name will be replaced by an incrementing integer. So the first file will be "Rplot001.png" and the next will be "Rplot002.png" etc.

If you don't like the default file name you can create your own and still insert the portion to be replaced by an integer, such as "myplots%02d.png". The % says this is where the number part starts, the 0 is optional, but says to 0 pad the numbers (so you get 01, 02, ... rather than 1, 2, ...), this is generally preferred so that the sorting works out correctly (otherwise you may see the sorting as 1,10,11,2,3,...) and the digit (3 in the default, 2 in my example) is the number of digits, if you will create more than 1,000 plots you should up that to 4, if you know that you will not create 100 then 2 is fine (1 is fine if you know that you will produce fewer than 10). And the d is just an indicator for an integer.

like image 149
Greg Snow Avatar answered Jan 16 '23 00:01

Greg Snow