Mihail
Mihail

Reputation: 827

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

Looking for a way to plot a pairwise scatterplot matrix where variables are in two groups, i.e. the plots need to be restricted to pairs between groups, excluding within-group pairs. For instance, the base pairs() produces this

    pairs(iris[1:4])

enter image description here

Whereas I am looking to, for example, correlate only sepal variables with petal variables, and am not interested in sepal (or petal) length-to-width correlations. So, instead of a 4x4 matrix, I'd need a 2x2 matrix.

The solution would look something like this

enter image description here

pairs() seems to support a formula argument, but pairs(Sepal.Length + Sepal.Width ~ Petal.Length + Petal.Width, data=iris) doesn't look right. It's in the right direction giving a 3x3 matrix, but for some reason the two sepal variables are grouped.

Preferably looking for a solution in ggplot2 or GGally::ggpairs().

Upvotes: 2

Views: 2074

Answers (1)

teunbrand
teunbrand

Reputation: 38023

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)

Upvotes: 3

Related Questions