I have a lot of functions that generate plots, typically with ggplot2. Right now, I\'m generating the plot and testing the underlying data. But I\'d like to know if there\'s
This seems to be what you're aiming at, though specific requirements for plotting parameters and contents will vary of course. But for the example you nicely crafted above these tests should all pass:
## Load the proto library for accessing sub-components of the ggplot2
## plot objects:
library(proto)
test_that("Plot layers match expectations",{
p <- plot_fun(df)
expect_is(p$layers[[1]], "proto")
expect_identical(p$layers[[1]]$geom$objname, "bar")
expect_identical(p$layers[[1]]$stat$objname, "identity")
})
test_that("Scale is labelled 'Proportion'",{
p <- plot_fun(df)
expect_identical(p$labels$y, "Proportion")
})
test_that("Scale range is NULL",{
p <- plot_fun(df)
expect_null(p$scales$scales[[1]]$range$range)
})
This question and its answers offer a good starting point on other ways to characterize ggplot objects in case you have other things you'd like to test.