0

By default in R, you can generate a PDF file with multiple pages (1 page: 1 graph) using a single script like so:

single-file-multi-page-PDF-genator.r

library(ggplot2)

# graph for the 1st page
ggplot(...)

# graph for the 2nd page
ggplot(...)

You can also generate a PNG file with a single graph on it

single-file-single-graph-PNG-genator.r

png('/tmp/my-chart.png')

library(ggplot2)

# graph
ggplot(...)

How can you generate multiple PNG graphs by using just a single script in R?

Abel Callejo
  • 13,779
  • 10
  • 69
  • 84
  • 1
    Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use `str()`, `head()` or screenshot)? You can use the [`reprex`](https://reprex.tidyverse.org/articles/articles/magic-reprex.html) and [`datapasta`](https://cran.r-project.org/web/packages/datapasta/vignettes/how-to-datapasta.html) packages to assist you with that. See also [Help me Help you](https://speakerdeck.com/jennybc/reprex-help-me-help-you?slide=5) & [How to make a great R reproducible example?](https://stackoverflow.com/q/5963269) – Tung May 10 '19 at 02:20
  • These might help https://stackoverflow.com/a/52902877/ & https://stackoverflow.com/a/55194736/ – Tung May 10 '19 at 02:23

1 Answers1

0

It turns out the png() function can be used more than once to target a new file

library(ggplot2)

# graph for the 1st PNG file
png('/tmp/my-chart-1.png')
ggplot(...)

# graph for the 2nd PNG file
png('/tmp/my-chart-2.png')
ggplot(...)
Abel Callejo
  • 13,779
  • 10
  • 69
  • 84