1

How to make ggpairs report the upper corner with R^2 instead of correlation?

library(GGally)    
ggpairs(mtcars[c("mpg", "disp", "hp", "drat", "wt", "qsec")])

enter image description here

user20650
  • 24,654
  • 5
  • 56
  • 91
ju.
  • 1,016
  • 1
  • 13
  • 34
  • 1
    I think that you will need to write a custom function -- see the cor_fn [here](https://stackoverflow.com/questions/37889222/change-colors-in-ggpairs-now-that-params-is-deprecated/37890371#37890371) for an example. You will need to change the correlations to lm / r^2. Remember, different to correlation the r^2 assumes a dependent so it may not be sensible. – user20650 Dec 18 '20 at 17:07

1 Answers1

3

I think that you will need to write a custom function, shown below. (One caveat to this approach is that different to correlation the r^2 assumes a dependent variable so this may not be sensible).

library(GGally) # version 1.5.0

lm_fun <- function(data, mapping, ndp=2, ...){

    # Extract the relevant columns as data
    x <- eval_data_col(data, mapping$x)
    y <- eval_data_col(data, mapping$y)

    # Calculate the r^2 & format output
    m <- summary(lm(y ~ x))
    lbl <- paste("r^2: ", formatC(m$r.squared, digits=ndp, format="f"))

    # Write out label which is centered at x&y position
    ggplot(data=data, mapping=mapping) + 
      annotate("text", x=mean(x, na.rm=TRUE), y=mean(y, na.rm=TRUE), label=lbl, parse=TRUE, ...)+
      theme(panel.grid = element_blank()) 
  }

# Call
ggpairs(mtcars[c("mpg", "disp", "hp", "drat", "wt", "qsec")], 
        upper=list(continuous=lm_fun))

EDIT: Can you please help to explain how to add a new line into the lbl between r^2 and the value?

You can use atop by changing the relevant code to either:

lbl <- substitute(atop(~r^2*':', v), 
                  list(v=formatC(m$r.squared, digits=ndp, format="f")))

or

 v <- formatC(m$r.squared, digits=ndp, format="f")
 lbl <- bquote(atop(~r^2*':', .(v))) 

You then need to tweak the annotate call to correctly parse the label

annotate("text", x=mean(x, na.rm=TRUE), y=mean(y, na.rm=TRUE), 
          label=deparse(lbl), parse=TRUE, hjust=0, ...)

I added hjust=0 in an attempt to left-align the text but this hasn't quite worked.

user20650
  • 24,654
  • 5
  • 56
  • 91
  • Can you please help to explain how to add a new line into the lbl between r^2 and the value? Adding sep='\n' in the paste(), lbl <- paste("r^2: ", formatC(m$r.squared, digits=ndp, format="f"), sep='\n') did not work. – ju. Dec 21 '20 at 22:33
  • @ju. ; please see update fr partial answer to comment – user20650 Dec 22 '20 at 01:00