The standard function for correlation plots in R is pairs(), which generates a matrix of scatter plots based on all pairwise combinations of variables in a data object. The standard graph looks something like this after a little color enhancement:” Click to enlarge
The code behind this plot is simple:
1 2 3 4 |
pairs(iris[1:4], main = "Anderson's Iris Data", pch = 21, bg = c("red", "green2", "steelblue4")[unclass(iris$Species)]) |
Meanwhile, new package extensions for R have made better use of the graphical systems intrinsic to R. One of those packages is ggplot2. Another closely related package is GGally, which makes use of multi-layerd graph layouts to extend the basic correlation plot in R. The alternative approach relies on the function ggpairs() to allow users to control what information appears where in a matrix of plots. As a result, a range of text and graph types can now be blended into the correlation plot layout. For example:
The code is a little more demanding, but still relatively simple, and the resulting graph is much more informative:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# Load packages library(ggplot2, quietly = TRUE) library(GGally, quietly = TRUE) library(grid, quietly = TRUE) # Load theme for ggplot2 theme_set(theme_bw()) # Generate plot plot.layout <- ggpairs(iris, title = "Anderson's Iris Data", axisLabels="show", color = "Species") plot.text <- ggally_text(paste0("Observations\nPer Variable:\n", dim(iris)[1]), aes(color="black")) plot.cor1 <- ggally_cor(iris, mapping = aes(x = "Sepal.Length", y = "Sepal.Width", color = "black"), corSize = 4.5) plot.cor2 <- ggally_cor(iris, mapping = aes(x = "Sepal.Length", y = "Petal.Length", color = "black"), corSize = 4.5) plot.cor3 <- ggally_cor(iris, mapping = aes(x = "Sepal.Length", y = "Petal.Width", color = "black"), corSize = 4.5) plot.cor4 <- ggally_cor(iris, mapping = aes(x = "Sepal.Width", y = "Petal.Length", color = "black"), corSize = 4.5) plot.cor5 <- ggally_cor(iris, mapping = aes(x = "Sepal.Width", y = "Petal.Width", color = "black"), corSize = 4.5) plot.cor6 <- ggally_cor(iris, mapping = aes(x = "Petal.Length", y = "Petal.Width", color = "black"), corSize = 4.5) plot.layout <- putPlot(plot.layout, plot.text, 5, 5) plot.layout <- putPlot(plot.layout, plot.cor1, 1, 2) plot.layout <- putPlot(plot.layout, plot.cor2, 1, 3) plot.layout <- putPlot(plot.layout, plot.cor3, 1, 4) plot.layout <- putPlot(plot.layout, plot.cor4, 2, 3) plot.layout <- putPlot(plot.layout, plot.cor5, 2, 4) putPlot(plot.layout, plot.cor6, 3, 4) |