找回密码
 立即注册
首页 资源区 代码 pheatmap实用参数(二)

pheatmap实用参数(二)

鞍注塔 5 天前
  1. # Create test matrix(造数据)
  2. set.seed(6)
  3. test = matrix(rnorm(200), 20, 10)
  4. test[1:10, seq(1, 10, 2)] = test[1:10, seq(1, 10, 2)] + 3
  5. test[11:20, seq(2, 10, 2)] = test[11:20, seq(2, 10, 2)] + 2
  6. test[15:20, seq(2, 10, 2)] = test[15:20, seq(2, 10, 2)] + 4
  7. colnames(test) = paste("Test", 1:10, sep = "")
  8. rownames(test) = paste("Gene", 1:20, sep = "")
复制代码
正文从这里开始
  1. # Show text within cells
  2. pheatmap(test, display_numbers = TRUE)
  3. pheatmap(test, display_numbers = TRUE, number_format = "\\%.1e")
  4. pheatmap(test, display_numbers = TRUE, display_numbers = matrix(ifelse(test > 5, "*", ""), nrow(test)))
复制代码

  • display_numbers = T 即Show text within cells
  • number_format 可以格式输出display_number
  • 或者干脆自定义一个matrix通过display_numbers参数进行display
  1. # legend(图例)的设置选项
  2. pheatmap(test)  # p1
  3. pheatmap(test, legend_breaks = -1:7)  # p2
  4. pheatmap(test, legend_breaks = 1:6, legend_labels = c("6","6", "6", "6", "6", "6"))  # p3
  5. pheatmap(test, legend_breaks = 9:14, legend_labels = c("6","6", "6", "6", "6", "6"))  # p4
复制代码

  • pheatmap函数会在内部算出legend的数值范围,本例中大概是 -1:7
  • 在数值范围内,我们可以设定legend_breaks,以及对legend_breaks这个label的文本展示
  • legend_breaks和legend_labels是有一个对应关系的,否则报错如下
    Error in pheatmap(test, legend_breaks = 1:6, legend_labels =
    c(“6”, “6”, : Lengths of legend_breaks and legend_labels must be
    the same
  • 假如我们的设定范围超出,就如p4所示
1.png
  1. # Fix cell sizes and save to file with correct size
  2. pheatmap(test, cellwidth = 15, cellheight = 12, main = "Example heatmap")  # the title of the plot
  3. pheatmap(test, cellwidth = 15, cellheight = 12, fontsize = 8, filename = "test.pdf")  # save to pdf
  4. # Change angle of text in the columns (0, 45, 90, 270 and 315)
  5. pheatmap(test, angle_col = "45")
  6. pheatmap(test, angle_col = "0")
复制代码
有关annotation
  1. # Generate annotations for rows and columns(先造数据)
  2. annotation_col = data.frame(
  3.                     CellType = factor(rep(c("CT1", "CT2"), 5)),
  4.                     Time = 1:5
  5.                 )
  6. rownames(annotation_col) = paste("Test", 1:10, sep = "")
  7. annotation_row = data.frame(
  8.                     GeneClass = factor(rep(c("Path1", "Path2", "Path3"), c(10, 4, 6)))
  9.                 )
  10. rownames(annotation_row) = paste("Gene", 1:20, sep = "")
  11. # Display row and color annotations
  12. pheatmap(test, annotation_col = annotation_col, cluster_cols = F)
  13. pheatmap(test, annotation_col = annotation_col, annotation_legend = FALSE, cluster_cols = F)
  14. pheatmap(test, annotation_row = annotation_row, cluster_rows = F)
复制代码
2.png


  • annotation数据首先要组织成dataframe,dataframe中的rownames要和注释项进行对应,
    而column就是要展示的注释条,每个column都会生成一个注释条来显示
  • annotation_col/row 默认会有legend配合展示,annotation_legend = FALSE可以去掉legend
  1. # Specify colors (自定义注释条颜色)
  2. ann_colors = list(
  3.     Time = c("white", "firebrick"),
  4.     CellType = c(CT1 = "#1B9E77", CT2 = "#D95F02"),
  5.     GeneClass = c(Path1 = "#7570B3", Path2 = "#E7298A", Path3 = "#66A61E")
  6. )
  7. pheatmap(test, annotation_col = annotation_col, annotation_colors = ann_colors)
  8. pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row, annotation_colors = ann_colors)
复制代码
3.png


  • 注释条颜色数据要组织成list(list的灵活性就在此处凸显出来了),
    list中的每个元素名作为对应项(对应前述dataframe中的colnames),
    同时可以进一步为attribute指定颜色,例如
    CT1 = "#1B9E77", CT2 = "#D95F02"
参考资料

pheatmap帮助文档

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册