[R] pairs: adjusting margins and labeling axes

michael young nutnutnutterson at gmail.com
Tue Jul 19 19:29:37 CEST 2016


The default shape for this correlation scatterplot is rectangle.  I changed
it to square, but then the x-axis spacing between squares are off.  Is
there an easy way to change x-axis spacing between squares to that of the
y-axis spacing size?

I decided to hide the name values of the diagonal squares.  I want them
along the x and y axis instead, outside of the fixed number scale I have.
I haven't seen any online example of 'pairs' with this and all my searches
have yielded nothing.  Any ideas?  Thanks

par(pty="s")
panel.cor <- function(x, y, digits = 2, prefix="", cex.cor, ...)
{
    usr <- par("usr"); on.exit(par(usr))
    par(usr = c(0, 1, 0, 1),xlog=FALSE,ylog=FALSE)
    # correlation coefficient
    r <- cor(x, y)
    txt <- format(c(r, 0.123456789), digits = digits)[1]
    txt <- paste("r= ", txt, sep = "")
    if(missing(cex.cor)) cex.cor <- 0.8/strwidth(txt)
    text(0.5, 0.6, txt, cex=cex.cor * r)

    # p-value calculation
    p <- cor.test(x, y)$p.value
    txt2 <- format(c(p, 0.123456789), digits = digits)[1]
    txt2 <- paste("p= ", txt2, sep = "")
    if(p<0.01) txt2 <- paste("p= ", "<0.01", sep = "")
    text(0.5, 0.4, txt2)
}

pairs(iris, upper.panel = panel.cor,xlim=c(0.1,100000),
ylim=c(0.1,100000),log="xy",text.panel = NULL,pch=".")

	[[alternative HTML version deleted]]



More information about the R-help mailing list