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

How it works...

Let's start from the top and discuss various parts of the plot:

> plot <- ggplot(bike,aes(temp,cnt))

First, we draw the plot. At this point, the graph is not printed as we have not added layers to it. ggplot needs at least one layer to display the graph. Next we add points to produce the scatter plot using the geom_point geometry:

plot + geom_point(size=3, aes(color=factor(windspeed)))

We can use various arguments to control how the points appear--the color of the dots and the size and shape of the dots. We can also use the aes argument to add aesthetics to this layer as well:

geom_smooth(method="lm", se=FALSE, col="red")

Adding geom_smooth helps you see a pattern. The method=lm argument uses a linear model as the smoothing ...

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