我发现了使用ggplot2创建关联热图的简单方法:
data(attitude)
library(ggplot2)
library(reshape2)
ggplot(melt(cor(attitude)), aes(Var1, Var2)) +
geom_tile(aes(fill = value))
现在,我通常看到的相关矩阵在主对角线上具有方差,而在对角线上不具有方差。
我尝试使用以下rev()
命令完成此操作:
ggplot(melt(cor(attitude)), aes(Var1, rev(Var2))) +
geom_tile(aes(fill = value))
就瓷砖内部的着色方案而言,哪种效果都很好。但是y轴上的标签保持不变!我该怎么办?我宁愿不手动输入正确的顺序,因为我的实际代码应该适用于任意数据集。
我以前遇到过这个问题,在这里找到了答案:https : //stackoverflow.com/a/14630937/4090947
基本上:
data(attitude)
library(ggplot2)
library(reshape2)
ggplot(melt(cor(attitude)), aes(Var1, ordered(Var2, levels = rev(sort(unique(Var2)))))) +
geom_tile(aes(fill = value))
这会反转您的Y轴
由于您的列名是离散的,所以关键是将字符串(列名)转换为因数。一旦有了因素,就可以更改顺序。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句