Search icon CANCEL
Subscription
0
Cart icon
Cart
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
Arrow up icon
GO TO TOP
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
Toc

Table of Contents (11) Chapters close

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

Mapping color, shape, and size to a variable


We saw how to map a color to a categorical variable using qplot. Now we map symbol color to the three levels of ETH using ggplot. In ggplot, we map color, size, and shape within aes(); also, as we did in qplot, we select our own color scheme using scale_color_manual(), as follows:

P + geom_point(aes(color = factor(ETH)), size=I(5)) + scale_color_manual(values = c("red", "yellow", "blue"))

Now the scatterplot looks like this:

Each level of ETH now has a different color. In ggplot, we can also map symbol size and shape to factor levels, again using aes(). Try the following code yourself:

P + geom_point(aes(size = factor(ETH))) 

You will get this scatterplot:

Another way of mapping symbol size is through scale_size_area(). Try the following code yourself:

P + geom_point(aes(size = WEIGHT_2)) + scale_size_area()

The scale_size_area() layer maps symbol area onto continuous variables by dividing the continuous variable into levels. Try the following syntax...

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}