Powered by AppSignal & Oban Pro

Function reference: index

notebooks/index.livemd

Function reference: index

About this notebook

This notebook follows ggplot2's function reference: its sections and their descriptions, in its order, with each of its pages linked to the notebook that follows that page's examples. Each notebook shows ggplot2's code for every example, draws with Giggly the ones it can draw, and names what the rest are waiting for, so together they are also a list of what remains. The descriptions are ggplot2's, about ggplot2.

Livebook opens a linked notebook as a notebook of its own. Each installs Giggly from the folder above it, so open this one from a clone of the repository.

Plot basics

All ggplot2 plots begin with a call to ggplot(), supplying default data and aesthetic mappings, specified by aes(). You then add layers, scales, coords and facets with +. To save a plot to disk, use ggsave().

Layers

Geoms

A layer combines data, aesthetic mapping, a geom (geometric object), a stat (statistical transformation), and a position adjustment. Typically, you will create layers using a geom_ function, overriding the default position and stat if needed.

Stats

A handful of layers are more easily specified with a stat_ function, drawing attention to the statistical transformation rather than the visual appearance. The computed variables can be mapped using after_stat().

Position adjustment

All layers have a position adjustment that resolves overlapping geoms. Override the default by using the position argument to the geom_ or stat_ function.

Annotations

Annotations are a special type of layer that don’t inherit global settings from the plot. They are used to add fixed reference data to plots.

Aesthetics

The following help topics give a broad overview of some of the ways you can use each aesthetic.

Scales

Scales control the details of how data values are translated to visual properties. Override the default scales to tweak details like the axis labels or legend keys, or to use a completely different translation from data to aesthetic. labs() and lims() are convenient helpers for the most common adjustments to the labels and limits.

Guides: axes and legends

The guides (the axes and legends) help readers interpret your plots. Guides are mostly controlled via the scale (e.g. with the limits, breaks, and labels arguments), but sometimes you will need additional control over guide appearance. Use guides() or the guide argument to individual scales along with guide_*() functions.

Facetting

Facetting generates small multiples, each displaying a different subset of the data. Facets are an alternative to aesthetics for displaying additional discrete variables.

Labels

These functions provide a flexible toolkit for controlling the display of the “strip” labels on facets.

Coordinate systems

The coordinate system determines how the x and y aesthetics combine to position elements in the plot. The default coordinate system is Cartesian (coord_cartesian()), which can be tweaked with coord_map(), coord_fixed(), coord_flip(), and coord_transform(), or completely replaced with coord_polar().

Themes

Themes control the display of all non-data elements of the plot. You can override all settings with a complete theme like theme_bw(), or choose to tweak individual settings by using theme() and the element_ functions. Use theme_set() to modify the active theme, affecting all future plots.

Programming with ggplot2

These functions provides tools to help you program with ggplot2, creating functions and for-loops that generate plots for you.

Extending ggplot2

To create your own geoms, stats, scales, and facets, you’ll need to learn a bit about the object oriented system that ggplot2 uses. Start by reading vignette("extending-ggplot2") then consult these functions for more details.

Vector helpers

ggplot2 also provides a handful of helpers that are useful for creating visualisations.

Data

ggplot2 comes with a selection of built-in datasets that are used in examples to illustrate various visualisation challenges.

Autoplot and fortify

autoplot() is an extension mechanism for ggplot2: it provides a way for package authors to add methods that work like the base plot() function, generating useful default plots with little user interaction. fortify() turns objects into tidy data frames: it has largely been superseded by the broom package.

  • autoplot(): Create a complete ggplot appropriate to a particular data type
  • autolayer(): Create a ggplot layer appropriate to a particular data type
  • fortify(): Fortify a model with data.
  • map_data(): Create a data frame of map data
  • automatic_plotting: Tailoring plots to particular data types