save 3d graph to multiple format - r

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.

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

Rasterize plot when using PDF output device

Hello everybody out there using R,
When putting multiple plots with thousands of data points into a single PDF file, this file can get huge and take a long time to open.
The following post describes exactly the same problem in Matplotlib, as well as a nice fix for it:
Matplotlib: multipage PDF with rasterized plots
Particularly nice about it is, that it only rasterizes the points without rasterizing the labels.
http://www.astrobetter.com/blog/2014/01/17/slim-down-your-bloated-graphics/ contains a nice example of it.
I am now looking for a similar solution in R.

How to save an object through GGally in R

I have a pretty dumb question to ask everyone.
I am using ggpairs under GGally to create a correlation matrix, and somehow I found that GGally did not provide a saving function as ggplot2 did. The function ggsave did not work for a non-ggplot2 object. I tried to use pdf or png, but they did not work. I am wondering if there's an easy to save this picture to a local file? Thank you for your kind help.
While #CMichael's comment is nice (I didn't know that, hence +1), it's applicable only if you want to save a particular plot from GGally-generated plot matrix. I believe that you'd like to save the whole plot matrix - the need, which I've recently also experienced. Therefore, you can use a standard R approach and save the graphics by opening corresponding (to desired format) graphical device, printing the object and closing the device, which will effectively save the graphics in a desired format.
# use pdf() instead of svg(), if you want PDF output
svg("myPlotMatrix.svg", height = 7, width = 7)
g <- ggpairs(...)
print(g)
dev.off()

How to save a graphic in R instead of visualizing it?

I a bit of code that produces a matrix. I then generate a heatmap from this matrix using the function:
heatmap(d)
However, I would like to be able to save this img directly and bypass displaying the graphic, so that I can incorporate this function into a unix-based workflow.
Can someone please show me the snippet of code required to save this image without displaying it on screen? Thanks!
Use jpeg(), png() or tiff() to create the file.
jpeg(file="filename.jpg")
heatmap(d)
dev.off()

Resources