2017-04-07 74 views
0

我試圖產生成對相關的lower triangleheatmapR下三角關係熱圖

這裏的數據:

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")) 

而且ggplotheatmap代碼

enter image description here

所以彩色元素被分散在各地,而不是被限制在較低的三角形。

任何想法是什麼問題?

+0

看來工作。我覺得你只是使用了錯誤的變量在ggplot功能:cor.mat.df應該cor.df. – thc

回答

1

好吧,我複製你的腳本,這是我的陰謀plot

僅僅通過

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="") 

我sessionInfo改變

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="") 

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 a 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