Cómo escribir una prueba para un diagrama de ggplot

Tengo muchas funciones que generan gráficos, generalmente con ggplot2. En este momento, estoy generando la trama y probando los datos subyacentes. Pero me gustaría saber si hay una manera razonable de probar que el gráfico contiene las capas / opciones que espero o si los elementos gráficos coinciden con las expectativas.

Por ejemplo:

library(ggplot2)
library(scales) # for percent()
library(testthat)

df <- data.frame(
  Response = LETTERS[1:5],
  Proportion = c(0.1,0.2,0.1,0.2,0.4)
)

#' @export plot_fun
plot_fun <- function(df) {
  p1 <- ggplot(df, aes(Response, Proportion)) +
    geom_bar(stat='identity') + 
    scale_y_continuous(labels = percent)
return(p1)
}

test_that("Plot returns ggplot object",{
  p <- plot_fun(df)
  expect_is(p,"ggplot")
})

test_that("Plot uses correct data", {
  p <- plot_fun(df)
  expect_that(df, equals(p$data))

})

Aquí es donde estoy atrapado

test_that("Plot layers match expectations",{
  p <- plot_fun(df)
  expect_that(...,...)
})

test_that("Scale is labelled percent",{
  p <- plot_fun(df)
  expect_that(...,...)
})

Tal vez hay un enfoque más directo?

Respuestas a la pregunta(3)

Su respuesta a la pregunta