使用R语言绘制热图heatmap

读取数据

注意:直接导入excel绘制热图时可能会出现Na报错,先另存为txt再导入

# 读取data
data <- read.table("heatmap/data.txt",
                   row.names=1,# 第一列为行名
                   header=T,# 第一行为变量名
                   sep="\t") # 指定分隔符号

# 读取用来制作热图的行
datai <- data[1:27,]

数据处理

# 如果datai直接绘制热图时报错:需要数值型
# 则先将datai中数据格式转换为数值型(会丢失行名)
datan <- as.data.frame(lapply(datai,
                              function(x) as.numeric(as.character(x))))

# 返回行名 rownames(datai)
# 返回列名 colnames(datai)
# 将行名赋给datan
row.names(datan) <- rownames(datai)

绘制Heatmap

# 构建分组信息
annotation_col = data.frame(Rank = factor(c("Info. gain", "Gain ratio","Gini"
                                            ,"Info. gain", "Gain ratio","Gain ratio"
                                            ,"Gain ratio", "Gain ratio","Gain ratio")),
                            Moddel = factor(c("Neural Network", "Neural Network","Naive Bayes"
                                              ,"Neural Network", "Neural Network","Random Forest"
                                              ,"Neural Network", "Logistic Regression","Naive Bayes")))


rownames(annotation_col) <- colnames(datan)

保存PDF时

landscape mode是指宽度比高度宽的模式,也就是俗称的宽屏模式;

portrait mode是指高度比宽度高的模式,也就是俗称的竖屏模式;

# 修改参数,绘制heatmap
p <- pheatmap(datan,
              scale="column",# 按列标准化
              border_color = "white",
              cluster_rows = F,
              cluster_cols = F,
              legend = F, # 去掉图例
              angle_col = 315, # 标签角度(“270”, “0”, “45”, “90”, “315”其中的一个)
              #main = "标题",
              #cellwidth = 60,cellheight = 20, # 设置热图方块宽度和高度
              #display_numbers = T, # 热图上显示数值
              annotation_col = annotation_col,# 加入分组信息
              )

更详细参数参考:https://blog.csdn.net/qazplm12_3/article/details/109172871

posted @ 2022-04-19 14:07  梅雨明夏  阅读(884)  评论(0编辑  收藏  举报