R Programming language - R Save Plot

https://‮ww‬w.theitroad.com

To save a plot in R, you can use the dev.copy() function to copy the plot to a device, and then use the dev.off() function to save the plot to a file.

Here is an example of how to save a plot to a PNG file:

# Create a plot
x <- 1:10
y <- x^2
plot(x, y)

# Copy the plot to a PNG file
png("myplot.png")
dev.copy()
dev.off()

This will create a PNG file named myplot.png in your working directory containing the plot.

If you want to save the plot in a different file format, you can change the argument of the png() function to the appropriate file extension (e.g., .pdf for a PDF file).

You can also specify the dimensions of the output file using the width and height arguments of the png() function. For example, to create a PNG file with a width of 800 pixels and a height of 600 pixels, you can use the following code:

# Create a plot
x <- 1:10
y <- x^2
plot(x, y)

# Copy the plot to a PNG file with dimensions 800x600 pixels
png("myplot.png", width = 800, height = 600)
dev.copy()
dev.off()

Note that if you are using RStudio, you can also save a plot by clicking on the "Export" button in the Plots pane, and selecting the desired file format. This will automatically save the plot with the appropriate dimensions and resolution.