O'Reilly logo

R Graph Essentials by David Alexander Lillis

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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:

Mapping color, shape, and size to a variable

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required