Book Image

R Graph Essentials

Book Image

R Graph Essentials

Overview of this book

Table of Contents (11 chapters)
R Graph Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating and joining points


Now, let's look at graphing individual points and creating lines that join them. We start off with a simple plot that has four points. We use the plot() command and group the x coordinates together and the y coordinates together. To do this job, we use the c operator to combine the x values and y values independently. Both groups of coordinates are written within parentheses, inside the plot() command. Enter the following syntax on the command line to create a graph with four points:

plot(c(1, 2, 3, 6), c(1, 2.5, 3.8, 9.2), pch = 16)

This command gives the following plot:

Note how the elements of the first vector gave the horizontal axis values, while the elements of the second vector gave the vertical axis values. Now, we join the four points using the lines() command, again grouping the horizontal axis values together and the vertical axis values together:

lines(c(1, 2, 3, 6), c(1, 2.5, 3.8, 9.2))

The following is the resulting graph, in which the points are now connected by line segments:

When you encounter plotting commands and arguments and want to know more about them, on the R command line, enter a question mark followed immediately by the command name (for example, ?plot()) and you will be taken directly to an online help page. You can also try several online resources. One of the best is the Quick-R website (http://statmethods.net/), which I mentioned earlier. Go straight to the Basic Graphs and Advanced Graphs pages. These pages give you a very helpful summary of the main plotting parameters (symbol types, line types, and parameters that control axes, titles, labels, and legends).

Alternatively, you can use a sensible web search (for example, enter R graphs in Google) and you will find several options.