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)
Explicitly setting
label.pos = 0.5seems to work for me:The default appears to be
0.5 + has.diag/3, wherehas.diagis set toTRUEwhen you specify your own customdiag.panelfunction, 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…?