Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying R Graph Essentials
  • Table Of Contents Toc
R Graph Essentials

R Graph Essentials

4.5 (4)
close
close
R Graph Essentials

R Graph Essentials

4.5 (4)

Overview of this book

This book is targeted at R programmers who want to learn the graphing capabilities of R. This book will presume that you have working knowledge of R.
Table of Contents (6 chapters)
close
close
5
Index

Saving your graphs

Of course, you will need to save many of the graphs that you create. The simplest method is to click inside the graph and then copy as a metafile or copy as a bitmap. You can then save your graph in a Word document or within a PowerPoint presentation. However, you may wish to save your graphs as JPEGS, PDFs, or in other formats.

Now we shall create a PDF of a graph (a histogram that we will create using the hist() command, which you will come across later in this book). First we get ready to create a PDF (in R, we refer to this procedure as opening the PDF device driver) using the command pdf(), and then we plot. Finally, we complete the job (closing the device driver) using the command dev.off().

You may wish to save your plot to a particular directory or folder. To do so, navigate to File | Change Dir in R and select the directory or folder that you wish to use as your R working directory. For example, I selected a directory called BOOK, which is located within the following filepath on my computer:

C:\Users\David\Documents\BOOK

To confirm that this folder is now my current working folder, I entered the following command:

getwd()

The output obtained is as follows:

[1] "C:/Users/David/Documents/BOOK"

R has confirmed that its working folder is the one that I wanted. Note that R uses forward slashes for filepaths. Now, we create a vector of data and create our histogram as follows:

  y <- c(7, 18, 5, 13, 6, 17, 7, 18, 28, 7,17,28)

 pdf("My_Histogram.pdf")
 hist(y, col = "darkgreen")
 dev.off()

A PDF of your histogram should be saved in your R working directory. It is called My_Histogram.pdf and it looks like the following:

Saving your graphs

The graphing options available in R include postscript(), pdf(), bitmap(), and jpeg(). For a complete list of options, navigate to Help | Search help and enter the word devices. The list you need is labelled List of graphical devices.

For example, to create a postscript plot of the histogram, you can use the following syntax:

postscript(file="myplot.ps")
hist(y, col = "darkgreen")
dev.off()

To create and save a JPEG image from the current graph, use the dev.copy()command:

dev.copy(device=jpeg,file="picture.jpg")
dev.off()

Your image is saved in the R working directory.

You can save and recall a plot that is currently displayed on your screen. If you have a plot on your screen, then try the following commands:

x = recordPlot()
x

You can delete your plot but get it back again later in your session using the following command:

replayPlot(x)
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
R Graph Essentials
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon