ggplot2 中的广义矩阵散点图?

Generalised matrix scatterplots in ggplot2?

我想创建一个大小为 NN 的网格图,其中 N 是字段数。我试图获得的网格图有点像 Weka 中的图:一个多图,其中每一行都是数据帧的一个字段,每一列也是一个字段。诀窍是我想获得矩阵散点图的更通用版本。我想要更丰富的数据,没有重复:例如,对角线值可以有分布。

维卡。矩阵散点图浪费了很多space,能不能丰富一下?

基本R解法1.plot(iris)同样有浪费的问题space,为什么要对角线?

看起来像是从维基百科以某种方式使用 R 基本命令创建的 R 解决方案 2。

R 中的小型演示(计算时间过长)

library(gridExtra)
library(grid)
library(ggplot2)
#library(lattice)
data(iris) 
p1 <- ggplot(data=iris,aes(x=Sepal.Length, y=Sepal.Length)) + geom_point()
p2 <- ggplot(data=iris,aes(x=Sepal.Length, y=Sepal.Width)) + geom_point()
p3 <- ggplot(data=iris,aes(x=Sepal.Length, y=Petal.Length)) + geom_point()
p4 <- ggplot(data=iris,aes(x=Sepal.Length, y=Petal.Width)) + geom_point()
p5 <- ggplot(data=iris,aes(x=Sepal.Length, y=Species)) + geom_point()
grid.arrange(p1, p2, p3, p4, p5, ncol=length(names(iris)))
#ERROR: In as.list(X): reached elapsed time limit
# https://cran.r-project.org/web/packages/gridExtra/vignettes/arrangeGrob.html

及其会话信息

version 3.4.1 (2017-06-30)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.1

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib

locale:
[1] C

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] lattice_0.20-35 gridExtra_2.3   BAS_1.4.7       statsr_0.0.1    dplyr_0.7.4    
[6] ggplot2_2.2.1  

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.14     knitr_1.17       bindr_0.1        magrittr_1.5    
 [5] munsell_0.4.3    colorspace_1.3-2 xtable_1.8-2     R6_2.2.2        
 [9] rlang_0.1.4      plyr_1.8.4       tools_3.4.1      gtable_0.2.0    
[13] htmltools_0.3.6  lazyeval_0.2.0   assertthat_0.2.0 digest_0.6.13   
[17] tibble_1.3.4     bindrcpp_0.2     shiny_1.0.5      glue_1.2.0      
[21] mime_0.5         labeling_0.3     compiler_3.4.1   scales_0.4.1    
[25] httpuv_1.3.5     pkgconfig_2.0.1 
> 

这非常耗时:对于我的 1.3GHz MBA,它甚至没有完成并且需要很长时间来绘制或根本不绘制。我想找到更现代的方法来绘制广义散点图。

如何使用 ggplot 和 tidyverse 创建广义矩阵散点图?

有趣的附带问题

  1. Side-by-side plots with ggplot2

  2. grid.layout in ggplot

  3. Grid of multiple ggplot2 plots which have been made in a for loop

关键搜索词是

  • Generalised Pairs Plots, generalised scatterplot matrix

  • scatterplot matrix

哈德利在 2012 年讨论过 here。我们在下面列出了替代方案,试图实现与原始矩阵散点图相同的探索性分析。

在撰写本文时,GGally 看起来是使用 ggplot 和 tideverse 的最佳人选。它是用 ggplot2 构建的,您可以进一步阅读 here.

备选方案

GGally 由 Marco Sandri 建议

dev.off()
library(GGally)
ggpairs(iris)

对于更大的数据集,您可能需要更改 cardinality_threshold 这样

ggpairs(movies[1:15,1:10], cardinality_threshold = 211)

其中电影数据来自上次分配 here

对于较大的数据集,这看起来有些难以阅读。

唉!您可以使用颜色并自定义 ggpairs 图

示例来自 here. GGally has an excellent manual here.