r - Lower triangle correlation heatmap -
i'm trying produce lower triangle
heatmap
of pairwise correlations in r
.
here's data:
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")
here's code i'm trying:
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"))
and ggplot
heatmap
code is:
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="")
which gives me:
so colored elements scattered on rather being confined lower triangle.
any idea what's problem?
well, reproduced script , plot
just change
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="")
by
ggplot(cor.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="")
my sessioninfo:
r version 3.3.2 (2016-10-31) platform: x86_64-w64-mingw32/x64 (64-bit) running under: windows 7 x64 (build 7601) service pack 1 locale: [1] lc_collate=spanish_colombia.1252 lc_ctype=spanish_colombia.1252 [3] lc_monetary=spanish_colombia.1252 lc_numeric=c [5] lc_time=spanish_colombia.1252 attached base packages: [1] stats graphics grdevices utils datasets methods base other attached packages: [1] ggplot2_2.2.1 loaded via namespace (and not attached): [1] rcpp_0.12.8 digest_0.6.11 assertthat_0.1 grid_3.3.2 plyr_1.8.4 [6] gtable_0.2.0 magrittr_1.5 scales_0.4.1 stringi_1.1.2 reshape2_1.4.2 [11] lazyeval_0.2.0 labeling_0.3 tools_3.3.2 stringr_1.1.0 munsell_0.4.3 [16] colorspace_1.3-2 tibble_1.2
Comments
Post a Comment