0
我試圖產生成對相關的lower triangle
heatmap
在R
。下三角關係熱圖
這裏的數據:
set.seed(1)
mat <- matrix(rnorm(6*10),ncol=6,nrow=10)
colnames(mat) <- c("s.-.+.1","s.-.+.2","s.-.+.3","s.+.-.1","s.+.-.2","s.+.-.3")
這裏是我想要的代碼:這使我
require(ggplot2)
ggplot(cor.mat.df,aes(sample2,sample1,fill=correlation))+geom_tile(color="white")+scale_fill_gradient2(low="blue",high="red",mid="white",midpoint=0,limit=c(-1,1),space="Lab",name="Pearson\nCorrelation")+theme_bw()+theme(axis.text.x=element_text(angle=45,vjust=1,size=10,hjust=1))+coord_fixed()+labs(x="",y="")
:
getUpperTri <- function(cor.mat){
cor.mat[lower.tri(cor.mat)] <- NA
return(cor.mat)
}
reorderCormat <- function(cor.mat){
dist.mat <- as.dist((1-cor.mat)/2)
hc <- hclust(dist.mat)
cor.mat <-cor.mat[hc$order,hc$order]
}
cor.df <- reshape2::melt(getUpperTri(reorderCormat(cor(mat))),na.rm=TRUE,value.name="correlation",varnames=c("sample1","sample2"))
而且ggplot
heatmap
代碼
所以彩色元素被分散在各地,而不是被限制在較低的三角形。
任何想法是什麼問題?
看來工作。我覺得你只是使用了錯誤的變量在ggplot功能:cor.mat.df應該cor.df. – thc