Wednesday, 12 September 2018

ggplot object test framework....

Not generic but needs to be adapted to the object.
Useful though so here it is...

Adapted from here:
https://github.com/brennanpincardiff/drawProteins/blob/master/tests/testthat/test-geoms.R
with info from here:
https://stackoverflow.com/questions/13457562/how-to-determine-the-geom-type-of-each-layer-of-a-ggplot2-object/43982598#43982598

# testing framework for a ggplot object...

# these are the parts of the ggplot object:
## p$data
## p$layers
## p$scales
## p$mapping
## p$theme
## p$coordinates
## p$facet
## p$plot_env
## p$labels


# confirm its a ggplot
expect_is(ggseg(),c("gg","ggplot"))

p <- ggseg()

# should be a list of 9
# a ggplot object should be a list of 9
expect_equal(mode(p), "list")
expect_equal(length(p), 9)

## p$data
expect_equal(dim(p$data)[1], 10913)
expect_equal(dim(p$data)[2], 11)
expect_equal(colnames(p$data)[1], "long")

## p$layers
# should have a defined number of layers
expect_equal(length(p$layers), 1)
# https://stackoverflow.com/questions/13457562/how-to-determine-the-geom-type-of-each-layer-of-a-ggplot2-object/43982598#43982598
# layer should be a specific geom
expect_equal(class(p$layers[[1]]$geom)[1], "GeomPolygon")
# if there are multiple layers we can test more than one...

## p$labels
# should have x but not y labels
expect_equal(p$labels$x, "hemisphere")
expect_equal(p$labels$y, "")

No comments:

Post a Comment