Saving plots in R

Saving plots in R


Hello All,

I am trying to save plot which I generated using ggplot in R. I saved with .jpeg, .pdf, .png formats. But none of them look good when I try to resize it for putting it on a presentation slide. Could someone suggest what is the best format to save those plots in order to look good on slides?





How are you saving the plots? If you’re manually saving them (say from Rstudio), the resolution is pretty bad (like 72 dpi or something by default).

The easiest way I’ve found to save plots with good resolution is as follows:

pdf(file = "FileName.pdf", width = 8, height = 11) # defaults to 7 x 7 inches
plot(x, y)

postscript("FileName.eps", width = 12, height = 17, horizontal = FALSE, 
           onefile = FALSE)

tiff("FileName.tiff", height = 30, width = 20, units="cm 
     compression = "lzw", res = 300)

The pdf and postscript option yields files that you can additionally edit in Illustrator, whereas tiff gives a high-res image file that”s much better than typical jpegs or png files. These commands won’t show the plots, but will save them directly to the file specified.

I’m late for the party, but I use and suggest another approach.

ggsave("you_plot_name.pdf", width = 12, height = 6)

This command will plot the last plot you made. As I am using Jupyter notebook/lab it’s very handy when you just find the perfect combination of width and heigh with the command
options(repr.plot.width = 12, repr.plot.height = 6)

I used to use the pdf() + set, but the thing is that you save running time with the notebooks using this single-line approach.

before adding your answer.

Traffic: 1800 users visited in the last hour

Read more here: Source link