Looking for a way to plot a pairwise scatterplot matrix where variables are in two groups

Solution 1:

Here is a vanilla ggplot approach with tidyr preprocessing.

First, we'll reshape the data such that the width and length variables have the same columns.

library(ggplot2)

df <- tidyr::pivot_longer(iris, c(Petal.Length, Petal.Width), 
                          names_to = "petal_metric", values_to = "petal_value")
df <- tidyr::pivot_longer(df, c(Sepal.Length, Sepal.Width),
                          names_to = "sepal_metric", values_to = "sepal_value")

Next, we can simply facet on the petal/sepal metrics.

p <- ggplot(df, aes(sepal_value, petal_value, colour = Species)) +
  geom_point()
  
p + facet_grid(petal_metric ~ sepal_metric)

We could decorate the plot a bit better to have the strips serve as axis titles.

p + facet_grid(petal_metric ~ sepal_metric, switch = "both") +
  theme(strip.placement = "outside",
        strip.background = element_blank(),
        axis.title = element_blank())

Created on 2022-01-11 by the reprex package (v2.0.1)