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

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