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()
.
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.
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()
.
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 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.
AestheticsThe following help topics give a broad overview of some of the ways you can use each aesthetic.
aes_colour_fill_alpha
colour
color
fill
aes_group_order
group
aes_linetype_size_shape
linetype
size
shape
aes_position
x
y
xmin
xmax
ymin
ymax
xend
yend
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.
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 generates small multiples, each displaying a different subset of the data. Facets are an alternative to aesthetics for displaying additional discrete variables.
LabelsThese functions provide a flexible toolkit for controlling the display of the “strip” labels on facets.
ThemesThemes 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.
These functions provides tools to help you program with ggplot2, creating functions and for-loops that generate plots for you.
Extending ggplot2To 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.
ggplot2 also provides a handful of helpers that are useful for creating visualisations.
Dataggplot2 comes with a selection of built-in datasets that are used in examples to illustrate various visualisation challenges.
Autoplot and fortifyautoplot()
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.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4