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
44 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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
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
13 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,314 views
0 votes
1 answer
0 votes
1 answer

How to create a box-plot using “plotly” in R?

You can use this command to create ...READ MORE

answered Jul 4, 2018 in Data Analytics by CodingByHeart77
• 3,680 points
456 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 12, 2018 in Data Analytics by DataKing99
• 8,100 points
46 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
646 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
32 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
52 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
61 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
42 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.