How to write a test for a ggplot plot

前端 未结 3 1443
别那么骄傲
别那么骄傲 2020-12-29 19:19

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

3条回答
  •  醉梦人生
    2020-12-29 20:10

    What I also find useful in addition to the existing answers, is to test if a plot can actually be printed.

    library(ggplot2)
    library(scales) # for percent()
    library(testthat)
    
    # First, 'correct' data frame
    df <- data.frame(
        Response   = LETTERS[1:5],
        Proportion = c(0.1,0.2,0.1,0.2,0.4)
    )
    
    # Second data frame where column has 'wrong' name that does not match aes()
    df2 <- data.frame(
        x          = LETTERS[1:5],
        Proportion = c(0.1,0.2,0.1,0.2,0.4)
    )
    
    plot_fun <- function(df) {
        p1 <- ggplot(df, aes(Response, Proportion)) +
            geom_bar(stat='identity') + 
            scale_y_continuous(labels = percent)
        return(p1)
    }
    
    # All tests succeed
    test_that("Scale is labelled 'Proportion'",{
        p <- plot_fun(df)
        expect_true(is.ggplot(p))
        expect_identical(p$labels$y, "Proportion")
    
        p <- plot_fun(df2)
        expect_true(is.ggplot(p))
        expect_identical(p$labels$y, "Proportion")
    })
    
    # Second test with data frame df2 fails
    test_that("Printing ggplot object actually works",{
        p <- plot_fun(df)
        expect_error(print(p), NA)
    
        p <- plot_fun(df2)
        expect_error(print(p), NA)
    })
    #> Error: Test failed: 'Printing ggplot object actually works'
    #> * `print(p)` threw an error.
    #> Message: object 'Response' not found
    #> Class:   simpleError/error/condition
    

提交回复
热议问题