ggplot2-ggproto {ggplot2}R Documentation

Base ggproto classes for ggplot2

Description

If you are creating a new geom, stat, position, or scale in another package, you'll need to extend from ggplot2::Geom, ggplot2::Stat, ggplot2::Position, or ggplot2::Scale.

Geoms

All ⁠geom_*()⁠ functions (like geom_point()) return a layer that contains a ⁠Geom*⁠ object (like GeomPoint). The ⁠Geom*⁠ object is responsible for rendering the data in the plot.

Each of the ⁠Geom*⁠ objects is a ggproto() object, descended from the top-level Geom, and each implements various methods and fields.

Compared to Stat and Position, Geom is a little different because the execution of the setup and compute functions is split up. setup_data runs before position adjustments, and draw_layer() is not run until render time, much later.

To create a new type of Geom object, you typically will want to override one or more of the following:

See also the new geoms section of the online ggplot2 book.

Coordinate systems

All ⁠coord_*()⁠ functions (like coord_trans()) return a ⁠Coord*⁠ object (like CoordTrans).

Each of the ⁠Coord*⁠ objects is a ggproto() object, descended from the top-level Coord. To create a new type of Coord object, you typically will want to implement one or more of the following:

See also the new coords section of the online ggplot2 book.

Facets

All ⁠facet_*⁠ functions returns a Facet object or an object of a Facet subclass. This object describes how to assign data to different panels, how to apply positional scales and how to lay out the panels, once rendered.

Extending facets can range from the simple modifications of current facets, to very laborious rewrites with a lot of gtable() manipulation. For some examples of both, please see the extension vignette.

Facet subclasses, like other extendible ggproto classes, have a range of methods that can be modified. Some of these are required for all new subclasses, while other only need to be modified if need arises.

The required methods are:

In addition to the methods described above, it is also possible to override the default behaviour of one or more of the following methods:

All extension methods receive the content of the params field as the params argument, so the constructor function will generally put all relevant information into this field. The only exception is the shrink parameter which is used to determine if scales are retrained after Stat transformations has been applied.

See also the new facets section of the online ggplot2 book.

Stats

All ⁠stat_*()⁠ functions (like stat_bin()) return a layer that contains a ⁠Stat*⁠ object (like StatBin). The ⁠Stat*⁠ object is responsible for rendering the data in the plot.

Each of the ⁠Stat*⁠ objects is a ggproto() object, descended from the top-level Stat, and each implements various methods and fields. To create a new type of Stat object, you typically will want to override one or more of the following:

See also the new stats section of the online ggplot2 book.

Guides

The ⁠guide_*()⁠ functions, such as guide_legend() return an object that is responsible for displaying how objects in the plotting panel are related to actual values.

Each of the ⁠Guide*⁠ object is a ggproto() object, descended from the top-level Guide, and each implements their own methods for drawing.

To create a new type of Guide object, you typically will want to override one or more of the following:

Properties:

Methods:

Positions

All ⁠position_*()⁠ functions (like position_dodge()) return a ⁠Position*⁠ object (like PositionDodge). The ⁠Position*⁠ object is responsible for adjusting the position of overlapping geoms.

The way that the ⁠position_*⁠ functions work is slightly different from the ⁠geom_*⁠ and ⁠stat_*⁠ functions, because a ⁠position_*⁠ function actually "instantiates" the ⁠Position*⁠ object by creating a descendant, and returns that.

Each of the ⁠Position*⁠ objects is a ggproto() object, descended from the top-level Position, and each implements the following methods:

And the following fields

See also the new positions section of the online ggplot2 book.

Scales

All ⁠scale_*⁠ functions like scale_x_continuous() return a ⁠Scale*⁠ object like ScaleContinuous. Each of the ⁠Scale*⁠ objects is a ggproto() object, descended from the top-level Scale.

Properties not documented in continuous_scale() or discrete_scale():

Methods:

These methods are only valid for position (x and y) scales:

See Also

ggproto


[Package ggplot2 version 3.5.1 Index]