0

I have used the following codes for the correlation matrix

library(ggplot2)
library(GGally)
ggpairs(CorrelationBINA, title="Correlation matrix of BINA dhan7",
        upper = list(continuous= wrap("cor", size = 10)),
        lower = list(continuous ="smooth"))

and got the following Correlation matrix. From the upper triangle of the matrix, I want to remove the word "Corr" and want to keep only the correlation value.

  • `ggally_cor` is the function that produces these values. It now takes a `title` argument which can be used to later or remove the word `Corr:` . You can use by setting `upper = list(continuous = wrap(ggally_cor, title=""))` – user20650 Sep 24 '21 at 01:48

1 Answers1

4

This takes a user-defined function, calculates the correlation, rounds to two decimals, and then has this text display instead of the correlation value default that has the "Corr" term:

#This function identifies correlation for each pair of variables that will go into ggpairs command written later

cor_func <- function(data, mapping, method, ...){
  x <- eval_data_col(data, mapping$x)
  y <- eval_data_col(data, mapping$y)
  
  corr <- cor(x, y, method=method, use='complete.obs')
  
  
  ggally_text(
    label = as.character(round(corr, 2)), 
    mapping = aes(),
    xP = 0.5, yP = 0.5,
    color = 'black',
    ...
  )
}


ggpairs(iris[-5], 
        upper = list(continuous = wrap(cor_func,
                                       method = 'spearman')))
Jonni
  • 804
  • 5
  • 16