pheatmap

最后发布时间:2022-09-05 16:45:00 浏览量:

https://github.com/raivokolde/pheatmap

https://r-charts.com/correlation/pheatmap/

https://zhuanlan.zhihu.com/p/370477638

自动化heatmap图的宽度

图片alt

图片alt

10 - 3.74-1=5.62
5.62/max(str_length(rownames(sig_res))) = 0.0985
(4.74+(0.0985 * max(str_length(rownames(sig_res))) )

get_gsva_heatmap_width <- function(obj,name,n=30){
    diff <- obj@GeneSetBySamples[[name]]@degs[[1]]@diff
    sig_res <- arrange(diff,pvalue ) # filter( diff, pvalue < 0.5 )
    if(nrow(sig_res) >= n){
        sig_res <- sig_res[1:n,]
    }else{
        sig_res <- sig_res[1:nrow(sig_res),]
    }
    return (4.74+(0.0985 * max(str_length(rownames(sig_res))) ) )
}
decorators  <- function(f) {

    wrapper <- function(...,filename=NULL,width=8,height=6) {
        flag = FALSE
        if(!is.null(filename)){
          flag <- TRUE
          strs <- str_split(filename,"\\.")
          suffix <- strs[[1]][length(strs[[1]])]
          if(suffix=="png"){
            do.call("png",list(file =filename ,width =width ,height =height ,units="in",res = 300) )
          }else if(suffix=="pdf"){
            do.call("pdf",list(file =filename ,width =width ,height =height) )
          }else{
            flag = FALSE
          }
          
        }
        res <- f(...)
        print(res)
        if(!is.null(filename) && flag){
            dev.off()
        }
        # <code prior to execution>

        # <code posterior to execution>
        # return(res)
    }
    return(wrapper)
}
heatmap <- function(count,diff){

    sig_res <- arrange(diff,pvalue ) # filter( diff, pvalue < 0.5 )
    if(nrow(sig_res) >= 30){
        sig_res <- sig_res[1:30,]
    }else{
        sig_res <- sig_res[1:nrow(sig_res),]
    }

    sig_count_df <- count[rownames(sig_res),]
    group <- c("darkred","darkblue")
    names(group) <- contrast
    annoCol<-list(group=group)
    p <- pheatmap(sig_count_df,
            scale = "row", 
            cluster_cols  = F,
            color = colorRampPalette(c("darkred", "#FFFFFF","darkblue"))(255),
            #   gaps_col = 5,
    #            gaps_row = c(8, 9),
            # cutree_rows = 2,
            angle_col = 45,
    #          cutree_cols = 2,
            annotation_col=dplyr::select(metadata,"group"),annotation_colors  = annoCol)
    print(p)
}
decorators_volcano <- decorators(volcano)