Search icon
Subscription
0
Cart icon
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
R Graph Essentials

You're reading from  R Graph Essentials

Product type Book
Published in Sep 2014
Publisher
ISBN-13 9781783554553
Pages 190 pages
Edition 1st Edition
Languages

Table of Contents (11) Chapters

R Graph Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. Base Graphics in R – One Step at a Time 2. Advanced Functions in Base Graphics 3. Mastering the qplot Function 4. Creating Graphs with ggplot 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.

You have been reading a chapter from
R Graph Essentials
Published in: Sep 2014 Publisher: ISBN-13: 9781783554553
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}