diff --git a/dev/articles/extending-ggplot2.html b/dev/articles/extending-ggplot2.html index 18ff5ba448..a3cfb4b7eb 100644 --- a/dev/articles/extending-ggplot2.html +++ b/dev/articles/extending-ggplot2.html @@ -220,9 +220,7 @@

The simplest statggplot(mpg, aes(displ, hwy)) + geom_point() + stat_chull(fill = NA, colour = "black") -

Scatterplot of engine displacement versus highway miles per
- gallon, for 234 cars. The convex hull of all the points is marked by a
- polygon with no fill.

+

Scatterplot of engine displacement versus highway miles per gallon, for 234 cars. The convex hull of all the points is marked by a polygon with no fill.

(We’ll see later how to change the defaults of the geom so that you don’t need to specify fill = NA every time.)

Once we’ve written this basic object, ggplot2 gives a lot for free. @@ -232,19 +230,14 @@

The simplest statggplot(mpg, aes(displ, hwy, colour = drv)) + geom_point() + stat_chull(fill = NA) -

Scatterplot of engine displacement versus highway miles per
- gallon, for 234 cars. The convex hulls of points, grouped and coloured by
- three types of drive train, are marked by polygons with no fill but the
- outline matches the colours of the points.

+

Scatterplot of engine displacement versus highway miles per gallon, for 234 cars. The convex hulls of points, grouped and coloured by three types of drive train, are marked by polygons with no fill but the outline matches the colours of the points.

We can also override the default geom to display the convex hull in a different way:

 ggplot(mpg, aes(displ, hwy)) + 
   stat_chull(geom = "point", size = 4, colour = "red") +
   geom_point()
-

Scatterplot of engine displacement versus highway miles per
- gallon, for 234 cars. The points that are part of the convex hull of all
- points are marked with a red outline.

+

Scatterplot of engine displacement versus highway miles per gallon, for 234 cars. The points that are part of the convex hull of all points are marked with a red outline.

Stat parameters @@ -281,9 +274,7 @@

Stat parametersggplot(mpg, aes(displ, hwy)) + geom_point() + stat_lm()

-

Scatterplot of engine displacement versus highway miles per
- gallon, for 234 cars. A straight line with a negative slope passes through
- the cloud of points.

+

Scatterplot of engine displacement versus highway miles per gallon, for 234 cars. A straight line with a negative slope passes through the cloud of points.

StatLm is inflexible because it has no parameters. We might want to allow the user to control the model formula and the number of points used to generate the grid. To do so, we add arguments to the @@ -318,10 +309,7 @@

Stat parameters geom_point() + stat_lm(formula = y ~ poly(x, 10)) + stat_lm(formula = y ~ poly(x, 10), geom = "point", colour = "red", n = 20) -

Scatterplot of engine displacement versus highway miles per
- gallon, for 234 cars. A wobbly line follows the point cloud over the
- horizontal direction. 20 points are placed on top of the line with constant
- horizontal intervals.

+

Scatterplot of engine displacement versus highway miles per gallon, for 234 cars. A wobbly line follows the point cloud over the horizontal direction. 20 points are placed on top of the line with constant horizontal intervals.

Note that we don’t have to explicitly include the new parameters in the arguments for the layer, ... will get passed to the right place anyway. But you’ll need to document them @@ -400,15 +388,12 @@

Picking defaultsggplot(mpg, aes(displ, colour = drv)) + stat_density_common() #> Picking bandwidth of 0.345 -

A line plot showing three kernel density estimates of engine displacement,
- coloured for three types of drive trains. The lines are a little bit
- wobbly.

+

A line plot showing three kernel density estimates of engine displacement, coloured for three types of drive trains. The lines are a little bit wobbly.

 
 ggplot(mpg, aes(displ, colour = drv)) + 
   stat_density_common(bandwidth = 0.5)
-

A line plot showing three kernel density estimates of engine displacement,
- coloured for three types of drive trains. The lines are fairly smooth.

+

A line plot showing three kernel density estimates of engine displacement, coloured for three types of drive trains. The lines are fairly smooth.

I recommend using NULL as a default value. If you pick important parameters automatically, it’s a good idea to message() to the user (and when printing a floating point @@ -437,19 +422,13 @@

Variable names and default aesthe ggplot(mpg, aes(displ, drv, colour = after_stat(density))) + stat_density_common(bandwidth = 1, geom = "point") -

A plot showing the engine displacement versus three types of drive
- trains. Every drive train is represented by a series of densely packed
- points that imitate a horizontal line, and their colour intensity indicates
- the kernel density estimate of the displacement.

+

A plot showing the engine displacement versus three types of drive trains. Every drive train is represented by a series of densely packed points that imitate a horizontal line, and their colour intensity indicates the kernel density estimate of the displacement.

However, using this stat with the area geom doesn’t work quite right. The areas don’t stack on top of each other:

 ggplot(mpg, aes(displ, fill = drv)) + 
   stat_density_common(bandwidth = 1, geom = "area", position = "stack")
-

An area plot showing the kernel density estimates of
- engine displacement. Three areas are shown that indicate the estimates for
- three types of drive trains separately. All areas are floored to the x-axis
- and overlap one another.

+

An area plot showing the kernel density estimates of engine displacement. Three areas are shown that indicate the estimates for three types of drive trains separately. All areas are floored to the x-axis and overlap one another.

This is because each density is computed independently, and the estimated xs don’t line up. We can resolve that issue by computing the range of the data once in setup_params().

@@ -478,17 +457,11 @@

Variable names and default aesthe ggplot(mpg, aes(displ, fill = drv)) + stat_density_common(bandwidth = 1, geom = "area", position = "stack") -

A stacked area plot showing kernel density estimates of engine displacement.
- Three areas are shown that indicate the estimates for three types of drive
- trains separately. The areas are stacked on top of one another and show
- no overlap.

+

A stacked area plot showing kernel density estimates of engine displacement. Three areas are shown that indicate the estimates for three types of drive trains separately. The areas are stacked on top of one another and show no overlap.

 ggplot(mpg, aes(displ, drv, fill = after_stat(density))) + 
   stat_density_common(bandwidth = 1, geom = "raster")
-

A heatmap showing the density of engine displacement for three types of
- drive trains. The heatmap has three rows for the drive trains, but are
- continuous in the horizontal direction. The fill intensity of the heatmap
- shows the kernel density estimates.

+

A heatmap showing the density of engine displacement for three types of drive trains. The heatmap has three rows for the drive trains, but are continuous in the horizontal direction. The fill intensity of the heatmap shows the kernel density estimates.

Exercises @@ -560,8 +533,7 @@

A simple geomggplot(mpg, aes(displ, hwy)) + geom_simple_point()

-

Scatterplot of engine displacement versus highway miles per
- gallon, for 234 cars. The points are larger than the default.

+

Scatterplot of engine displacement versus highway miles per gallon, for 234 cars. The points are larger than the default.

This is very similar to defining a new stat. You always need to provide fields/methods for the four pieces shown above: