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) top_layer(x) bottom_layer(x) num_layers(x)
an object of class
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
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
move_layers. An integer vector of indexes
giving the positions of the matching layers in the list of layers contained
x in the case of
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
idx must be passed a
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
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
position, the topmost position will be used. As indexing in R
starts at 1, passing 0 or
"bottom" as argument for
puts the moved or appended layer(s) behind all other layers (prepends the
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(fun.data = "mean_se", colour = "red") pdelete_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")#> [] #> geom_point: na.rm = FALSE #> stat_identity: na.rm = FALSE #> position_identity #>which_layers(p, "GeomPoint")#>  1num_layers(p)#>  2top_layer(p)#>  2bottom_layer(p)#>  1num_layers(ggplot())#>  0top_layer(ggplot())#>  NAbottom_layer(ggplot())#>  NA