Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R :Plot and save in a pdf file

Tags:

r

Write R code to draw sample of size 100 from N(0,1) and N(5,1) each . Plot the two figures on the same graph.Save the graph as Sample.pdf in the working directory I.

My attempt :

 pdf("SampleGraph.pdf",width=7,height=5)
 x=rnorm(100)
 y=rnorm(100,5,1)
 plot(x,lty=2,lwd=2,col="red")
 lines(y,lty=3,col="green")
 dev.off()

It's not working.

like image 947
user2983722 Avatar asked Nov 16 '13 08:11

user2983722


People also ask

How do I export a graph from R to pdf?

Plots panel –> Export –> Save as Image or Save as PDF Specify files to save your image using a function such as jpeg(), png(), svg() or pdf(). Additional argument indicating the width and the height of the image can be also used.

How do I save multiple plots as pdf in R?

To save multiple plots to the same page in the PDF file, we use the par() function to create a grid and then add plots to the grid. In this way, all the plots are saved on the same page of the pdf file. We use the mfrow argument to the par() function to create the desired grid.

Can you save plots in R?

we plot in R programming are displayed on the screen by default. We can save these plots as a file on disk with the help of built-in functions. It is important to know that plots can be saved as bitmap image (raster) which are fixed size or as vector image which are easily resizable.

What does Dev OFF () do in R?

dev. off shuts down the specified (by default the current) device. If the current device is shut down and any other devices are open, the next open device is made current.


1 Answers

It works for me.

Check the working directory for the Sample.pdf file:

> getwd()
[1] "C:/Users/user2983722/Documents"
like image 132
Andrey Shabalin Avatar answered Oct 04 '22 03:10

Andrey Shabalin