美文网首页单细胞绘图
pheatmap热图简介

pheatmap热图简介

作者: 瓜瓜大白 | 来源:发表于2021-08-23 13:03 被阅读0次

    前言

    进入生物信息这个行业也快两年了,记得两年前写下自己的第一篇日志,是小白的心理。现今可能偶有心得,但是也是纸上谈兵,不过近来自己想做一些事情,想做什么了,就是把自己的积累,还是自己的想法付诸于文字,让更多的人去感受一个小白的心路历程。有开心的,有悲伤的,有苦涩的,总归还在前行着。
    第一篇就是关于pheatmap这个包的简介吧,因为有太多的人问我这个包的参数,那么还不如把它写出来。

    ### 获取R的版本信息
    getRversion()
    [1] '4.1.0'
    ### 1.设置默认工作目录
    setwd("C:/Users/zq201/Desktop")
    if(dir.exists("./pheatmap包热图篇")) {
        setwd("./pheatmap包热图篇")
        getwd()
    } else{
        dir.create("./pheatmap包热图篇") # 创建heatmap目录
        setwd("./pheatmap包热图篇") # 设置heatmap目录为工作目录
        getwd() # 查看工作路径
    }
    ### 3. 安装和导入R包:pheatmap
    ## 因为我已经安装了,如果你之前没有过这个R包,那么需要在使用前安装
    install.packages("pheatmap")
    
    # 3.1 单独导入
    library(pheatmap)
    ## 4.绘制简单热图
    ### 4.1 导入R内置数据集
    data("mtcars")
    
    ### 4.2 查看前六行
    head(mtcars)
    
    dim(mtcars)
    32 11
    class(mtcars)
    'data.frame'
    ### 4.3 绘制热图
    pheatmap(mtcars, 
                       cluster_rows = T, 
                       show_rownames = T, 
                       show_colnames = T, 
                       color = colorRampPalette(c("blue", "lightblue", "red"), bias = 1)(500), 
                       border_color = NA, 
                       cellwidth = 30, 
                       cellheight = 12, 
                       fontsize_col = 18, 
                       fontsize_row = 10, 
                       scale = "row")
    
    output_10_0.png
    ### 5. 绘制个性化热图
    # 5.1 pheatmap函数的结构
    str(pheatmap) # 参数很多,庞大的一个函数
    function (mat, color = colorRampPalette(rev(brewer.pal(n = 7, name = "RdYlBu")))(100), 
        kmeans_k = NA, breaks = NA, border_color = "grey60", cellwidth = NA, 
        cellheight = NA, scale = "none", cluster_rows = TRUE, cluster_cols = TRUE, 
        clustering_distance_rows = "euclidean", clustering_distance_cols = "euclidean", 
        clustering_method = "complete", clustering_callback = identity2, cutree_rows = NA, 
        cutree_cols = NA, treeheight_row = ifelse((class(cluster_rows) == "hclust") || 
            cluster_rows, 50, 0), treeheight_col = ifelse((class(cluster_cols) == 
            "hclust") || cluster_cols, 50, 0), legend = TRUE, legend_breaks = NA, 
        legend_labels = NA, annotation_row = NA, annotation_col = NA, annotation = NA, 
        annotation_colors = NA, annotation_legend = TRUE, annotation_names_row = TRUE, 
        annotation_names_col = TRUE, drop_levels = TRUE, show_rownames = T, 
        show_colnames = T, main = NA, fontsize = 10, fontsize_row = fontsize, 
        fontsize_col = fontsize, angle_col = c("270", "0", "45", "90", "315"), 
        display_numbers = F, number_format = "%.2f", number_color = "grey30", 
        fontsize_number = 0.8 * fontsize, gaps_row = NULL, gaps_col = NULL, 
        labels_row = NULL, labels_col = NULL, filename = NA, width = NA, height = NA,  silent = FALSE, na_col = "#DDDDDD", ... )
    
    ### 5.2 构建数据矩阵
    
    set.seed(2020) # 设置随机种子,可以实现随机数据重现
    test <- matrix(rnorm(200, mean = 0, sd = 1), 20, 10) # 创建一个矩阵:服从标准正态分布的随机200个数字填充20行x10列
    
    test[1:10, seq(1, 10, 2)] <- test[1:10, seq(1, 10, 2)] + 3
    test[11:20, seq(2, 10, 2)] <- test[11:20, seq(2, 10, 2)] + 2
    test[15:20, seq(2, 10, 2)] <- test[15:20, seq(2, 10, 2)] + 4
    
    colnames(test) <- paste("Sample", 1:10, sep = "")
    colnames(test)
    rownames(test) <- paste("Gene", 1:20, sep = "")
    rownames(test)
    
    test # 查看test所有行/列内容
    
    WeChat Image_20210823124010.png
    ### 5.3 默认参数绘制热图
    pheatmap(test)
    
    output_13_0.png
    ### 5.4 kmeans = 3聚类
    pheatmap(test, kmeans_k = 3)
    
    pheatmap(test, kmeans_k = 5)
    
    ### 5.5 scale = "row",按行进行缩放,还可以按列column缩放;
    # character indicating if the values should be centered and scaled in either the row direction or the column direction, or none. 
    # Corresponding values are "row", "column" and "none"
    pheatmap(test, scale = "row")
    
    pheatmap(test, scale = "column")
    
    ### 5.6 clustering_distance_rows = "correlation",对行进行Pearson相关性距离进行聚类,也可以按照euclidean进行聚类
    pheatmap(test, scale = "row", clustering_distance_rows = "correlation")
    
    pheatmap(test, scale = "row", clustering_distance_rows = "euclidean")
    
    ### 5.7 修改默认颜色,重新绘制热图
    pheatmap(test, color = colorRampPalette(c("navy", "white", "firebrick3"))(50))
    
    pheatmap(test, color = colorRampPalette(c("blue", "black", "red"))(50))
    
    pheatmap(test, color = colorRampPalette(c("green", "black", "red"))(50))
    
    pheatmap(test, color = colorRampPalette(c("blue", "black", "yellow"))(1000))
    
    output_14_0.png
    ![![![![![![ output_14_7.png output_14_8.png output_14_9.png
    ](https://img.haomeiwen.com/i18076917/693725d0e58b62c0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    ](https://img.haomeiwen.com/i18076917/934bc46572ebb1fa.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    ](https://img.haomeiwen.com/i18076917/fb4bc6460fa46f8e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    ](https://img.haomeiwen.com/i18076917/24086f6c2b0964ba.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    ](https://img.haomeiwen.com/i18076917/f517e8297fb606e4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    ](https://img.haomeiwen.com/i18076917/8d5a47d34e211e59.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    ### 5.8 行聚类参数cluster_row
    pheatmap(test, cluster_row = FALSE) # 参数cluster_row = FALSE不进行行聚类,cluster_row = TRUE进行行聚类,默认是进行行聚类的
    
    ### 5.9 列聚类参数cluster_cols
    pheatmap(test, cluster_cols = FALSE) # 参数cluster_cols = FALSE不进行列聚类,cluster_cols = TRUE进行列聚类,默认进行列聚类
    
    pheatmap(test, cluster_cols = FALSE, cluster_rows = FALSE)
    
    output_15_0.png
    output_15_1.png
    output_15_2.png
    ### 5.10 图例参数:legend
    pheatmap(test, legend = FALSE) # 参数legend = FALSE不显示图例,legend = TRUE显示图例,默认显示图例
    
    output_16_0.png
    ### 5.11 热图单元格显示参数:display_numbers
    pheatmap(test, display_numbers = TRUE) # 参数display_numbers = TRUE显示单元格数值(热图颜色对应的表达值),display_numbers = FALSE不显示单元格数值,默认不显示
    
    ### 5.12 单元格显示格式参数:number_format
    pheatmap(test, display_numbers = TRUE, number_format = "%.1e") # 参数number_format = "%.1e"设置数值展示的格式,科学计数法显示数值
    
    output_17_0.png
    output_17_1.png
    ### 5.13 单元格个性化显示设置(通过一些条件限制)
    pheatmap(test, display_numbers = matrix(ifelse(test > 5, "***", ""), nrow(test))) # 参数display_numbers = matrix(ifelse(test > 5, "***", "")根据条件显示单元格标记,可以用一些特殊字符,例如:*
    
    output_18_0.png
    ### 5.14 热图图例个性化设置参数:legend_breaks和legend_labels
    pheatmap(test, cluster_row = FALSE, legend_breaks = -1:4, legend_labels = c("0", "1e-4", "1e-3", "1e-2", "1e-1", "1")) # 参数legend_breaks = -1:4设置图例分段数,legend_labels则设置图例标记内容和格式
    
    output_19_0.png
    ### 5.15 热图单元格大小调整
    pheatmap(test, cellwidth = 15, cellheight = 12)
    
    output_20_0.png
    ### 5.16 为热图填加标题
    pheatmap(test, cellwidth = 15, cellheight = 12, main = "Gene heatmap")
    
    output_21_0.png
    ### 5.17 绘制热图并保存文件(文件大小自适应热图的大小)
    # 图片文件格式支持png, pdf, tiff, bmp, jpeg
    #### Save as PDF
    pheatmap(test, cellwidth = 15, cellheight = 12, fontsize = 8, filename = "test.pdf") # 文件名及格式设置参数:filename
    #### Save as PNG
    pheatmap(test, cellwidth = 15, cellheight = 12, fontsize = 8, filename = "test.png")
    #### Save as TIFF
    pheatmap(test, cellwidth = 15, cellheight = 12, fontsize = 8, filename = "test.tiff")
    #### Save as BMP
    pheatmap(test, cellwidth = 15, cellheight = 12, fontsize = 8, filename = "test.bmp")
    #### Save as JPEG
    pheatmap(test, cellwidth = 15, cellheight = 12, fontsize = 8, filename = "test.jpeg")
    dir()
    

    1.'test.bmp'
    2.'test.jpeg'
    3.'test.pdf'
    4.'test.png'
    5.'test.tiff'

    ### 5.18 注释行名和列名
    #### Generate annotations for columns
    annotation_col = data.frame(CellType = factor(rep(c("CT1", "CT2"), 5)), Time = 1:5)
    rownames(annotation_col) = paste("Sample", 1:10, sep = "")
    annotation_col
    #### Generate annotations for rows
    annotation_row = data.frame(GeneClass = factor(rep(c("Path1", "Path2", "Path3"), c(10, 4, 6))))
    rownames(annotation_row) = paste("Gene", 1:20, sep = "")
    annotation_row
    
    WeChat Image_20210823125444.png
    WeChat Image_20210823125449.png
    ### 5.19 绘制注释列名的热图
    pheatmap(test, annotation_col = annotation_col)
    
    output_24_0.png
    ### 5.20 绘制填加注释行名的热图
    pheatmap(test, annotation_row = annotation_row)
    
    output_25_0.png
    ### 5.21 绘制填加注释行名和列名的热图
    pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row)
    
    output_26_0.png
    ### 5.22 改变行名和列名的角度绘制热图
    pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row, angle_col = "45") # 逆时针旋转45度,是个好方法排列较多的列名
    
    output_27_0.png
    ### 5.23 个性化注释标记颜色
    ann_colors <- list(Time = c("white", "firebrick"),
                       CellType = c(CT1 = "#1B9E77", CT2 = "#D95F02"),
                       GeneClass = c(Path1 = "#7570B3", Path2 = "#E7298A", Path3 = "#66A61E")) # firebrick耐火砖,十六进制颜色码:#1B9E77,#D95F02, #7570B3,#E7298A,#66A61E
    
    pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row, annotation_colors = ann_colors)
    
    output_29_0.png
    ### 5.24热图的间隙Gaps
    pheatmap(test, annotation_col = annotation_col, cluster_rows = FALSE, gaps_row = c(10, 14), cutree_col = 2) # 第10、14行设置空隙,然后热图纵向分隔为二
    
    output_30_0.png
    ### 5.25 显示自定义的行名和列名(只显示想显示的)
    labels_row <- c("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "Il10", "Il15", "Il1b")
    labels_row
    labels_col <- c("heart", "brain", "blood", "liver", "lung", "eye", "bone", "stomach", "", "")
    labels_col
    pheatmap(test, annotation_col = annotation_col, labels_row = labels_row, labels_col = labels_col)
    
    output_31_2.png
    getwd()
    dir()
    
    sessionInfo()
    
    R version 4.1.0 (2021-05-18)
    Platform: x86_64-w64-mingw32/x64 (64-bit)
    Running under: Windows 10 x64 (build 19043)
    
    Matrix products: default
    
    locale:
    [1] LC_COLLATE=Chinese (Simplified)_China.936 
    [2] LC_CTYPE=Chinese (Simplified)_China.936   
    [3] LC_MONETARY=Chinese (Simplified)_China.936
    [4] LC_NUMERIC=C                              
    [5] LC_TIME=Chinese (Simplified)_China.936    
    
    attached base packages:
    [1] stats     graphics  grDevices utils     datasets  methods   base     
    
    other attached packages:
    [1] pheatmap_1.0.12
    
    loaded via a namespace (and not attached):
     [1] munsell_0.5.0       uuid_0.1-4          colorspace_2.0-2   
     [4] R6_2.5.0            rlang_0.4.11        fansi_0.5.0        
     [7] tools_4.1.0         grid_4.1.0          gtable_0.3.0       
    [10] utf8_1.2.2          htmltools_0.5.1.1   ellipsis_0.3.2     
    [13] digest_0.6.27       lifecycle_1.0.0     crayon_1.4.1       
    [16] IRdisplay_1.0       farver_2.1.0        RColorBrewer_1.1-2 
    [19] repr_1.1.3          base64enc_0.1-3     vctrs_0.3.8        
    [22] IRkernel_1.2.0.9000 evaluate_0.14       pbdZMQ_0.3-5       
    [25] compiler_4.1.0      pillar_1.6.2        scales_1.1.1       
    [28] jsonlite_1.7.2      Cairo_1.5-12.2     
    

    相关文章

      网友评论

        本文标题:pheatmap热图简介

        本文链接:https://www.haomeiwen.com/subject/uzzkiltx.html