Save a plot as image on the disk using R

0 votes

I have plotted a simple regression plot using R. Now, I want to save that plot as a PNG or JPEG.

Is it possible to do it automatically via code

So I'm looking for an answer in 2 situations:

1.In a situation where I'm already looking at my lot on my PC and would just want to save it.

2.In a situation where I still have to generate the plot and would directly like to save it while I execute my plotting code.

Apr 13, 2018 in Data Analytics by kappa3010
• 2,010 points
66 views

1 answer to this question.

0 votes

Consider for both the situations:

1. Image will be generated in future in my R script. So, how do I save it to disk?

To save your required plot, you need to do the following:

  1. Open a device, using png(), bmp(), pdf() or similar
  2. Plot your model
  3. Close the device using dev.off()

Below is the example code for saving the plot in png format

required_fit <- lm(some ~ model)

png(filename="your/file/location/name.png")
plot(required_fit)
dev.off()

All these are described in the help page. To know about the formats, you just have to type ?pdf, ?png ,?jpeg

NOTE:

a.The image might look different on disk to the same plot directly plotted to your screen.

b.If your plot is made by either lattice or ggplot2 you have to  print the plot.

2 . The plot is already plotted and I just want to copy it as it is to a disk .

dev.print(pdf, 'filename.pdf')

The above command  should copy the image.

For any other formats, you can, replace pdf with other file types such as png, jpeg etc.

answered Apr 13, 2018 by darklord
• 6,140 points

Related Questions In Data Analytics

0 votes
0 answers

How to edit the labels and limit if a plot using ggplot? - R

How to edit the labels and limit ...READ MORE

3 days ago in Data Analytics by piyush
5 views
0 votes
1 answer

How can I print string and variable contents on the same line using R?

There are two options for doing so.  You ...READ MORE

answered May 9, 2018 in Data Analytics by zombie
• 3,690 points
22 views
0 votes
1 answer

Changing the order of bars in a bar-plot - ggplot2 - R

You can use the scale_x_discrete() function with ...READ MORE

answered May 28, 2018 in Data Analytics by Bharani
• 4,550 points
1,875 views
0 votes
1 answer
0 votes
1 answer

Working of ggplot scale_continuous 'expand' argument

According to the document if you set limits manually, ...READ MORE

answered Apr 12, 2018 in Data Analytics by DataKing99
• 8,100 points
62 views
0 votes
1 answer

Plot two variables as lines on the same graph using ggplot

If you have small number of variables, ...READ MORE

answered Apr 17, 2018 in Data Analytics by kappa3010
• 2,010 points
797 views
0 votes
1 answer

R programming: How to compute Euler's number?

The following R statement exp(1) indicates e, and exp(2) represents e^2. This ...READ MORE

answered Dec 28, 2018 by Tyrion anex
• 8,280 points
48 views
0 votes
1 answer

R programming: How to pass variables from a r program to mysql function?

To include the R variables called start.date and end.date, you can use paste to ...READ MORE

answered Dec 28, 2018 in Data Analytics by Tyrion anex
• 8,280 points
57 views
0 votes
1 answer

How to convert a list of dataframes in to a single dataframe using R?

You can use the plyr function: data <- ...READ MORE

answered Apr 13, 2018 in Data Analytics by darklord
• 6,140 points
91 views
0 votes
1 answer

How to change y axis max in time series using R?

The axis limits are being set using ...READ MORE

answered Apr 3, 2018 in Data Analytics by darklord
• 6,140 points
69 views