There's any easier way to select a row from ggpairs
output using GGally
's own getPlot()
and ggmatrix()
. To select a row by number (like the first) just specify i = 1
in getPlot()
.
library(ggplot2)
library(GGally)
pairs <- ggpairs(mtcars, columns = c(1,2,8:11))
plots <- lapply(1:pairs$ncol, function(j) getPlot(pairs, i = 1, j = j))
ggmatrix(
plots,
nrow = 1,
ncol = pairs$ncol,
xAxisLabels = pairs$xAxisLabels,
yAxisLabels = primary_var
)
With slightly more work you could select the row using the primary variable of interest. Just set primary_var
to the name of the variable you want in the following code:
library(ggplot2)
library(GGally)
primary_var <- "mpg"
pairs <- ggpairs(mtcars, columns = c(1,2,8:11))
pvar_pos <- match(primary_var, pairs$xAxisLabels)
plots <- lapply(1:pairs$ncol, function(j) getPlot(pairs, i = pvar_pos, j = j))
ggmatrix(
plots,
nrow = 1,
ncol = pairs$ncol,
xAxisLabels = pairs$xAxisLabels,
yAxisLabels = primary_var
)
NOTE: Almost any row you choose will by default have 1+ empty plots with only the correlation between variables in it. You can change that by changing the upper
argument in ggpairs()
. Check the documentation for details.