gginnards (version 0.0.1)

delete_layers: Layer manipulation

Description

Delete, move or append one or more layers in a ggplot.

Usage

delete_layers(x, match_type = NULL, idx = NULL)

append_layers(x, object, position = "top")

move_layers(x, match_type = NULL, position = "top", idx = NULL)

shift_layers(x, match_type = NULL, idx = NULL, shift = 1L)

which_layers(x, match_type = NULL, idx = NULL)

extract_layers(x, match_type = NULL, idx = NULL)

top_layer(x)

bottom_layer(x)

num_layers(x)

Arguments

x

an object of class gg to be operated upon.

match_type

The name of the ggproto object class for the geom(s), position(s) or stat(s) matching that of the layers to be operated upon.

idx

integer vector Index into the list of layers used to select the layers to be operated upon.

object

a ggplot layer created by a geom_ or stat_ function or a list of such layers or an empty list.

position

character or interger, the position of the layer immediately above of which to move or append the moved or appended layers.

shift

integer.

Value

An edited copy of x for delete_layers, append_layers and move_layers. An integer vector of indexes giving the positions of the matching layers in the list of layers contained in x in the case of which_layers.

Details

These functions must be used with care as they select all layers matching the provided geom, position or stat ggproto object class. Layers added with a stat do use a geom, and vice versa.

One and only one of match_type and idx must be passed a non-null argument.

In plots with several layers, it is possible that more than one layer matches the class name passed to match_type. It is also possible to pass a numeric vector with multiple indexes through parameter idx. In both cases multiple layers will be operated upon, but their relative positions will remain unchanged.

If a numeric vector with multiple position indexes is supplied as argument for position, the topmost position will be used. As indexing in R starts at 1, passing 0 or "bottom" as argument for position puts the moved or appended layer(s) behind all other layers (prepends the layer).

References

https://stackoverflow.com/questions/13407236/remove-a-layer-from-a-ggplot2-chart

Examples

Run this code
# NOT RUN {
library(ggplot2)

df <- data.frame(
  gp = factor(rep(letters[1:3], each = 10)),
  y = rnorm(30)
)
p <- ggplot(df, aes(gp, y)) +
     geom_point() +
     stat_summary(fun.data = "mean_se", colour = "red")
p
delete_layers(p, "GeomPoint")
delete_layers(p, "StatSummary")
move_layers(p, "GeomPoint", position = "top")
move_layers(p, "GeomPointrange", position = "bottom")
move_layers(p, "StatSummary", position = "bottom")
move_layers(p, "GeomPointrange", position = 1L)
append_layers(p, geom_line(colour = "orange"), position = "bottom")
append_layers(p, geom_line(colour = "orange"), position = 1L)
extract_layers(p, "GeomPoint")
which_layers(p, "GeomPoint")
num_layers(p)
top_layer(p)
bottom_layer(p)
num_layers(ggplot())
top_layer(ggplot())
bottom_layer(ggplot())

# }

Run the code above in your browser using DataCamp Workspace