Book Image

R Graphs Cookbook Second Edition

Book Image

R Graphs Cookbook Second Edition

Overview of this book

Table of Contents (22 chapters)
R Graphs Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Grouping data points within a scatter plot


A basic scatter plot has a set of points plotted at the intersection of their values along x and y axes. Sometimes, we might wish to further distinguish between these points based on another value associated with the points. In this recipe, we will learn how we can group data points using colors.

Getting ready

To try out this recipe, start R and type the recipe in the command prompt. You can also choose to save the recipe as a script so that you can use it again later on.

We will also need the lattice and ggplot2 packages. The lattice package is included automatically in the base R installation, but we will need to install the ggplot2 package. To do this, run the following command in the R prompt:

install.packages("ggplot2")

How to do it...

As a first example, let's use the xyplot() command of the lattice library:

library(lattice)

xyplot(mpg~disp,
data=mtcars,
groups=cyl,
auto.key=list(corner=c(1,1)))

How it works...

In this example, we used the xyplot...