r - ggpairs:仅绘制相关矩阵的前两行
问题描述
我想用绘图做一些相关性分析。由于我的实际数据太大,我使用mtcars数据框来设置示例。
这里的代码
library(ggplot2)
library(ggcorrplot)
mtcars
library(ggcorrplot)
# Computing correlation matrix
corrmatr_mtcars <- round(cor(subset(mtcars[c(3:7,1)])),1)
head(corrmatr_mtcars[,1:6])
corrmatr_mtcars
# Computing correlation matrix with p-values
corrmatr_mtcars.mat <- cor_pmat(mtcars[c(3:7,1)])
head(corrmatr_mtcars.mat[, 1:6])
corrmatr_mtcars.mat
library(GGally)
ggpairs(mtcars[c(3:7,1)],
title = "Corr Analysis of...",
lower = list(continuous = wrap("cor",
size = 3)),
upper = list(
continuous = wrap("smooth",
alpha = 0.3,
size = 0.1))
)
有了这个绘图结果:
但是,我只对前两个变量与所有其他变量的相关性感兴趣。所以,为了避免不必要的信息和节省地方,我宁愿
- 我的情节只显示前两个相关行。所有其他相关性都可以删除。最后,我想像下面这样只需要 3 行。
- 随后,Corr-Value 标签应放置在散点图面板上。>br>
我找不到任何选择这样做。
ggpairs
(没有复杂的功能)这甚至通常是可能的吗?
如果是:如何?
如果不是:什么是具有可比结果的方法?
解决方案
可以这样做
library(ggplot2)
library(ggcorrplot)
mtcars
library(ggcorrplot)
# Computing correlation matrix
corrmatr_mtcars <- round(cor(subset(mtcars[c(3:7,1)])),1)
head(corrmatr_mtcars[,1:6])
corrmatr_mtcars
# Computing correlation matrix with p-values
corrmatr_mtcars.mat <- cor_pmat(mtcars[c(3:7,1)])
head(corrmatr_mtcars.mat[, 1:6])
corrmatr_mtcars.mat
library(GGally)
gg1 = ggpairs(mtcars[c(3:7,1)],
title = "Corr Analysis of...",
lower = list(continuous = wrap("cor",
size = 3)),
upper = list(
continuous = wrap("smooth",
alpha = 0.3,
size = 0.1))
)
gg1$plots = gg1$plots[1:12]
gg1$yAxisLabels = gg1$yAxisLabels[1:2]
gg1
推荐阅读
- javascript - 无法在输入字段中输入
- amazon-web-services - EC2 - 授予外部开发人员服务器访问权限?
- vhdl - 可合成的 VHDL 递归,Vivado:模拟器以意外方式终止
- c# - 将 REST API 转换为 websocket 客户端
- android-studio - Android studio 3.6 canary 6 不启动只是加载
- angular - 条形图中未显示低值的条形
- google-chrome - GMail 阻止通过自动化(Selenium)登录
- html - 如何从 html 文件中 grep 特定日期
- angular - 在 Angular 7 表单控件的单个输入字段中验证电子邮件、电话和 PAN 号码
- javascript - 如何将数组映射到对象