Learn R Programming

ggplot2 (version 1.0.0)

+.gg: Modify a ggplot or theme object by adding on new components.

Description

This operator allows you to add objects to a ggplot or theme object.

Usage

## S3 method for class 'gg':
+(e1, e2)

e1 %+% e2

e1 %+replace% e2

Arguments

e1
An object of class ggplot or theme
e2
A component to add to e1

Details

If the first object is an object of class ggplot, you can add the following types of objects, and it will return a modified ggplot object.

  • data.frame: replace current data.frame (must use%+%)
  • uneval: replace current aesthetics
  • layer: add new layer
  • theme: update plot theme
  • scale: replace current scale
  • coord: override current coordinate system
  • facet: override current coordinate faceting

If the first object is an object of class theme, you can add another theme object. This will return a modified theme object.

For theme objects, the + operator and the %+replace% can be used to modify elements in themes.

The + operator completely replaces elements with elements from e2.

In contrast, the %+replace% operator does not replace the entire element; it only updates element properties which are present (not NULL) in the second object.

See Also

theme

Examples

Run this code
### Adding objects to a ggplot object
p <- qplot(wt, mpg, colour = hp, data = mtcars)

p + coord_cartesian(ylim = c(0, 40))
p + scale_colour_continuous(breaks = c(100, 300))
p + guides(colour = "colourbar")

# Use a different data frame
m <- mtcars[1:10, ]
p %+% m


### Adding objects to a theme object
# Compare these results of adding theme objects to other theme objects
add_el <- theme_grey() + theme(text = element_text(family = "Times"))
rep_el <- theme_grey() %+replace% theme(text = element_text(family = "Times"))

add_el$text
rep_el$text

Run the code above in your browser using DataLab