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))
Related
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).
Using the forest.robu function from the robumeta package. Trying pdf(filename="test.pdf",width=20,height=8) suggested here did result in the error "unused argument filename". Any idea how I could make a plot from this function that does not fit the plot window fit in an output file? Thank you!
I'm guessing that you don't yet understand how R graphics devices are used. One needs to set a particular output device up and then print to it with one of the three graphics paradigms and then execute dev.off() to finish the process. Failing to execute dev.off() with ps or pdf or png will start a file but then it doesn't get finish and will be unreadable. I'm guessing that forest.robu constructs an object using either ggplot or lattice function calls, so do this:
?pdf; ?Devices; ?pdf.options # the help pages have further useful links
pdf(file= "My_grph.pdf", width=20, height=8)
gobj <- forest.robu( ..... whatever ...)
print(gobj)
dev.off()
If on the other hand, forest.robu() uses base-graphics plotting, then the print call is unneeded and might even cause problems so leave it out.
I currently have a 3d graph and I would like to save the output in both png and pdf. Is there a way to save it in those format at the same time? I know there is a ggplot function that can save plot but I'm not sure how in base r.
As far as I know you will need at least 2 lines of code, if this is what you mean. I like to use the dev.print() function to save graphics. The function is saving a copy of the plot your are currently looking at. Please note that dev.print() copy the interactive window and not directly the plot as it is specified in the help section.
dev.print() example:
dev.print(png, "yourfile.png")
dev.print(pdf, "yourfile.pdf")
To know where your file is saved you can also get and set your working directory with those 2 functions :
getwd() and setwd()
You can also use the png() and pdf() functions to save plot specifically in those format.
Your question is probably a duplicate tho.
I have to write a report about a model that I have built using Netlogo. I have made many plots of model's variables and I'd like to extract them and put them in my report to show how these variables vary upon the time. These variables represent what the change of some parameters imply. So, I'd like to obtain a better plot than netlogo's one, because netlogo's plots haven't got enumerated axis and I'd like plots with enumerated axis.
It would be amazing to put the plot in a word document or in a power point document
See the NetLogo dictionary entry for export-all-plots. The easiest way to access this is the menu File > Export > Export All Plots ... and then choose a folder/directory to store the file and a file name. You will then get a file in csv format that you can open in your graphing package (eg R, Excel).
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.