1% Generated by roxygen2: do not edit by hand 2% Please edit documentation in R/aaa-.r, R/geom-.r, R/annotation-custom.r, 3% R/annotation-logticks.r, R/geom-polygon.r, R/geom-map.r, R/annotation-map.r, 4% R/geom-raster.r, R/annotation-raster.r, R/axis-secondary.R, R/coord-.r, 5% R/coord-cartesian-.r, R/coord-fixed.r, R/coord-flip.r, R/coord-map.r, 6% R/coord-polar.r, R/coord-quickmap.R, R/coord-transform.r, R/facet-.r, 7% R/facet-grid-.r, R/facet-null.r, R/facet-wrap.r, R/stat-.r, R/geom-abline.r, 8% R/geom-rect.r, R/geom-bar.r, R/geom-blank.r, R/geom-boxplot.r, R/geom-col.r, 9% R/geom-path.r, R/geom-contour.r, R/geom-crossbar.r, R/geom-segment.r, 10% R/geom-curve.r, R/geom-ribbon.r, R/geom-density.r, R/geom-density2d.r, 11% R/geom-dotplot.r, R/geom-errorbar.r, R/geom-errorbarh.r, R/geom-function.R, 12% R/geom-hex.r, R/geom-hline.r, R/geom-label.R, R/geom-linerange.r, 13% R/geom-point.r, R/geom-pointrange.r, R/geom-quantile.r, R/geom-rug.r, 14% R/geom-smooth.r, R/geom-spoke.r, R/geom-text.r, R/geom-tile.r, 15% R/geom-violin.r, R/geom-vline.r, R/layout.R, R/position-.r, 16% R/position-dodge.r, R/position-dodge2.r, R/position-identity.r, 17% R/position-jitter.r, R/position-jitterdodge.R, R/position-nudge.R, 18% R/position-stack.r, R/scale-.r, R/scale-binned.R, R/scale-continuous.r, 19% R/scale-date.r, R/scale-discrete-.r, R/scale-identity.r, R/stat-bin.r, 20% R/stat-bin2d.r, R/stat-bindot.r, R/stat-binhex.r, R/stat-boxplot.r, 21% R/stat-contour.r, R/stat-count.r, R/stat-density-2d.r, R/stat-density.r, 22% R/stat-ecdf.r, R/stat-ellipse.R, R/stat-function.r, R/stat-identity.r, 23% R/stat-qq-line.R, R/stat-qq.r, R/stat-quantile.r, R/stat-smooth.r, 24% R/stat-sum.r, R/stat-summary-2d.r, R/stat-summary-bin.R, 25% R/stat-summary-hex.r, R/stat-summary.r, R/stat-unique.r, R/stat-ydensity.r 26\docType{data} 27\name{ggplot2-ggproto} 28\alias{ggplot2-ggproto} 29\alias{Geom} 30\alias{GeomCustomAnn} 31\alias{GeomLogticks} 32\alias{GeomPolygon} 33\alias{GeomMap} 34\alias{GeomAnnotationMap} 35\alias{GeomRaster} 36\alias{GeomRasterAnn} 37\alias{AxisSecondary} 38\alias{Coord} 39\alias{CoordCartesian} 40\alias{CoordFixed} 41\alias{CoordFlip} 42\alias{CoordMap} 43\alias{CoordPolar} 44\alias{CoordQuickmap} 45\alias{CoordTrans} 46\alias{Facet} 47\alias{FacetGrid} 48\alias{FacetNull} 49\alias{FacetWrap} 50\alias{Stat} 51\alias{GeomAbline} 52\alias{GeomRect} 53\alias{GeomBar} 54\alias{GeomBlank} 55\alias{GeomBoxplot} 56\alias{GeomCol} 57\alias{GeomPath} 58\alias{GeomLine} 59\alias{GeomStep} 60\alias{GeomContour} 61\alias{GeomContourFilled} 62\alias{GeomCrossbar} 63\alias{GeomSegment} 64\alias{GeomCurve} 65\alias{GeomRibbon} 66\alias{GeomArea} 67\alias{GeomDensity} 68\alias{GeomDensity2d} 69\alias{GeomDensity2dFilled} 70\alias{GeomDotplot} 71\alias{GeomErrorbar} 72\alias{GeomErrorbarh} 73\alias{GeomFunction} 74\alias{GeomHex} 75\alias{GeomHline} 76\alias{GeomLabel} 77\alias{GeomLinerange} 78\alias{GeomPoint} 79\alias{GeomPointrange} 80\alias{GeomQuantile} 81\alias{GeomRug} 82\alias{GeomSmooth} 83\alias{GeomSpoke} 84\alias{GeomText} 85\alias{GeomTile} 86\alias{GeomViolin} 87\alias{GeomVline} 88\alias{Layout} 89\alias{Position} 90\alias{PositionDodge} 91\alias{PositionDodge2} 92\alias{PositionIdentity} 93\alias{PositionJitter} 94\alias{PositionJitterdodge} 95\alias{PositionNudge} 96\alias{PositionStack} 97\alias{PositionFill} 98\alias{Scale} 99\alias{ScaleContinuous} 100\alias{ScaleDiscrete} 101\alias{ScaleBinned} 102\alias{ScaleBinnedPosition} 103\alias{ScaleContinuousPosition} 104\alias{ScaleContinuousDatetime} 105\alias{ScaleContinuousDate} 106\alias{ScaleDiscretePosition} 107\alias{ScaleDiscreteIdentity} 108\alias{ScaleContinuousIdentity} 109\alias{StatBin} 110\alias{StatBin2d} 111\alias{StatBindot} 112\alias{StatBinhex} 113\alias{StatBoxplot} 114\alias{StatContour} 115\alias{StatContourFilled} 116\alias{StatCount} 117\alias{StatDensity2d} 118\alias{StatDensity2dFilled} 119\alias{StatDensity} 120\alias{StatEcdf} 121\alias{StatEllipse} 122\alias{StatFunction} 123\alias{StatIdentity} 124\alias{StatQqLine} 125\alias{StatQq} 126\alias{StatQuantile} 127\alias{StatSmooth} 128\alias{StatSum} 129\alias{StatSummary2d} 130\alias{StatSummaryBin} 131\alias{StatSummaryHex} 132\alias{StatSummary} 133\alias{StatUnique} 134\alias{StatYdensity} 135\title{Base ggproto classes for ggplot2} 136\description{ 137If you are creating a new geom, stat, position, or scale in another package, 138you'll need to extend from \code{ggplot2::Geom}, \code{ggplot2::Stat}, 139\code{ggplot2::Position}, or \code{ggplot2::Scale}. 140} 141\section{Geoms}{ 142 143 144All \verb{geom_*()} functions (like \code{geom_point()}) return a layer that 145contains a \verb{Geom*} object (like \code{GeomPoint}). The \verb{Geom*} 146object is responsible for rendering the data in the plot. 147 148Each of the \verb{Geom*} objects is a \code{\link[=ggproto]{ggproto()}} object, descended 149from the top-level \code{Geom}, and each implements various methods and 150fields. 151 152Compared to \code{Stat} and \code{Position}, \code{Geom} is a little 153different because the execution of the setup and compute functions is 154split up. \code{setup_data} runs before position adjustments, and 155\code{draw_layer()} is not run until render time, much later. 156 157To create a new type of Geom object, you typically will want to 158override one or more of the following: 159\itemize{ 160\item Either \code{draw_panel(self, data, panel_params, coord)} or 161\code{draw_group(self, data, panel_params, coord)}. \code{draw_panel} is 162called once per panel, \code{draw_group} is called once per group. 163 164Use \code{draw_panel} if each row in the data represents a 165single element. Use \code{draw_group} if each group represents 166an element (e.g. a smooth, a violin). 167 168\code{data} is a data frame of scaled aesthetics. 169 170\code{panel_params} is a set of per-panel parameters for the 171\code{coord}. Generally, you should consider \code{panel_params} 172to be an opaque data structure that you pass along whenever you call 173a coord method. 174 175You must always call \code{coord$transform(data, panel_params)} to 176get the (position) scaled data for plotting. To work with 177non-linear coordinate systems, you typically need to convert into a 178primitive geom (e.g. point, path or polygon), and then pass on to the 179corresponding draw method for munching. 180 181Must return a grob. Use \code{\link[=zeroGrob]{zeroGrob()}} if there's nothing to 182draw. 183\item \code{draw_key}: Renders a single legend key. 184\item \code{required_aes}: A character vector of aesthetics needed to 185render the geom. 186\item \code{default_aes}: A list (generated by \code{\link[=aes]{aes()}} of 187default values for aesthetics. 188\item \code{setup_data}: Converts width and height to xmin and xmax, 189and ymin and ymax values. It can potentially set other values as well. 190} 191} 192 193\section{Coordinate systems}{ 194 195 196All \verb{coord_*()} functions (like \code{coord_trans()}) return a \verb{Coord*} 197object (like \code{CoordTrans}). 198 199Each of the \verb{Coord*} objects is a \code{\link[=ggproto]{ggproto()}} object, 200descended from the top-level \code{Coord}. To create a new type of Coord 201object, you typically will want to implement one or more of the following: 202\itemize{ 203\item \code{aspect}: Returns the desired aspect ratio for the plot. 204\item \code{labels}: Returns a list containing labels for x and y. 205\item \code{render_fg}: Renders foreground elements. 206\item \code{render_bg}: Renders background elements. 207\item \code{render_axis_h}: Renders the horizontal axes. 208\item \code{render_axis_v}: Renders the vertical axes. 209\item \code{backtransform_range(panel_params)}: Extracts the panel range provided 210in \code{panel_params} (created by \code{setup_panel_params()}, see below) and 211back-transforms to data coordinates. This back-transformation can be needed 212for coords such as \code{coord_trans()} where the range in the transformed 213coordinates differs from the range in the untransformed coordinates. Returns 214a list of two ranges, \code{x} and \code{y}, and these correspond to the variables 215mapped to the \code{x} and \code{y} aesthetics, even for coords such as \code{coord_flip()} 216where the \code{x} aesthetic is shown along the y direction and vice versa. 217\item \code{range(panel_params)}: Extracts the panel range provided 218in \code{panel_params} (created by \code{setup_panel_params()}, see below) and 219returns it. Unlike \code{backtransform_range()}, this function does not perform 220any back-transformation and instead returns final transformed coordinates. Returns 221a list of two ranges, \code{x} and \code{y}, and these correspond to the variables 222mapped to the \code{x} and \code{y} aesthetics, even for coords such as \code{coord_flip()} 223where the \code{x} aesthetic is shown along the y direction and vice versa. 224\item \code{transform}: Transforms x and y coordinates. 225\item \code{distance}: Calculates distance. 226\item \code{is_linear}: Returns \code{TRUE} if the coordinate system is 227linear; \code{FALSE} otherwise. 228\item \code{is_free}: Returns \code{TRUE} if the coordinate system supports free 229positional scales; \code{FALSE} otherwise. 230\item \code{setup_panel_params(scale_x, scale_y, params)}: Determines the appropriate 231x and y ranges for each panel, and also calculates anything else needed to 232render the panel and axes, such as tick positions and labels for major 233and minor ticks. Returns all this information in a named list. 234\item \code{setup_data(data, params)}: Allows the coordinate system to 235manipulate the plot data. Should return list of data frames. 236\item \code{setup_layout(layout, params)}: Allows the coordinate 237system to manipulate the \code{layout} data frame which assigns 238data to panels and scales. 239} 240} 241 242\section{Facets}{ 243 244 245All \verb{facet_*} functions returns a \code{Facet} object or an object of a 246\code{Facet} subclass. This object describes how to assign data to different 247panels, how to apply positional scales and how to lay out the panels, once 248rendered. 249 250Extending facets can range from the simple modifications of current facets, 251to very laborious rewrites with a lot of \code{\link[=gtable]{gtable()}} manipulation. 252For some examples of both, please see the extension vignette. 253 254\code{Facet} subclasses, like other extendible ggproto classes, have a range 255of methods that can be modified. Some of these are required for all new 256subclasses, while other only need to be modified if need arises. 257 258The required methods are: 259\itemize{ 260\item \code{compute_layout}: Based on layer data compute a mapping between 261panels, axes, and potentially other parameters such as faceting variable 262level etc. This method must return a data.frame containing at least the 263columns \code{PANEL}, \code{SCALE_X}, and \code{SCALE_Y} each containing 264integer keys mapping a PANEL to which axes it should use. In addition the 265data.frame can contain whatever other information is necessary to assign 266observations to the correct panel as well as determining the position of 267the panel. 268\item \code{map_data}: This method is supplied the data for each layer in 269turn and is expected to supply a \code{PANEL} column mapping each row to a 270panel defined in the layout. Additionally this method can also add or 271subtract data points as needed e.g. in the case of adding margins to 272\code{facet_grid()}. 273\item \code{draw_panels}: This is where the panels are assembled into a 274\code{gtable} object. The method receives, among others, a list of grobs 275defining the content of each panel as generated by the Geoms and Coord 276objects. The responsibility of the method is to decorate the panels with 277axes and strips as needed, as well as position them relative to each other 278in a gtable. For some of the automatic functions to work correctly, each 279panel, axis, and strip grob name must be prefixed with "panel", "axis", and 280"strip" respectively. 281} 282 283In addition to the methods described above, it is also possible to override 284the default behaviour of one or more of the following methods: 285\itemize{ 286\item \code{setup_params}: 287\item \code{init_scales}: Given a master scale for x and y, create panel 288specific scales for each panel defined in the layout. The default is to 289simply clone the master scale. 290\item \code{train_scales}: Based on layer data train each set of panel 291scales. The default is to train it on the data related to the panel. 292\item \code{finish_data}: Make last-minute modifications to layer data 293before it is rendered by the Geoms. The default is to not modify it. 294\item \code{draw_back}: Add a grob in between the background defined by the 295Coord object (usually the axis grid) and the layer stack. The default is to 296return an empty grob for each panel. 297\item \code{draw_front}: As above except the returned grob is placed 298between the layer stack and the foreground defined by the Coord object 299(usually empty). The default is, as above, to return an empty grob. 300\item \code{draw_labels}: Given the gtable returned by \code{draw_panels}, 301add axis titles to the gtable. The default is to add one title at each side 302depending on the position and existence of axes. 303} 304 305All extension methods receive the content of the params field as the params 306argument, so the constructor function will generally put all relevant 307information into this field. The only exception is the \code{shrink} 308parameter which is used to determine if scales are retrained after Stat 309transformations has been applied. 310} 311 312\section{Stats}{ 313 314 315All \verb{stat_*()} functions (like \code{stat_bin()}) return a layer that 316contains a \verb{Stat*} object (like \code{StatBin}). The \verb{Stat*} 317object is responsible for rendering the data in the plot. 318 319Each of the \verb{Stat*} objects is a \code{\link[=ggproto]{ggproto()}} object, descended 320from the top-level \code{Stat}, and each implements various methods and 321fields. To create a new type of Stat object, you typically will want to 322override one or more of the following: 323\itemize{ 324\item One of : 325\code{compute_layer(self, data, scales, ...)}, 326\code{compute_panel(self, data, scales, ...)}, or 327\code{compute_group(self, data, scales, ...)}. 328 329\code{compute_layer()} is called once per layer, \code{compute_panel_()} 330is called once per panel, and \code{compute_group()} is called once per 331group. All must return a data frame. 332 333It's usually best to start by overriding \code{compute_group}: if 334you find substantial performance optimisations, override higher up. 335You'll need to read the source code of the default methods to see 336what else you should be doing. 337 338\code{data} is a data frame containing the variables named according 339to the aesthetics that they're mapped to. \code{scales} is a list 340containing the \code{x} and \code{y} scales. There functions are called 341before the facets are trained, so they are global scales, not local 342to the individual panels.\code{...} contains the parameters returned by 343\code{setup_params()}. 344\item \code{finish_layer(data, params)}: called once for each layer. Used 345to modify the data after scales has been applied, but before the data is 346handed of to the geom for rendering. The default is to not modify the 347data. Use this hook if the stat needs access to the actual aesthetic 348values rather than the values that are mapped to the aesthetic. 349\item \code{setup_params(data, params)}: called once for each layer. 350Used to setup defaults that need to complete dataset, and to inform 351the user of important choices. Should return list of parameters. 352\item \code{setup_data(data, params)}: called once for each layer, 353after \code{setup_params()}. Should return modified \code{data}. 354Default methods removes all rows containing a missing value in 355required aesthetics (with a warning if \code{!na.rm}). 356\item \code{required_aes}: A character vector of aesthetics needed to 357render the geom. 358\item \code{default_aes}: A list (generated by \code{\link[=aes]{aes()}} of 359default values for aesthetics. 360} 361} 362 363\section{Positions}{ 364 365 366All \verb{position_*()} functions (like \code{position_dodge()}) return a 367\verb{Position*} object (like \code{PositionDodge}). The \verb{Position*} 368object is responsible for adjusting the position of overlapping geoms. 369 370The way that the \verb{position_*} functions work is slightly different from 371the \verb{geom_*} and \verb{stat_*} functions, because a \verb{position_*} 372function actually "instantiates" the \verb{Position*} object by creating a 373descendant, and returns that. 374 375Each of the \verb{Position*} objects is a \code{\link[=ggproto]{ggproto()}} object, 376descended from the top-level \code{Position}, and each implements the 377following methods: 378\itemize{ 379\item \code{compute_layer(self, data, params, panel)} is called once 380per layer. \code{panel} is currently an internal data structure, so 381this method should not be overridden. 382\item \code{compute_panel(self, data, params, scales)} is called once per 383panel and should return a modified data frame. 384 385\code{data} is a data frame containing the variables named according 386to the aesthetics that they're mapped to. \code{scales} is a list 387containing the \code{x} and \code{y} scales. There functions are called 388before the facets are trained, so they are global scales, not local 389to the individual panels. \code{params} contains the parameters returned by 390\code{setup_params()}. 391\item \code{setup_params(data, params)}: called once for each layer. 392Used to setup defaults that need to complete dataset, and to inform 393the user of important choices. Should return list of parameters. 394\item \code{setup_data(data, params)}: called once for each layer, 395after \code{setup_params()}. Should return modified \code{data}. 396Default checks that required aesthetics are present. 397} 398 399And the following fields 400\itemize{ 401\item \code{required_aes}: a character vector giving the aesthetics 402that must be present for this position adjustment to work. 403} 404} 405 406\section{Scales}{ 407 408 409All \verb{scale_*} functions like \code{\link[=scale_x_continuous]{scale_x_continuous()}} return a \verb{Scale*} 410object like \code{ScaleContinuous}. Each of the \verb{Scale*} objects is a \code{\link[=ggproto]{ggproto()}} 411object, descended from the top-level \code{Scale}. 412 413Properties not documented in \code{\link[=continuous_scale]{continuous_scale()}} or \code{\link[=discrete_scale]{discrete_scale()}}: 414\itemize{ 415\item \code{call} The call to \code{\link[=continuous_scale]{continuous_scale()}} or \code{\link[=discrete_scale]{discrete_scale()}} that constructed 416the scale. 417\item \code{range} One of \code{continuous_range()} or \code{discrete_range()}. 418} 419 420Methods: 421\itemize{ 422\item \code{is_discrete()} Returns \code{TRUE} if the scale is a discrete scale 423\item \code{is_empty()} Returns \code{TRUE} if the scale contains no information (i.e., 424it has no information with which to calculate its \code{limits}). 425\item \code{clone()} Returns a copy of the scale that can be trained 426independently without affecting the original scale. 427\item \code{transform()} Transforms a vector of values using \code{self$trans}. 428This occurs before the \code{Stat} is calculated. 429\item \code{train()} Update the \code{self$range} of observed (transformed) data values with 430a vector of (possibly) new values. 431\item \code{reset()} Reset the \code{self$range} of observed data values. For discrete 432position scales, only the continuous range is reset. 433\item \code{map()} Map transformed data values to some output value as 434determined by \code{self$rescale()} and \code{self$palette} (except for position scales, 435which do not use the default implementation of this method). The output corresponds 436to the transformed data value in aesthetic space (e.g., a color, line width, or size). 437\item \code{rescale()} Rescale transformed data to the the range 0, 1. This is most useful for 438position scales. For continuous scales, \code{rescale()} uses the \code{rescaler} that 439was provided to the constructor. \code{rescale()} does not apply \code{self$oob()} to 440its input, which means that discrete values outside \code{limits} will be \code{NA}, and 441values that are outside \code{range} will have values less than 0 or greater than 1. 442This allows guides more control over how out-of-bounds values are displayed. 443\item \code{transform_df()}, \code{train_df()}, \code{map_df()} These \verb{_df} variants 444accept a data frame, and apply the \code{transform}, \code{train}, and \code{map} methods 445(respectively) to the columns whose names are in \code{self$aesthetics}. 446\item \code{get_limits()} Calculates the final scale limits in transformed data space 447based on the combination of \code{self$limits} and/or the range of observed values 448(\code{self$range}). 449\item \code{get_breaks()} Calculates the final scale breaks in transformed data space 450based on on the combination of \code{self$breaks}, \code{self$trans$breaks()} (for 451continuous scales), and \code{limits}. Breaks outside of \code{limits} are assigned 452a value of \code{NA} (continuous scales) or dropped (discrete scales). 453\item \code{get_labels()} Calculates labels for a given set of (transformed) \code{breaks} 454based on the combination of \code{self$labels} and \code{breaks}. 455\item \code{get_breaks_minor()} For continuous scales, calculates the final scale minor breaks 456in transformed data space based on the rescaled \code{breaks}, the value of \code{self$minor_breaks}, 457and the value of \code{self$trans$minor_breaks()}. Discrete scales always return \code{NULL}. 458\item \code{make_title()} Hook to modify the title that is calculated during guide construction 459(for non-position scales) or when the \code{Layout} calculates the x and y labels 460(position scales). 461} 462 463These methods are only valid for position (x and y) scales: 464\itemize{ 465\item \code{dimension()} For continuous scales, the dimension is the same concept as the limits. 466For discrete scales, \code{dimension()} returns a continuous range, where the limits 467would be placed at integer positions. \code{dimension()} optionally expands 468this range given an expantion of length 4 (see \code{\link[=expansion]{expansion()}}). 469\item \code{break_info()} Returns a \code{list()} with calculated values needed for the \code{Coord} 470to transform values in transformed data space. Axis and grid guides also use 471these values to draw guides. This is called with 472a (usually expanded) continuous range, such as that returned by \code{self$dimension()} 473(even for discrete scales). The list has components \code{major_source} 474(\code{self$get_breaks()} for continuous scales, or \code{seq_along(self$get_breaks())} 475for discrete scales), \code{major} (the rescaled value of \code{major_source}, ignoring 476\code{self$rescaler}), \code{minor} (the rescaled value of \code{minor_source}, ignoring 477\code{self$rescaler}), \code{range} (the range that was passed in to \code{break_info()}), 478\code{labels} (the label values, one for each element in \code{breaks}). 479\item \code{axis_order()} One of \code{c("primary", "secondary")} or \code{c("secondary", "primary")} 480\item \code{make_sec_title()} Hook to modify the title for the second axis that is calculated 481when the \code{Layout} calculates the x and y labels. 482} 483} 484 485\seealso{ 486ggproto 487} 488\keyword{datasets} 489\keyword{internal} 490