Export Plots in R to Existing Pdf without Overwriting - r

I have two separate scripts that each outputs a large number of plots. I can set the parameters for the plots but I can not easily combine the scripts. I'd like for all the plots to be saved into the same pdf (or tiff, or png, whatever works) in a way that on each page I have 1 figure from script 1 and 1 figure from script 2. My thinking is that I'll have script 1 to output the figures on the left side of the pages, saving space for script 2. Then in script 2, if I can somehow have the plots exported to the same file, only on the right side of the pages WITHOUT overwriting the existing plots. Any help will be greatly appreciated!
I'm also open to other apps that can automatically combine existing pdfs in this way because I actually have already made the plots separately.
TIA

Related

How do I size plots correctly in Rmarkdown?

When I create a plot from a normal R script, click Export and Copy to Clipboard... I get a nice sized plot which works well to paste in a presentation. The size in the Copy Plot to Clipboard plot shows Width of 1065 and Height of 652. See below -
However when I create the sample plot in an rmarkdown and knit, I get something like this -
How can I play with plot settings in rmarkdown to produce an output like the first screenshot?
Hi there so you should stop using the console interface totally for this. You want more control over the size of plots than that can offer you. Also doing it the way you do it offers no means to autogenerate plots, what happens when you need to run a loop and make 100 images or even just like 10 or 20? That's a lot of unnecessary manual labor
Here's how you can do this....
dev.off() # this clears the plotting function
jpeg("filename", width = 100, hieght = "100") # note you can use png or
# other commands, this creates an image file for you to call a plot function on
plot(data) # any plotting function may be called here, ggplots pheatmap etc..
dev.off() # this causes R to save the image of the plot to file
Controlling image size in Rmarkdown, there is documentation on how to do this which is located here: https://bookdown.org/yihui/rmarkdown-cookbook/figure-size.html
I recommend doing what I showed above, supplying the code in markdown but not necessarily running it every time you knit the file. Instead its better to make the plot you want once then load it into markdown from a saved location so you can fiddle with the size easier. It will save you a ton of time running the same computation unnecessarily multiple time each time you knit. Again supply the code you used to make the plot in the Rmarkdown for completeness sake but don't actually run it. Making an Rmarkdown file will require you to knit multiple times repeatedly as you fiddle with the settings on your images and the text itself. Best to avoid running computation as much as possible, especially if your plots take a while to generate (heatmaps for instance can be a little heavy).

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.
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))

showing saved HTML plots in Shiny

I have a lot of plots saved in a folder and their format is *.html so they could be easily viewed in a browser. Plots were made using plotly in R. I want to build a Shiny app to view these saved files. Does anyone know how to read and view these files into Shiny?
I know the other alternative is to plot on demand in Shiny, but due to the large number of data points and time to generate plots, I want to use the saved files. I appreciate your help.

Interactive plots on local .html via .rmd or Shiny

I'm trying to build a .html file via RStudio in to have following function(simplified).
plot1:a simple time series plot of $y_t$, where user can manually pull/drag each dot to change it's values a each time point.
plot2:a time series dependent on $y_t$, such as $f(y_t)=2*y_t+1$, once the value in plot1 changed, the plot2 will also change accordingly.
I want the .html self contained, not cloud based. I'm thinking of plotly, shininy/knitr, but I'm not sure if I'm in the direction or how to connect the dots. Hope anyone can point me to the right direction.
A self-contained/client-side html report sounds well-suited for flexdashboard. It's based on R Markdown, and therefore can accommodate Shiny elements.
I don't know if you can modify values with a mouse, but you can certainly modify values with sliders and other inputs, as well as have a second plot react to a first plot.
You may see some more possibilities in the gallery.

Editing multiple plots in Rstudio

One interesting feature of RStudio is it allows to save multiple plots generated from a script. This however opens up the problem of how to edit multiple plots. My issue at the moment is adding lines to histograms using the abline() function. This function was designed however to work with the last plot generated by the environment. One way of course would be ad the lines as soon as the plot is generated, however I have to calculate the coordinates at the end of the algorithm, by then I have transformed the data and generated multiple plots from it. So I was wondering if there isn't a way to tell R to search for a given plot and add the line to it. I read abline() documentation but found nothing regarding it. One can always save the data necessary to generate the plot and generate it at the end of the script, but I was wondering if there isn't a less consuming memory method.
One way to get around this issue is:
1.Save your graphics as variables, for ex: hist_1=hist(x, plot=FALSE)
2.Write any code u like, for ex: very complicated code give y as a number for output
3.plot(hist_1)
4.abline(hist_1, v=y)
gives a general idea of how to edit multiple plots without having to save multiple copies of datasets and without overloading Rstudio interface. Works well with the R ubuntu terminal too.

Resources