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()
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 have been trying to perform some of the raster related operations using R and whenever I am loading raster files in R in the plot window it is being displayed at a different scale which is hard to notice. I'm a little confused about how to bring it back to a standard size. As I'm new to the R language I'm not able to figure it out. Little help would be appreciated. Thanks in advance.
It was due to the default values set in par() function. By changing the "mar" parameter in the function I was able to resize it to my own convenience.enter image description here
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 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()