Correlation matrix plot with ggplot2
I want to create a correlation matrix plot, that is, a plot in which each variable is plotted in a scatterplot relative to every other variable, for example using pairs()
or splom()
. I want to do this with ggplot2. See examples here . The link mentions code that someone wrote to do this in ggplot2, however it is deprecated and no longer works (even after you replaced the deprecated parts).
You can do this with a loop in a loop and then multiplot()
, but there must be a better way. I tried for a long time to melt the dataset and copy the values ββand variables and then use edges. It almost gives you something right.
d = data.frame(x1=rnorm(100),
x2=rnorm(100),
x3=rnorm(100),
x4=rnorm(100),
x5=rnorm(100))
library(reshape2)
d = melt(d)
d$value2 = d$value
d$variable2 = d$variable
library(ggplot2)
ggplot(data=d, aes(x=value, y=value2)) +
geom_point() +
facet_grid(variable ~ variable2)
This provides the correct overall structure, but only works for plotting each variable against itself. Is there an even smarter way to do this without resorting to 2 loops?
source to share