R代码4-热图heatmap绘制

首席躺平官 2021-07-29 16:07:53 阅读: 1651
setwd("D:/geo/GSE70689/")

library("pheatmap")

exprSet = read.csv("exprSet.csv", header = T, row.names = 1)

#查看所有基因的热图
pdf(file = "heatmap_all.pdf", width=6, height=8)
pheatmap(
  exprSet,
  scale = 'row',
  clustering_distance_rows = "euclidean",
  display_numbers=F,
  number_format="%.2f",
  number_color="grey",
  fontsize_number=6
)
dev.off()


#查看部分基因的热图,下面展示20个基因的热图,只需要修改下面的基因名称
geneSets = c("Eef1g","Nsep1","3010033P07Rik","Actb","Gapd","Uba52","Rpl13","Rps8","Ubb","Fth1",
             "Rpl24","Hist1h2ai","Rps5","Sap30","Rps27a","Rpl41","Krtcap2","Saa3","Rps10")
dataTemp = exprSet[row.names(exprSet) %in% geneSets,]
pdf(file = "heatmap_some_gene.pdf", width=6, height=8)
pheatmap(
  dataTemp,
  scale = 'row',
  clustering_distance_rows = "euclidean",
  display_numbers=F,
  number_format="%.2f",
  number_color="grey",
  fontsize_number=6
)
dev.off()




#为每一列添加分组标签
sampleInfo = read.csv("sampleInfo.csv", header = T, row.names = 1)
head(sampleInfo)
data = exprSet
colnames(data) = sampleInfo$title


geneSets = c("Eef1g","Nsep1","3010033P07Rik","Actb","Gapd","Uba52","Rpl13","Rps8","Ubb","Fth1",
             "Rpl24","Hist1h2ai","Rps5","Sap30","Rps27a","Rpl41","Krtcap2","Saa3","Rps10")
dataTemp = data[row.names(data) %in% geneSets,]


annotation_col = data.frame(
  "Sample" = c("THC_LPS","Cntr","LPS","THC","LPS","THC","THC_LPS","CBD_LPS","Cntr","Cntr","THC","LPS","CBD","Cntr","THC","LPS","CBD","LPS","LPS","THC_LPS","CBD_LPS","THC_LPS","Cntr","Cntr"),
  "other" = c(rep(1:6,each=4)) #这个是其他属性
)
row.names(annotation_col) = sampleInfo$title

pdf(file = "heatmap_some_gene_info.pdf", width=8, height=8)
pheatmap(
  dataTemp,
  scale = 'row',
  clustering_distance_rows = "euclidean",
  display_numbers=F, #这里改为T,则会在热图的每个格子中显示数值
  number_format="%.2f",
  number_color="grey",
  fontsize_number=6,
  annotation_col = annotation_col,
  cellwidth = 12, #每个小格子宽度
  cellheight = 12, #每个小格子高度
  border_color = NA #不需要每个格子的边框
)
dev.off()
邀请讨论

附件

{{f.title}} 大小 {{f.file_size}} 下载 {{f.count_download}} 金币 {{f.count_gold}}
{{item.nick_name}} 受邀请回答 {{item.create_time}}
{{item.refer_comment.nick_name}} {{item.refer_comment.create_time}}

附件

{{f.title}} 大小 {{f.file_size}} 下载 {{f.count_download}} 金币 {{f.count_gold}}
切换到完整回复 发送回复
赞({{item.count_zan}}) 踩({{item.count_cai}}) 删除 回复 关闭
科研狗©2015-2024 科研好助手,京ICP备20005780号-1 建议意见

服务热线

178 0020 3020

微信服务号