Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

gg-add

Add components to a plot


Description

+ is the key to constructing sophisticated ggplot2 graphics. It allows you to start simple, then get more and more complex, checking your work at each step.

Usage

## S3 method for class 'gg'
e1 + e2

e1 %+% e2

Arguments

e1

An object of class ggplot() or a theme().

e2

A plot component, as described below.

What can you add?

You can add any of the following types of objects:

  • An aes() object replaces the default aesthetics.

  • A layer created by a geom_ or stat_ function adds a new layer.

  • A scale overrides the existing scale.

  • A theme() modifies the current theme.

  • A coord overrides the current coordinate system.

  • A facet specification overrides the current faceting.

To replace the current default data frame, you must use %+%, due to S3 method precedence issues.

You can also supply a list, in which case each element of the list will be added in turn.

See Also

Examples

base <- ggplot(mpg, aes(displ, hwy)) + geom_point()
base + geom_smooth()

# To override the data, you must use %+%
base %+% subset(mpg, fl == "p")

# Alternatively, you can add multiple components with a list.
# This can be useful to return from a function.
base + list(subset(mpg, fl == "p"), geom_smooth())

ggplot2

Create Elegant Data Visualisations Using the Grammar of Graphics

v3.3.3
MIT + file LICENSE
Authors
Hadley Wickham [aut] (<https://orcid.org/0000-0003-4757-117X>), Winston Chang [aut] (<https://orcid.org/0000-0002-1576-2126>), Lionel Henry [aut], Thomas Lin Pedersen [aut, cre] (<https://orcid.org/0000-0002-5147-4711>), Kohske Takahashi [aut], Claus Wilke [aut] (<https://orcid.org/0000-0002-7470-9261>), Kara Woo [aut] (<https://orcid.org/0000-0002-5125-4188>), Hiroaki Yutani [aut] (<https://orcid.org/0000-0002-3385-7233>), Dewey Dunnington [aut] (<https://orcid.org/0000-0002-9415-4582>), RStudio [cph, fnd]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.