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 curves for each factor level


Let's see how to produce multiple curves in ggplot. We use the Children dataset (refer to the Creating multiple curves simultaneously section in Chapter 3, Mastering the qplot Function) to produce a line graph of height against age for each child. We created this particular graph in qplot and now we create it in ggplot, but we amend it a little. You can cut and paste the data directly from the code file for this chapter. Remember that the data for each child is arranged in a single column that holds six measurements of height for each child. We will include large points (size = 3) and slightly heavier lines (lwd = 1.2) using geom_point() and geom_line(), respectively. We will also map a color to the variable Child, so that both points and lines have a unique color for each child.

Finally, we impose our own color scheme using scale_color_manual():

ggplot(cheight, aes(x=Age, y=Height, color = factor(Child))) + geom_point(size = 3) +  geom_line(lwd = 0.7...
lock icon The rest of the chapter is locked
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}