heatmap-热图创建

作者: 我最有才 | 来源:发表于2019-01-09 13:52 被阅读5次

首先,我们需要知道,热图的原理就是根据你的matrix或者data.frame中行列数字大小,映射到一个个独立的小矩形面中的一种对数据直观演示的一种方法,只是用颜色深浅对数据大小对大面积数据展开后的展示;类似于条形图-直方图-饼图-boxplot,都是对数据的直观表示。

获得热图有很多的包,就用这个R自带的pheatmap包来就可以了,也是很漂亮的呀呀呀呀呀呀。

下面是 pheatmap()的解释

例子可以用下面的代码表示

library(pheatmap)

  # Create test matrix

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 = "")

class(test)

str(test)

# Draw heatmaps

pheatmap(test)

pheatmap(test, kmeans_k = 2)

pheatmap(test, scale = "row", clustering_distance_rows = "correlation")

pheatmap(test,scale = "row")

pheatmap(test,scale="column")

pheatmap(test, color = colorRampPalette(c("navy", "white", "firebrick3"))(50))

###break 调节lengend的区间;col调节颜色;scale调节

bk = unique(c(seq(-8,8, length=100)))

pheatmap(test,breaks = bk,,scale="column")

pheatmap(test,breaks=unique(seq(-2,8,length=100)),color=colorRampPalette(c("navy", "white", "firebrick3"))(100))

pheatmap(test, cluster_row = FALSE)

pheatmap(test, legend = FALSE)

# Show text within cells

pheatmap(test, display_numbers = TRUE)

pheatmap(test, display_numbers = TRUE, number_format = "\%.1e")

pheatmap(test, display_numbers = matrix(ifelse(test > 5, "*", ""), nrow(test)))

pheatmap(test, cluster_row = FALSE, legend_breaks = -1:4, legend_labels = c("0",

                                                                            "1e-4", "1e-3", "1e-2", "1e-1", "1"))

# Fix cell sizes and save to file with correct size

pheatmap(test, cellwidth = 15, cellheight = 12, main = "Example heatmap")

pheatmap(test, cellwidth = 15, cellheight = 12, fontsize = 8, filename = "test.pdf")

# 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)

pheatmap(test, annotation_col = annotation_col, annotation_legend = FALSE,legend_breaks = NA)

pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row)

pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row,annotation_legend  = F)

# 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, main = "Title")

pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row,

        annotation_colors = ann_colors)

pheatmap(test, annotation_col = annotation_col, annotation_colors = ann_colors[2])

# Gaps in heatmaps

pheatmap(test, annotation_col = annotation_col, cluster_rows = FALSE, gaps_row = c(10, 14))

pheatmap(test, annotation_col = annotation_col, cluster_rows = FALSE, gaps_row = c(10, 14),

        cutree_col = 2)

# Show custom strings as row/col names

labels_row = c("", "", "", "", "", "", "", "", "", "", "", "", "", "", "",

              "", "", "Il10", "Il15", "Il1b")

table(labels_row)

pheatmap(test, annotation_col = annotation_col, labels_row = labels_row)

# Specifying clustering from distance matrix

drows = dist(test, method = "minkowski")

dcols = dist(t(test), method = "minkowski")

pheatmap(test, clustering_distance_rows = drows, clustering_distance_cols = dcols)

# Modify ordering of the clusters using clustering callback option

callback = function(hc, mat){

  sv = svd(t(mat))$v[,1]

  dend = reorder(as.dendrogram(hc), wts = sv)

  as.hclust(dend)

}

pheatmap(test, clustering_callback = callback)

## Not run:

# Same using dendsort package

library(dendsort)

callback = function(hc, ...){dendsort(hc)}

pheatmap(test, clustering_callback = callback)

完整例子:###例子

###创建matrix

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 = "")

###建立annotation数据框

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 = "")

class(test)

str(test)

pheatmap(test,scale = "row",clustering_distance_rows = "euclidean",clustering_distance_cols = "euclidean", clustering_method = "complete",

        cluster_rows = TRUE,cluster_cols = TRUE,cutree_rows = NA, cutree_cols = NA,

        color = colorRampPalette(c("navy", "white", "firebrick3"))(100),

        border_color = "grey60",cellwidth = NA, cellheight = NA,

        legend = TRUE, legend_breaks = NA,legend_labels = NA, breaks = unique(c(seq(-6,6, length=100))),

        annotation_row = annotation_row , annotation_col = annotation_col,annotation = NA, annotation_colors = NA, annotation_legend = TRUE,

        annotation_names_row = TRUE, annotation_names_col = TRUE,

        show_rownames = T, show_colnames = T, main = "Heatmap",

        display_numbers = F, number_format = "%.2f", number_color = "grey30")

相关文章

  • heatmap-热图创建

    首先,我们需要知道,热图的原理就是根据你的matrix或者data.frame中行列数字大小,映射到一个个独立的小...

  • 如何在R中创建日历热图

    首先,我们运行Paul Bleicher创建的calendarHeat函数以显示日历热图。 其次,我们创建一些随机...

  • R学习笔记(2):用pheatmap画个热图

    安装pheatmap包创建测试矩阵画个热图显示色块的数值或文本调整色块或文本大小行列注释列名的文本角度调整切分热图...

  • Chart.js

    Chart.js 创建Chart.js 创建折线图 创建柱状图 雷达图 极地区域图 饼状图与环形图 气泡图 设置图...

  • complexheatmap学习3——热图拼接

    最基本的例子 标题 热图的大小 热图之间的空隙 基于主要的热图自动调整 主要热图的设置 注释部分的调整 热图和注释...

  • 热图

    R绘图基础(四)热图 heatmap:https://qiubio.com/archives/2477 那些年画过...

  • 热图

    「热图」ComplexHeatmap展示单细胞聚类:http://xuzhougeng.top/archives/...

  • 热图

    Create test matrix Draw heatmaps Show text within cells F...

  • 【热图】

    2021.4.28持续更新中。。。 参考:《R数据可视化手册》、学术数据分析及可视化[https://space....

  • 热图

    有点小瑕疵 参考:木舟笔记

网友评论

    本文标题:heatmap-热图创建

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