2

The following code colorizes the panel backgrounds of a pairs plot in R. How can I colorize the diagonal panel (where the variable names are printed)? As you can see, I tried it but the variable names are not correctly aligned (for whatever reason).

count <- 0 
mypanel <- function(x, y, ...){

   count <<- count+1 
   bg <- if(count %in% c(1,4,9,12)) "#FDFF65" else "transparent"    ll <- par("usr") 
   rect(ll[1], ll[3], ll[2], ll[4], col=bg)    points(x, y, cex=0.5) 
}

mydiag.panel <- function(x, ...){

   ll <- par("usr") 
   rect(ll[1], ll[3], ll[2], ll[4], col="#FDFF65") }

U <- matrix(runif(4*500), ncol=4) 
pairs(U, panel=mypanel, diag.panel=mydiag.panel)
Marius Hofert
  • 6,546
  • 10
  • 48
  • 102

2 Answers2

2

Explicitly setting label.pos = 0.5 seems to work for me:

pairs(U,panel = mypanel, diag.panel=mydiag.panel,label.pos = 0.5)

The default appears to be 0.5 + has.diag/3, where has.diag is set to TRUE when you specify your own custom diag.panel function, which ends up changing the default to 0.5 + 1/3. Honestly, I'm not sure why that would be.

Possibly the thinking is that if you define your own plotting function for the diagonals, the assumption is that you're plotting data in those panels, and so it makes sense to move the default label positioning away from the center of the panel...?

joran
  • 169,992
  • 32
  • 429
  • 468
0

I was able to shade my diagonal without a complicated code:

panel.diag <- function(x,...){   
usr <- par("usr")
rect(usr[1],usr[3],usr[2],usr[4],col="pink")
}

pairs(U,upper.panel=panel.cor,lower.panel=panel.pts, diag.panel=panel.diag, label.pos=0.5)
shirleywu
  • 674
  • 10
  • 23