You should check out ?pairs
. It's great for doing scatterplots for combinations of variables, but varying the type of graph that is displayed on the
- lower triangle (pass function taking 2 variables to
lower.panel
argument)
- e.g.,
pairs(df, lower.panel=points
for a scatter plot
- diagonal (pass function taking 1 variable to
diag.panel
or text.panel
argument)
- this is tricky! see help file for how to do a histogram
- upper triangle (pass function taking 2 variables to
upper.panel
argument)
- e.g.,
pairs(df, upper.panel=function(x,y)text(0,0,cor(x,y)))
, but see below
Scatter plot and correlation, from help file (?pairs
):
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)
With your data, do the following:
pairs(df, lower.panel=plot, upper.panel=panel.cor)
I love that function, and use it myself as-is. It might look a little odd with only an x
and a y
, but