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 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:

Note the default labels for the horizontal and vertical axes.