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 14, 2018 in Data Analytics by kappa3010
• 2,090 points
2,146 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 14, 2018 by Sahiti
• 6,370 points

Related Questions In Data Analytics

0 votes
1 answer

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

Add a limit to axis ticks using ...READ MORE

answered Nov 3, 2019 in Data Analytics by anonymous
• 33,030 points
714 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,790 points
1,877 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,660 points
9,723 views
0 votes
1 answer

R language has several packages for solving a problem. How do you make a decision on which one is the best to use?

CRAN package ecosystem has more than 6000 ...READ MORE

answered Jun 5, 2018 in Data Analytics by zombie
• 3,790 points
1,152 views
0 votes
1 answer

Working of ggplot scale_continuous 'expand' argument

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

answered Apr 13, 2018 in Data Analytics by DataKing99
• 8,250 points
3,391 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,090 points
5,057 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,700 points
1,518 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,700 points
1,333 views
+1 vote
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 14, 2018 in Data Analytics by Sahiti
• 6,370 points
7,040 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 Sahiti
• 6,370 points
3,832 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP