Adding an identity string to correlation graphs using the pairs () command in R

Like the previous post , I would like to change the following code ( from example in the R documentation for the pairs () command :

## put (absolute) correlations on the upper panels, ## with size proportional to the correlations. panel.cor <- function(x, y, digits = 2, prefix = "", cex.cor, ...) { usr <- par("usr"); on.exit(par(usr)) par(usr = c(0, 1, 0, 1)) r <- abs(cor(x, y)) txt <- format(c(r, 0.123456789), digits = digits)[1] txt <- paste0(prefix, txt) if(missing(cex.cor)) cex.cor <- 0.8/strwidth(txt) text(0.5, 0.5, txt, cex = cex.cor * r) } pairs(USJudgeRatings, lower.panel = panel.smooth, upper.panel = panel.cor) 

Instead of a loess line, I need an identity line for each plot. The secret lies in the $ "panel.smooth" function, but I don’t know how to modify it.

+1
source share
2 answers

I think you just mean something like this:

 my_line <- function(x,y,...){ points(x,y,...) abline(a = 0,b = 1,...) } pairs(USJudgeRatings, lower.panel = my_line, upper.panel = panel.cor) 
+4
source

Or, if you want to build a linear line, you can change joran's answer:

 my_line <- function(x,y,...){ points(x,y,...) abline(a = lm(y ~ x)$coefficients[1] , b = lm(y ~ x)$coefficients[2] , ...) } 

However, if you really use pairs, it looks like loess would be more appropriate, since you probably studied the dataset and guaranteed that the line would be extraneous at that point.

+1
source

Source: https://habr.com/ru/post/1235782/


All Articles