This is kind of a hacky solution to check whether a ggplot object contains a layer with the class GeomLine. The function has_geom_line() iterates over the layers to check class inheritance. It returns TRUE if any layers are geom_line(), FALSE otherwise.
library(ggplot2)
library(magrittr)
has_geom_line <- function(plot) {
stopifnot(is.ggplot(plot))
any(purrr::map_lgl(plot$layers, ~inherits(.x$geom, "GeomLine")))
}
p <-
mtcars %>%
ggplot(aes(hp, mpg)) +
geom_line() +
geom_point()
has_geom_line(p)
#> [1] TRUE