Search code examples
rggplot2facebook-prophet

Multiple prophet plots saved to jpeg in for loop (R)


I'm trying to, within a for loop, create and save multiple plots from fb prophet and save them to jpeg files.

Despite following the code for many questions related to saving R plots in a loop on stackoverflow, none of the solutions seem to be working and I can't figure out what I'm doing wrong. The below code would be within a for loop:

  jpeg(filename="plot1.jpeg")
  plot(model, future_forecast) + ggtitle(Material_name)
  dev.off()

  #now plot seasonalities
  jpeg(filename="plot2.jpeg")
  prophet_plot_components(model, future_forecast) 
  dev.off()


I expect that this code would create two separate plots using the prophet plotting functionality, and save them to jpeg files. What actually happens is that the program saves one plot file correctly, and a second plot file as a blank plot.


Solution

  • When you execute (or source) a script as opposed to running it line-by-line, some of the output is suppressed. This is what is happening here. In order to force the output of your plot, just enclose the statement within the print function.
    For example:

    print(prophet_plot_components(model, future_forecast))