Saving the Displayed Graph in R

Saving the Displayed Graph in R

Saving your plotted graphs is essential for sharing results and including visuals in reports. Here’s a detailed guide on how to save the displayed graph in R.

General Steps for Saving Graphs

To save a graph in R, you need to:

  • Open a Graphics Device: Specify the type of file and output settings.
  • Plot the Graph: Use plotting functions like plot(), ggplot(), etc.
  • Close the Graphics Device: Save and close the file.

Types of File Formats and Corresponding Functions

PDF

Creates a PDF file, suitable for printed or digital documents.

Example: Save a Graph as a PDF 

pdf("graph.pdf", width = 8, height = 6)  # Opens a PDF device
plot(1:10, (1:10)^2, main = "Example PDF Graph", xlab = "x", ylab = "y")
dev.off()  # Closes the device and saves the file
  • width and height: Dimensions of the graph in inches.

PNG

Creates a PNG file, a raster format ideal for web images and presentations.

Example: Save a Graph as a PNG 

png("graph.png", width = 800, height = 600, res = 100)  # Opens a PNG device
plot(1:10, (1:10)^2, main = "Example PNG Graph", xlab = "x", ylab = "y")
dev.off()  # Closes the device and saves the file
  • width and height: Dimensions of the graph in pixels.
  • res: Resolution in dots per inch (DPI).

JPEG

Creates a JPEG file, often used for web images and presentations.

Example: Save a Graph as a JPEG 

jpeg("graph.jpeg", width = 800, height = 600, quality = 90)  # Opens a JPEG device
plot(1:10, (1:10)^2, main = "Example JPEG Graph", xlab = "x", ylab = "y")
dev.off()  # Closes the device and saves the file
  • quality: Compression quality (0 to 100).

TIFF

Creates a TIFF file, often used for high-quality images in scientific publications.

Example: Save a Graph as a TIFF 

tiff("graph.tiff", width = 800, height = 600, res = 300)  # Opens a TIFF device
plot(1:10, (1:10)^2, main = "Example TIFF Graph", xlab = "x", ylab = "y")
dev.off()  # Closes the device and saves the file
  • res: Resolution in DPI.

Common Parameters for File Devices

  • width and height: Control the size of the graph.
  • res: Controls the resolution for raster formats (PNG, JPEG, TIFF).
  • bg: Background color of the graph (for raster formats).

Advanced Methods for Saving Graphs

Multiple Plots in One File

To save multiple plots in a single file, use par() to configure the layout.

Example: Multiple Plots in a PDF 

pdf("multiple_plots.pdf")
par(mfrow = c(2, 2))  # Layout for 2x2 plots
plot(1:10, (1:10)^2, main = "Plot 1")
plot(10:1, (1:10)^2, main = "Plot 2")
plot(1:10, (1:10)^3, main = "Plot 3")
plot((1:10)^2, (1:10)^3, main = "Plot 4")
dev.off()

Using ggsave() for ggplot2

If you use ggplot2, the ggsave() function simplifies saving graphs.

Example: Save with ggsave() 

library(ggplot2)
p <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()
ggsave("ggplot_graph.png", plot = p, width = 8, height = 6, dpi = 100)
  • plot: The graph to save.
  • width and height: Dimensions of the file in inches.
  • dpi: Resolution in DPI.

Saving Graphs in R Markdown

In R Markdown, graphs are automatically saved in the specified format in the document.

Example: R Markdown with Graphs 

```{r}
plot(1:10, (1:10)^2, main = "Graph in R Markdown")

Graphs will be saved in the format specified by the options in the R Markdown document.

 Summary

  • Open a File Device**: Use `pdf()`, `png()`, `jpeg()`, `tiff()` to specify the format and settings.
  • Plot the Graph**: Use plotting functions like `plot()`, `ggplot()`, etc.
  • Close the Device**: Use `dev.off()` to save and close the file.
  • Customize Settings**: Adjust size, resolution, and background color as needed.
  • Advanced Methods**: Save multiple plots in one file or use `ggsave()` for `ggplot2`.

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Facebook
Twitter
LinkedIn
WhatsApp
Email
Print