0
私は、R
のペアワイズ相関のlower triangle
heatmap
を生成しようとしています。下三角相関ヒートマップ
ここではデータです:
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")
は、ここで私がしようとしているコードです:
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
コードは次のとおりです。私に与え
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="")
:
したがって、着色された要素は、下三角に限定されるのではなく、全面に散在しています。
何が問題なのですか?
。私はggplot関数で間違った変数を使用したと思います:cor.mat.dfはcor.dfでなければなりません。 – thc