1

I have pairs of several data sets and I want to add the correlation coefficient to each one so that the correlations can be visually seen. Ex:

corr1 <- cor(richstruc[,c(4,10,11)])
pairs(richstruc[,c(4,10,11)], main = "comparison 1")

Is there any way I can add the annotations "p = (the corresponding value from corr1)" to each corresponding subplot in the pair?

Brian Tompsett - 汤莱恩
  • 5,438
  • 68
  • 55
  • 126
Nate
  • 73
  • 1
  • 7
  • When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick May 16 '18 at 16:53

1 Answers1

0

Using the argument panel, you can control what will be displayed in each panel. You can customize that to add a marginal label. I increased the margin size (gap=2) and decreased the font size (cex=0.8) to make the text fit. Since you do not provide your data, I will illustrate with the built-in iris data.

Display = function(x,y) {
    points(x,y, pch=16, col=rainbow(3)[iris$Species])
    CXY = round(cor(x,y), 3)
    mtext(paste("cor =", CXY), 1, cex=0.8)
}
pairs(iris[1:3], panel=Display, gap=2)

Pairs with correlations

G5W
  • 34,378
  • 10
  • 39
  • 71