pheatmap有用参数(二)
# Create test matrix(造数据)
set.seed(6)
test = matrix(rnorm(200), 20, 10)
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("Test", 1:10, sep = "")
rownames(test) = paste("Gene", 1:20, sep = "")
正文从这儿开端
# Show text within cells
pheatmap(test, display_numbers = TRUE)
pheatmap(test, display_numbers = TRUE, number_format = "\\%.1e")
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
# legend(图例)的设置选项
pheatmap(test) # p1
pheatmap(test, legend_breaks = -1:7) # p2
pheatmap(test, legend_breaks = 1:6, legend_labels = c("6","6", "6", "6", "6", "6")) # p3
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所示
# Fix cell sizes and save to file with correct size
pheatmap(test, cellwidth = 15, cellheight = 12, main = "Example heatmap") # the title of the plot
pheatmap(test, cellwidth = 15, cellheight = 12, fontsize = 8, filename = "test.pdf") # save to pdf
# Change angle of text in the columns (0, 45, 90, 270 and 315)
pheatmap(test, angle_col = "45")
pheatmap(test, angle_col = "0")
有关annotation
# Generate annotations for rows and columns(先造数据)
annotation_col = data.frame(
CellType = factor(rep(c("CT1", "CT2"), 5)),
Time = 1:5
)
rownames(annotation_col) = paste("Test", 1:10, sep = "")
annotation_row = data.frame(
GeneClass = factor(rep(c("Path1", "Path2", "Path3"), c(10, 4, 6)))
)
rownames(annotation_row) = paste("Gene", 1:20, sep = "")
# Display row and color annotations
pheatmap(test, annotation_col = annotation_col, cluster_cols = F)
pheatmap(test, annotation_col = annotation_col, annotation_legend = FALSE, cluster_cols = F)
pheatmap(test, annotation_row = annotation_row, cluster_rows = F)
-
annotation数据首先要组织成dataframe,dataframe中的rownames要和注释项进行对应,
而column便是要展现的注释条,每个column都会生成一个注释条来显现 -
annotation_col/row 默许会有legend合作展现,annotation_legend = FALSE能够去掉legend
# Specify colors (自定义注释条色彩)
ann_colors = list(
Time = c("white", "firebrick"),
CellType = c(CT1 = "#1B9E77", CT2 = "#D95F02"),
GeneClass = c(Path1 = "#7570B3", Path2 = "#E7298A", Path3 = "#66A61E")
)
pheatmap(test, annotation_col = annotation_col, annotation_colors = ann_colors)
pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row, annotation_colors = ann_colors)
- 注释条色彩数据要组织成list(list的灵活性就在此处凸显出来了),
list中的每个元素名作为对应项(对应前述dataframe中的colnames),
一起能够进一步为attribute指定色彩,例如CT1 = "#1B9E77", CT2 = "#D95F02"
参考资料
pheatmap协助文档