简体   繁体   中英

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, ie 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])

在此处输入图像描述

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

在此处输入图像描述

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() .

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM