Saving Plots

R Graphics
R Programming
Learn how to save plots created in R. This lecture covers essential techniques for exporting plots to various file formats using R.
Author

TERE

Published

June 21, 2024

Introduction

Saving plots is an important aspect of data visualization, allowing you to share your work, include plots in reports or presentations, and preserve your visualizations for future use. In this lecture, we will learn how to save plots created in R to various file formats.

Key Concepts

1. File Formats

R supports saving plots to several file formats, including:

  • PNG: Portable Network Graphics

  • JPEG: Joint Photographic Experts Group

  • PDF: Portable Document Format

  • SVG: Scalable Vector Graphics

2. Functions for Saving Plots

R provides several functions for saving plots to different file formats:

  • png(): Save a plot as a PNG file.

  • jpeg(): Save a plot as a JPEG file.

  • pdf(): Save a plot as a PDF file.

  • svg(): Save a plot as an SVG file.

  • ggsave(): Save a ggplot2 plot to a file.

Saving Plots in R

1. Saving Base R Plots

Saving as PNG


# Creating a sample plot

plot(rnorm(100), main = "Sample Plot")



# Saving the plot as a PNG file

png("sample_plot.png")

plot(rnorm(100), main = "Sample Plot")

dev.off()

Saving as JPEG


# Saving the plot as a JPEG file

jpeg("sample_plot.jpg")

plot(rnorm(100), main = "Sample Plot")

dev.off()

Saving as PDF


# Saving the plot as a PDF file

pdf("sample_plot.pdf")

plot(rnorm(100), main = "Sample Plot")

dev.off()

Saving as SVG


# Saving the plot as an SVG file

svg("sample_plot.svg")

plot(rnorm(100), main = "Sample Plot")

dev.off()

2. Saving ggplot2 Plots

The ggsave() function is specifically designed for saving ggplot2 plots and can save to various file formats.


# Installing and loading ggplot2

install.packages("ggplot2")

library(ggplot2)



# Creating a sample ggplot2 plot

p <- ggplot(data.frame(x = rnorm(100), y = rnorm(100)), aes(x = x, y = y)) +

  geom_point() +

  labs(title = "Sample ggplot2 Plot")



# Saving the plot using ggsave()

ggsave("sample_ggplot2_plot.png", plot = p)

ggsave("sample_ggplot2_plot.pdf", plot = p)

3. Customizing Plot Size and Resolution

You can customize the size and resolution of the saved plot using the width, height, and dpi parameters in the ggsave() function.


# Saving the plot with custom size and resolution

ggsave("sample_ggplot2_plot_custom.png", plot = p, width = 10, height = 6, dpi = 300)

4. Example: Comprehensive Plot Saving

Here’s a comprehensive example of saving plots in R.


# Creating a sample base R plot

plot(rnorm(100), main = "Sample Base R Plot")



# Saving the base R plot as a PNG file

png("sample_base_plot.png")

plot(rnorm(100), main = "Sample Base R Plot")

dev.off()



# Saving the base R plot as a JPEG file

jpeg("sample_base_plot.jpg")

plot(rnorm(100), main = "Sample Base R Plot")

dev.off()



# Saving the base R plot as a PDF file

pdf("sample_base_plot.pdf")

plot(rnorm(100), main = "Sample Base R Plot")

dev.off()



# Saving the base R plot as an SVG file

svg("sample_base_plot.svg")

plot(rnorm(100), main = "Sample Base R Plot")

dev.off()



# Creating a sample ggplot2 plot

p <- ggplot(data.frame(x = rnorm(100), y = rnorm(100)), aes(x = x, y = y)) +

  geom_point() +

  labs(title = "Sample ggplot2 Plot")



# Saving the ggplot2 plot using ggsave()

ggsave("sample_ggplot2_plot.png", plot = p)

ggsave("sample_ggplot2_plot.pdf", plot = p)



# Saving the ggplot2 plot with custom size and resolution

ggsave("sample_ggplot2_plot_custom.png", plot = p, width = 10, height = 6, dpi = 300)

Summary

In this lecture, we covered how to save plots created in R to various file formats. We explored several functions for saving plots, including png(), jpeg(), pdf(), svg(), and ggsave(). We also learned how to customize the size and resolution of the saved plots.

Further Reading

For more detailed information, consider exploring the following resources:

Call to Action

If you found this lecture helpful, make sure to check out the other lectures in the R Graphs series. Happy plotting!