Graphics Reference
In-Depth Information
show higher correlations in a larger font. Don't worry about the details for now—just paste this
code into your R session or script:
panel.cor
<-
function
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, use
=
"complete.obs"
))
txt
<-
format(c(r,
0.123456789
), digits
=
digits)[
1
]
txt
<-
paste(prefix, txt, sep
=
""
)
iif
if(missing(cex.cor)) cex.cor
<-
0.8
/
strwidth(txt)
text(
0.5
,
0.5
, txt, cex
=
cex.cor
*
(
1
+
r)
/
2
)
}
To show histograms of each variable along the diagonal, we'll define
panel.hist
:
panel.hist
<-
function
function
(x,
...
) {
usr
<-
par(
"usr"
)
on.exit(par(usr))
par(usr
=
c(usr[
1
:
2
],
0
,
1.5
) )
h
<-
hist(x, plot
=
FALSE
FALSE
)
breaks
<-
h$breaks
nB
<-
length(breaks)
y
<-
h$counts
y
<-
y
/
max(y)
rect(breaks[
-
nB],
0
, breaks[
-1
], y, col
=
"white"
,
...
)
}
Both of these panel functions are taken from the
pairs
help page, so if it's more convenient,
you can simply open that help page, then copy and paste. The last line of this version of the
pan-
el.cor
function is slightly modified, however, so that the changes in font size aren't as extreme
as with the original.
Now that we've defined these functions we can use them for our scatter plot matrix, by telling
pairs()
to use
panel.cor
for the upper panels and
panel.hist
for the diagonal panels.
We'll also throw in one more thing:
panel.smooth
for the lower panels, which makes a scatter
plot and adds a LOWESS smoothed line, as shown in
Figure 5-37
. (LOWESS is slightly differ-
ent from LOESS, which we saw in
Adding Fitted Regression Model Lines
, but the differences
aren't important for this sort of rough exploratory visualization):
pairs(c2009[,
2
:
5
], upper.panel
=
panel.cor,
diag.panel
=
panel.hist,
lower.panel
=
panel.smooth)