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)

      

enter image description here

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?

+3


source to share


2 answers


library(GGally)

set.seed(42)
d = data.frame(x1=rnorm(100),
               x2=rnorm(100),
               x3=rnorm(100),
               x4=rnorm(100),
               x5=rnorm(100))

# estimated density in diagonal
ggpairs(d)

      

enter image description here



# blank
ggpairs(d, diag = list("continuous"="blank")

      

enter image description here

+11


source


Using the PerformanceAnalytics library:

library("PerformanceAnalytics")
chart.Correlation(df, histogram = T, pch= 19)

      



enter image description here

+2


source







All Articles