Skip to contents

This is a convenience function to allow layer objects, such as geoms, to take a subset of the data in the main ggplot() call, without storing a duplicate of the subset in the ggplot object.


ggsubset(rowtest = NULL, omit = NULL)



logical expression indicating which rows to keep.


a character column name to exclude.


A function that takes a data.frame as argument and returns a subset of that data.frame according to rowtest


ggsubset is a wrapper around where the subset argument is set to rowtest and the select argument to -omit. Since the data argument in the layer() function can take a function with one argument, we can pass the function returned from ggsubset as that argument to subset the data by rows.

See also

See ggplot2::layer(), specifically the data argument. See for the internal function.


ggplot(iris, aes(Sepal.Width, Sepal.Length)) +
  geom_point(data = ggsubset(Species == "setosa"))
#> Warning: `ggsubset()` was deprecated in ggh4x 0.2.0.
#>  This is best replaced by using `data = ~ subset(.x, ...)` instead.