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幫助檔案
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/546601.html
標籤:R
上一篇:AIGC導航:AIGC即AI Generated Content是指利用人工智能技術來生成內容
下一篇:廣發基金外包面經