For the iris data, we get the scatter plot using the pairs() function as below:
pairs(iris[1:4],
main = "Edgar Anderson's Iris Data",
lower.panel=panel.pearson,
pch = 21,
bg = c("red", "green3", "blue")[unclass(iris$Species)])
With the function panel.pearson defined as follows:
panel.pearson <- function(x, y, ...) {
horizontal <- (par("usr")[1] + par("usr")[2]) / 2;
vertical <- (par("usr")[3] + par("usr")[4]) / 2;
text(horizontal, vertical, format(abs(cor(x,y)), digits=2)) }
I needed to convert the lower panel to correlation matrix and remove the labels from the diagonal and put them along the right and bottom axes. I tried the following:
pairs(iris[1:4],
main = "Edgar Anderson's Iris Data",
labels=NULL,
lower.panel=panel.pearson,
xaxt='n',
yaxt='n',
pch = 21,
bg = c("red", "green3", "blue")[unclass(iris$Species)])
This gives me what I need. Except that I do not understand how to get the labels on the bottom and right axes (the variable labels, I mean, Sepal.Length, Sepal.Width etc..). Any help is tremendously appreciated. Thanks!
Is this what you had in mind?
I positioned the labels by trial and error. There’s probably a better way, but at least this gets the labels in (nearly) the right place.
Update: A new SO question gave me an idea for a slightly better way to position the axis labels. As the linked answer points out, you can get the current coordinates of the plot area with
par('usr'). So here’s an update to the code, based on that:It’s still not ideal, because the size of the offset is determined by trial and error. If someone knows how R determines the size of the offset between the boundary of the plot area and where the actual plot begins, then the offset can be determined programmatically also.