是否有一个 R 函数通过将它们全部绘制在一个正方形中来比较数据框成对列的分布 sheet

Is there an R function that compare the distribution of pairs of columns of a data frame by plotting them all in one square sheet

我有一个可以按如下方式创建的数据框。

dat <- data.frame(dens = c(rnorm(50), rnorm(50, 10, 5), rnorm(50, 5, 1), rnorm(50, 3,2))
              , group = rep(c("a", "b", "c", "d"), each = 50))

我正在尝试比较每对可能的组 (a,b),(a,c), (a,d), (b,c), ... 的分布每对彼此重叠。

我想要一个矩阵形式的 sheet/frame,它有 16 个槽,这样每个槽代表假设对 (a,b) 等的分布。

我现在得到的是一张图中所有4组的分布图。

ggplot(dat, aes(x = dens, fill = group))+ 
              geom_density(alpha = 0.5)

你知道我是否可以创建 4 * 4 sheet 的地块吗?

这是一种在嵌套成组后将数据与其自身连接的方法。这样每个组就会与其他组(和它自己)配对一次,从而可以在各个方面将每个组相互绘制。

library(tidyverse)

dat_nested <- dat %>%
  nest(data = c(dens)) %>%    # Edit: better syntax to avoid warning msg
  mutate(all = 1)

dat_nested %>%
  full_join(dat_nested %>% 
              rename(group2 = group),
            by = "all") %>%
  unnest(cols = c(data.x, data.y),   # updated to preferred tidyr 1.0.0 syntax
         names_repair = make.unique) %>%  

  ggplot() +
  geom_density(aes(x = dens, fill = group), alpha = 0.5) +
  geom_density(aes(x = dens.1, fill = group2), alpha = 0.5) +
  facet_grid(group ~ group2)