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

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)






an object of class gg to be operated upon.


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.


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


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


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




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.


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).


The functions described here are not expected to be useful in everyday plotting as one can more easily change the order in which layers are added to a ggplot. However, if one uses high level methods or functions that automatically produce a full plot using 'ggplot2' internally, one may need to add, move or delete layers so as to profit from such canned methods and retain enough flexibility.



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( = "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")
#> [[1]] #> geom_point: na.rm = FALSE #> stat_identity: na.rm = FALSE #> position_identity #>
which_layers(p, "GeomPoint")
#> [1] 1
#> [1] 2
#> [1] 2
#> [1] 1
#> [1] 0
#> [1] NA
#> [1] NA