专栏名称: EasyCharts
EasyCharts,易图表,我们将定期推送各种数据可视化与分析教程,包括Excel(Power BI)、Origin、Sigmaplot、GraphPad、R、Python、Matlab、Tableau、D3.js等。
目录
相关文章推荐
前端大全  ·  前端行情变了,差别真的挺大。。。 ·  昨天  
前端大全  ·  真的建议所有前端立即拿下软考(红利期) ·  3 天前  
前端大全  ·  React+AI 技术栈(2025 版) ·  3 天前  
奇舞精选  ·  从 DeepSeek 看25年前端的一个小趋势 ·  3 天前  
奇舞精选  ·  从 DeepSeek 看25年前端的一个小趋势 ·  3 天前  
51好读  ›  专栏  ›  EasyCharts

pheatmap|暴雨暂歇,“热图”来袭!!!

EasyCharts  · 公众号  · 前端  · 2019-12-05 23:36

正文


送书回馈



《Scratch 3.0 少儿人工智能编程指南》


内容简介: 本书第 1 章介绍 Scratch ,以及它能够实现的内容;然后引出第 2 Scratch 3.0 的界面知识与操作方法,介绍 Scratch 3.0 的一些基本操作;接着在第 3~6 章以案例形式介绍了 Scratch 3.0 在动画制作、游戏设计、数学编程、硬件编程 4 个方面的实现方法和步骤;第 7 章介绍 Scratch 官方社区的相关内容。

本书适合小学到高中阶段初次接触编程的少年儿童学习,既可以作为他们自学编程的入门教材,也可以作为一般培训机构的少儿编程入门教材。读者可以从 Scratch 图形化编程入手,首先了解计算机编程的一些基本概念,以便后续学习与理解高级编程语言。

阅读原文可达京东购买链接:

https://item.jd.com/12758648.html


福利 留言点赞第1位的朋友可以免费获得该书1本,截至日期12月8日。


热图可以聚合大量的数据,并可以用一种渐进色来优雅地表现,可以很直观地展现数据的疏密程度或频率高低。

本文利用R语言 p heatmap 包从头开始绘制各种漂亮的热图。参数像积木,拼凑出你最喜欢的热图即可,如下图:

基因和样本都可以单独聚类,排序,聚类再分组,行列注释,配色调整,调整聚类线以及单元格的宽度和高度均可实现。

载入数据,R包

#R包library(pheatmap)# 构建测试数据 set.seed(1234)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 = "")head(test[,1:6])


绘制热图

绘制默认热图

pheatmap(test)


基本参数

# scale = "row"参数对行进行归一化
# clustering_method参数设定不同聚类方法,默认为"complete",可以设定为'ward', 'ward.D', 'ward.D2', 'single', 'complete', 'average', 'mcquitty', 'median' or 'centroid'

pheatmap(test,scale = "row", clustering_method = "average")



#表示行聚类使用皮尔森相关系数聚类,默认为欧氏距离"euclidean"

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



#行 列是否聚类,cluster_row ,cluster_col

pheatmap(test, cluster_row = FALSE,cluster_col = TRUE)



# treeheight_row和treeheight_col参数设定行和列聚类树的高度,默认为50

pheatmap(test, treeheight_row = 30, treeheight_col = 50)



# 设定cell 的大小

pheatmap(test, cellwidth = 15, cellheight = 12, fontsize = 10)


设定 text


热图中展示数值

# display_numbers = TRUE参数设定在每个热图格子中显示相应的数值,#number_color参数设置数值字体的颜色

pheatmap(test, display_numbers = TRUE,number_color = "blue")



# 设定数值的显示格式

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


#设定条件式展示

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



设置 legend


设定legend展示的值
#legend_breaks参数设定图例显示范围,legend_labels参数添加图例标签

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


#去掉legend

pheatmap(test, legend = FALSE)


设定 color

自定义颜色

# colorRampPalette

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



# border_color参数设定每个热图格子的边框色

# border=TRIUE/FALSE参数是否要边框线

pheatmap(test, border_color = "red", border=TRUE)


设定 annotations

# 生成行 列的注释

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


#添加列的注释

pheatmap(test, annotation_col = annotation_col)



#添加行 列的注释

# angle_col 改变列标签的角度

pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row, angle_col = "45")



# 根据聚类结果,自定义注释分组及颜色

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_row=annotation_row, annotation_colors = ann_colors, main = "Title")


设定 gap


#根据聚类结果,设定行gap
pheatmap (test, annotation_col = annotation_col, cluster_rows = FALSE , gaps_row = c ( 10 , 14 ))

#根据聚类结果,设定列gap

pheatmap(test,annotation_col = annotation_col, cluster_rows = FALSE,cutree_col = 2)


#展示行或者列的label
labels_row = c ( "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "Il10" , "Il15" , "Il1b" )
pheatmap (test, annotation_col = annotation_col, labels_row = labels_row)

热图汇总

pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row, annotation_colors = ann_colors,gaps_row = c(10, 14),cutree_col = 2,main = "Pheatmap")



输出结果

A = pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row, annotation_colors = ann_colors,gaps_row = c(10, 14),cutree_col = 2,main = "Pheatmap") #记录热图的行排序
order_row = A$tree_row$order #记录热图的列排序order_col = A$tree_col$order # 按照热图的顺序,重新排原始数据result = data.frame(test[order_row,order_col]) # 将行名加到表格数据中result = data.frame(rownames(result),result,check.names =F)  colnames(result)[1] = "geneid" #result结果按照热图中的顺序write.table(result,file="reorder.txt",row.names=FALSE,quote = FALSE,sep='\t')  

R的当前工作目录下即可查看热图的结果。









请到「今天看啥」查看全文