# 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, 1 ...
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幫助文檔