R Series

How to save plots in R?

Since R runs on so many different operating systems, and supports so many different graphics formats, it’s not surprising that there are a variety of ways of saving your plots, depending on what operating system you are using, what you plan to do with the graph, and whether you’re connecting locally or remotely.

Once you’ve created a plot in R, you may wish to save it to a file so you can use it in another document. To do this, you’ll use either the pdf()png() or jpeg() functions. These functions will save your plot to either a .pdf, .jpg, or .png file.

ArgumentsOutcome
fileThe directory and name of the final plot entered as a string. For example, to put a plot on my desktop, I’d write file = "/Users/Desktop/plot.pdf" when creating a pdf, and file = "/Users/Desktop/plot.jpg" when creating a jpeg.
width, heightThe width and height of the final plot in inches.
dev.off()This is not an argument to pdf() and jpeg(). You just need to execute this code after creating the plot to finish creating the image file (see examples).

To use these functions to save files, you need to follow 3 steps:

  1. Execute the pdf() or jpeg() functions with file, width, height arguments.
  2. Execute all your plotting code (e.g.; plot(x = 1:10, y = 1:10))
  3. Complete the file by executing the command dev.off(). This tells R that you’re done creating the file.

Let’s save a plot in pdf

# Step 1: Call the pdf command to start the plot
> pdf(file="my_plot.pdf",
      width = 4,
      height = 4)  
# The width and height are in inches

# Srep 2: Create a plot
> plot(cars, main="My_plot")

# Step 3: Run dev.off() to create the file.
> dev.off()

Save a plot as png

# Step 1: Call the pdf command to start the plot
> png(file="my_plot.png")  
# The width and height are in inches

# Srep 2: Create a plot
> plot(cars, main="My_plot")

# Step 3: Run dev.off() to create the file.
> dev.off()
saved as png

You’ll notice that after you close the plot with dev.off(), you’ll see a message in the prompt like “null device”. That’s just R telling you that you can now create plots in the main R plotting window again.

The functions pdf()jpeg(), and png() all work the same way, they just return different file types. If you can, use pdf() it saves the plot in a high quality format.

Conclusion

Hence, we studied how to save a plot in different format like pdf()jpeg(), and png() . This helps us to keep track of all the plot we created so far by saving the plot.

Zigya Acadmey

Share
Published by
Zigya Acadmey

Recent Posts

Understanding Standard Form of Numbers: An Explanation With Examples

Through the standard form offers different advantages in mathematical calculations and scientific notation. Firstly, it…

5 months ago

How to deal with stress and anxiety in college

Introduction Stress is a feeling caused by an external trigger that makes us frustrated, such…

6 months ago

Why is Sociology Important These Days?

Sociology is a broad discipline that examines societal issues. It looks at the meaningful patterns…

6 months ago

How to Convert Inches to mm

Some info about Inch Inches are a unique measure that persuades us that even the…

8 months ago

Antilogarithms – Definition, Methods, and Examples

You should be familiar with logarithms to understand antilogarithms in a better manner. Logarithms involve…

10 months ago

नाटककार सुरेंद्र वर्मा

यहां "नाटककार सुरेंद्र वर्मा" पुस्तक की पीडीएफ विद्यार्थी, शोधार्थी और जो इसका अभ्यास के लिए…

10 months ago