rggally

Is there a way to add robust correlations and significance value/stars using WRS2 in GGally: ggpairs plots?


ggpairs plot computes correlations using base cor method only. How can we supply the pbcor or wincor values/p-values (from WRS2 package) to the plot?


Solution

  • ggpairs allows you to provide custom functions for the different facets of your matrix (as arguments to upper, lower and diag). There is also a ggally_statistic function that allows you to print results from other functions/tests into those facets. By wrapping this function (using the wrapping mechanism provided by the GGally package) with the correct parameters, we can easily create a custom function using the required correlation measure.

    library(GGally)
    
    d <- data.frame(A = rnorm(100),
                    B = rnorm(100),
                    C = rnorm(100))
    
    mcor <- wrap(ggally_statistic,
                 title = "pbcor", # name of the metric to be displayed
                 text_fn = function(x, y) {
                   corObj <- WRS2::pbcor(x, y) # use the desired function here or add arguments
                   cor_txt <- formatC(corObj$cor, digits = 3, format = "f")
                   cor_txt <- stringr::str_c(cor_txt, signif_stars(corObj$p.value))
                   cor_txt
                 })
    ggpairs(d, upper = list(continuous = mcor))
    

    Example of a ggpairs matrix with pbcor