Book Image

R Graph Essentials

Book Image

R Graph Essentials

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)
5
Index

Creating graphs with several curves

Let's take an example with two dependent variables and create a nice graph. Enter the following code:

X  <- c(1, 2, 3, 4, 5, 6, 7)
Y1 <- c(2, 4, 5, 7, 12, 14, 16)
Y2 <- c(3, 6, 7, 8, 9, 11, 12)

Now, we graph Y1 using a vertical axis from 0 to 20 as follows:

plot(X, Y1, type="o", pch = 17, cex=1.2, col="darkgreen", ylim=c(0, 20))

Now superpose Y2 using the following command:

lines(Y2, type="o", pch=16, lty=2, col="blue")

Notice how we plotted the first curve and then added the second using the lines() command. Let's create a title using the title() command:

title(main="A PLOT OF TWO VARIABLES", col.main="red", font.main=2)

Our graph contains two curves, each with the specified line type and symbols:

Creating graphs with several curves

Note the default labels for the horizontal and vertical axes.