Search code examples
rggplot2correlationpartial

The best example to plot a correlation graph with ggplot2


I made a partial correlation analysis, with the ggm package

list = list(mtcars, mtcars)
    list = lapply(list, function(x) x %>%
                    mutate(gear = as.factor(gear)))

library(ggm)
lapply(list, function(x) {
  sapply(split(x, x$gear), function(x) {
    pcor(u = c('mpg', 'disp', 'hp', 'vs'), S = var(x))
  })
})

and with the pcor package

pcorr1 = list %>% 
  map(function(x) split(x[c('mpg', 'disp', 'hp', 'vs')], x$gear))
coeff = c("pearson", "spearman")
res = lapply(1:2, function(x) lapply(seq(coeff), function(x) {
  lapply(pcorr1[[x]], function(y) pcor(y, method = coeff[[x]]))}))

Can anyone recommend a way how to compute such correlation in a graph with ggplot2?

Thanks

UPFATE Just to make understand I am wondering wether it is possible to use the correlation coefficients as y and on x all level of grouping variable (it should be a sort of barplot)


Solution

  • I'm having trouble understanding your expected output, but perhaps you could use pairs plots to show the correlation between variables and label each plot with your ggm::pcor() value? E.g.

    library(tidyverse)
    # install.packages("ggm")
    library(ggm)
    
    list_of_mtcars = list(mtcars, mtcars)
    list_to_plot = lapply(list_of_mtcars, function(x) x %>%
                    mutate(gear = as.factor(gear)))
    
    ggm_pcor_vals <- lapply(list_to_plot, function(x) {
      sapply(split(x, x$gear), function(x) {
        pcor(u = c('mpg', 'disp', 'hp', 'vs'), S = var(x))
      })
    })
    
    pcorr1 <- list_to_plot %>% 
      map(function(x) split(x[c('mpg', 'disp', 'hp', 'vs')], x$gear))
    coeff <- c("pearson", "spearman")
    res <- lapply(1:2, function(x) lapply(seq(coeff), function(x) {
      lapply(pcorr1[[x]], function(y) cor(y, method = coeff[x]))}))
    
    library(GGally)
    #> Registered S3 method overwritten by 'GGally':
    #>   method from   
    #>   +.gg   ggplot2
    map(1:3, \(y) map(pcorr1,
         \(x) ggpairs(as.data.frame(x[[y]])) + 
           ggtitle(paste("ggm pcor mpg vs disp given hp and vs =", round(ggm_pcor_vals[[1]][[y]], 4), "when gear =", names(x)[[y]]))))
    #> [[1]]
    #> [[1]][[1]]
    

    #> 
    #> [[1]][[2]]
    

    #> 
    #> 
    #> [[2]]
    #> [[2]][[1]]
    

    #> 
    #> [[2]][[2]]
    

    #> 
    #> 
    #> [[3]]
    #> [[3]][[1]]
    

    #> 
    #> [[3]][[2]]
    

    Created on 2023-09-05 with reprex v2.0.2

    Or is this totally off the mark?

    NB. it's better to avoid using the word list as a name for your list, and also it looks like you have 'duplicate' plots in the map() output, but I think it's because you have list(mtcars, mtcars) as your input.

    Edit 1

    Based on your update, it sounds like you want a barplot of the pcor values? What do you want your barplot to look like? I.e. how would you change this:

    library(tidyverse)
    # install.packages("ggm")
    library(ggm)
    
    list_of_mtcars = list(mtcars, mtcars)
    list_to_plot = lapply(list_of_mtcars, function(x) x %>%
                            mutate(gear = as.factor(gear)))
    
    ggm_pcor_vals <- lapply(list_to_plot, function(x) {
      sapply(split(x, x$gear), function(x) {
        pcor(u = c('mpg', 'disp', 'hp', 'vs'), S = var(x))
      })
    })
    
    par(mfrow = c(1, 2))
    lapply(ggm_pcor_vals, barplot)
    

    Created on 2023-09-08 with reprex v2.0.2