O'Reilly logo

R Data Analysis Cookbook - Second Edition by Kuntal Ganguly

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

Color specific points on a scatterplot

Using the following code, you can first generate the scatterplot and then color the points with different colors corresponding to different values of cylinders. Note that mpg and weight are in different orders in the plot and points function invocations. This is because in plot, we ask the system to plot mpg as a function of weight, whereas in the points function, we just supply a set of (x,y) coordinates to plot.

> # first generate the empty plot > # to get the axes for the whole dat > plot(mpg ~ horsepower, type = "n") > # Then plot the points in different colors > with(subset(auto, cylinders == "8cyl"),     points(horsepower, mpg, col = "blue")) > with(subset(auto, cylinders == "6cyl"), points(horsepower, ...

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