前期设置

setwd("D:/R/R-4.0.5/bin/project_visual/visualization/GSE24673")
rm(list=ls())
options(stringsAsFactors =  F)#在调用as.data.frame的时,将stringsAsFactors设置为FALSE可以避免character类型自动转化为factor类型

数据准备

suppressPackageStartupMessages(library(pheatmap)) 
suppressPackageStartupMessages(library(tidyverse)) 

load("D:/R/R-4.0.5/bin/project_visual/visualization/GSE24673/prepare_heatmap.Rdata")
## DEG_mtx  exprSet_dif  group_list_dif

#从差异表达矩阵中挑选基因
dif <- DEG_mtx %>% filter(adj.P.Val <0.05 & abs(logFC) > 2)
dim(dif)#筛选出来73个基因
## [1] 73  6
head(dif)#筛选后的表达矩阵,为了挑选前25位基因
##             logFC   AveExpr        t      P.Value    adj.P.Val        B
## DDX3Y    4.256833  8.575889 63.37291 5.671376e-14 1.063610e-09 19.43568
## UTY      3.163833  7.632556 43.37164 2.178363e-12 2.042651e-08 17.60272
## RPS4Y1   2.869333  7.865222 32.42195 3.548980e-11 2.218586e-07 15.69950
## PRUNE2   2.713000  9.497667 29.60698 8.459667e-11 3.768028e-07 15.02565
## HLA-DPA1 2.370667  8.736778 29.07918 1.004593e-10 3.768028e-07 14.88817
## GNGT2    2.383667 10.619778 24.42084 5.306366e-10 1.105729e-06 13.49148
getgene <- head(rownames(dif),25)
getgene_matrix <- exprSet_dif[getgene,]
dim(getgene_matrix)
## [1] 25  9
head(getgene_matrix,3)#最终画图使用的数据,选择前25个基因的表达值画图
##        GSM607938 GSM607939 GSM607940 GSM607941 GSM607942 GSM607943 GSM607944
## DDX3Y      9.905    10.104     9.931     5.796     5.643     5.775     9.965
## UTY        8.530     8.675     8.622     5.529     5.580     5.461     8.732
## RPS4Y1     8.936     8.647     8.905     5.965     5.938     5.954     8.652
##        GSM607945 GSM607946
## DDX3Y     10.034    10.030
## UTY        8.803     8.761
## RPS4Y1     8.982     8.808
#生成分组信息
group_list_dif <- data.frame(group_list_dif)
rownames(group_list_dif) <- colnames(exprSet_dif)
head(group_list_dif,3)#生成用来标注分组情况的数据框(annotation_col)
##           group_list_dif
## GSM607938           prcs
## GSM607939           prcs
## GSM607940           prcs

pheatmap

getgene_matrix <- t(scale(t(getgene_matrix)))
head(getgene_matrix)#按行标准化后成为[-1,1]之间的数字
##          GSM607938 GSM607939 GSM607940 GSM607941 GSM607942 GSM607943 GSM607944
## DDX3Y    0.6241030 0.7175462 0.6363116 -1.305336 -1.377180 -1.315197 0.6522768
## UTY      0.5665072 0.6580378 0.6245818 -1.327859 -1.295665 -1.370783 0.6940187
## RPS4Y1   0.7439787 0.5431809 0.7224399 -1.320279 -1.339038 -1.327921 0.5466549
## PRUNE2   0.5803355 0.6420161 0.6229245 -1.327355 -1.405925 -1.250989 0.5634468
## HLA-DPA1 0.6289247 0.5490716 0.5826940 -1.292592 -1.299317 -1.393460 0.6675904
## GNGT2    0.7591108 0.6915581 0.6281753 -1.382560 -1.408414 -1.184906 0.4497027
##          GSM607945 GSM607946
## DDX3Y    0.6846767 0.6827985
## UTY      0.7388371 0.7123248
## RPS4Y1   0.7759396 0.6550441
## PRUNE2   0.8087004 0.7668457
## HLA-DPA1 0.7297917 0.8272965
## GNGT2    0.7566089 0.6907241
pheatmap(getgene_matrix,
         show_rownames = F,
         #border_color = NA,
         treeheight_col = 10,
         treeheight_row = 10,
         annotation_col = group_list_dif)

heatmap

heatmap(getgene_matrix)

heatmap.2

suppressPackageStartupMessages(library(gplots))
heatmap.2(getgene_matrix,key = T,symkey = F,density.info = "none", trace = "none")

ggplot

suppressPackageStartupMessages(library(reshape2))
getgene_matrixl <- melt(getgene_matrix)
colnames(getgene_matrixl) <- c("gene","sample","value")
ggplot(getgene_matrixl,aes(sample,gene))+
  geom_tile(aes(fill=value),colour = "white")+
  scale_fill_gradient(low = "white",high = "steelblue")