ggplot2-ggproto: Base ggproto classes for ggplot2

GeomDebugPanelR Documentation

Base ggproto classes for ggplot2

Description

Base ggproto classes for ggplot2

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:

  • Either draw_panel(self, data, panel_params, coord) or draw_group(self, data, panel_params, coord). draw_panel is called once per panel, draw_group is called once per group.

    Use draw_panel if each row in the data represents a single element. Use draw_group if each group represents an element (e.g. a smooth, a violin).

    data is a data frame of scaled aesthetics.

    panel_params is a set of per-panel parameters for the coord. Generally, you should consider panel_params to be an opaque data structure that you pass along whenever you call a coord method.

    You must always call coord$transform(data, panel_params) to get the (position) scaled data for plotting. To work with non-linear coordinate systems, you typically need to convert into a primitive geom (e.g. point, path or polygon), and then pass on to the corresponding draw method for munching.

    Must return a grob. Use zeroGrob() if there's nothing to draw.

  • draw_key: Renders a single legend key.

  • required_aes: A character vector of aesthetics needed to render the geom.

  • default_aes: A list (generated by aes() of default values for aesthetics.

  • setup_data: Converts width and height to xmin and xmax, and ymin and ymax values. It can potentially set other values as well.

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:

  • aspect: Returns the desired aspect ratio for the plot.

  • labels: Returns a list containing labels for x and y.

  • render_fg: Renders foreground elements.

  • render_bg: Renders background elements.

  • render_axis_h: Renders the horizontal axes.

  • render_axis_v: Renders the vertical axes.

  • backtransform_range(panel_params): Extracts the panel range provided in panel_params (created by setup_panel_params(), see below) and back-transforms to data coordinates. This back-transformation can be needed for coords such as coord_trans() where the range in the transformed coordinates differs from the range in the untransformed coordinates. Returns a list of two ranges, x and y, and these correspond to the variables mapped to the x and y aesthetics, even for coords such as coord_flip() where the x aesthetic is shown along the y direction and vice versa.

  • range(panel_params): Extracts the panel range provided in panel_params (created by setup_panel_params(), see below) and returns it. Unlike backtransform_range(), this function does not perform any back-transformation and instead returns final transformed coordinates. Returns a list of two ranges, x and y, and these correspond to the variables mapped to the x and y aesthetics, even for coords such as coord_flip() where the x aesthetic is shown along the y direction and vice versa.

  • transform: Transforms x and y coordinates.

  • distance: Calculates distance.

  • is_linear: Returns TRUE if the coordinate system is linear; FALSE otherwise.

  • is_free: Returns TRUE if the coordinate system supports free positional scales; FALSE otherwise.

  • setup_panel_params(scale_x, scale_y, params): Determines the appropriate x and y ranges for each panel, and also calculates anything else needed to render the panel and axes, such as tick positions and labels for major and minor ticks. Returns all this information in a named list.

  • setup_data(data, params): Allows the coordinate system to manipulate the plot data. Should return list of data frames.

  • setup_layout(layout, params): Allows the coordinate system to manipulate the layout data frame which assigns data to panels and scales.

See Also

ggproto


Yunuuuu/ggdraw documentation built on Sept. 14, 2022, 8:02 p.m.