| 前言:自從公開ggheatmap后,筆者收到了許多讀者的鼓勵。真的感激大家的支持與鼓勵~如果在使用過程中存在什么問題,或者有比較希望實現(xiàn)的功能可以聯(lián)系筆者,你的每一個建議對我來說都是一次學習和進步,也希望有機會和大家一起探討學習~ 接下來,筆者將會舉兩個例子,用來說明ggheatmap的用途,希望都過去拋磚引玉的作用(本質(zhì)上:也就heatmap+拼圖)
 前期準備說明:以下數(shù)據(jù)并非真實存在,只是作者隨機生成,所以不同人運行的結果可能存在不同,大家不放心的話,可以使用pheatmap包進行驗證。
 devtools::install_github("XiaoLuo-boy/ggheatmap")library(ggheatmap)
 library(aplot)
 set.seed(123)
 df <- matrix(runif(600,0,10),ncol = 12)
 colnames(df) <- paste("sample",1:12,sep = "")
 rownames(df) <- sapply(1:50, function(x)paste(sample(LETTERS,3,replace = F),collapse = ""))
 df[1:4,1:4]
 
 row_metaData <- data.frame(exprtype=sample(c("Up","Down"),50,replace = T),
 genetype=sample(c("Metabolism","Immune","None"),50,replace = T))
 rownames(row_metaData) <- rownames(df)
 
 col_metaData <- data.frame(tissue=sample(c("Normal","Tumor"),12,replace = T),
 risklevel=sample(c("High","Low"),12,replace = T))
 rownames(col_metaData) <- colnames(df)
 
 exprcol <- c("#EE0000FF","#008B45FF" )
 names(exprcol) <- c("Up","Down")
 genecol <- c("#EE7E30","#5D9AD3","#D0DFE6FF")
 names(genecol) <- c("Metabolism","Immune","None")
 tissuecol <- c("#98D352","#FF7F0E")
 names(tissuecol) <- c("Normal","Tumor")
 riskcol <- c("#EEA236FF","#46B8DAFF")
 names(riskcol) <- c("High","Low")
 col <- list(exprtype=exprcol,genetype=genecol,tissue=tissuecol,risklevel=riskcol)
 
 stackDat <- data.frame(sample=rep(paste0("sample",1:12),each=2),
 count=sample(1:10,24,replace = T),
 type=rep(c("Negative","Positive"),times=12))
 
數(shù)據(jù)說明: row_metaData:行注釋數(shù)據(jù)col_metaData:列注釋數(shù)據(jù)stackDat:每個樣本上調(diào)基因和下調(diào)基因的總數(shù)(為了方便,該數(shù)據(jù)也是隨機生成的)
 Example1 復現(xiàn)圖片1 **圖片出處說明:**本熱圖出自《Cell》文獻:https:///10.1016/j.cell.2020.05.032。起初是作者在“木舟筆記”推送上面看到的圖片,感覺熱圖超級好看的,所以盡力復現(xiàn)出來(只模仿形,不模仿意)。 用法:可以用于對特定基因或者樣本的多重注釋。比如某些基因是免疫基因,某些基因是自噬相關相關基因,某些是腫瘤驅(qū)動基因??赡苣承┗蚩赡芗仁敲庖呋蛞彩亲允上嚓P基因,這也就意味這用條形圖注釋不在合適,所以可以采用這個辦法進行可視化。 代碼實現(xiàn) ggheatmap<- ggheatmap(df,color=colorRampPalette(c( "#66b032","white","#ff3800"))(100),
 cluster_rows = T,cluster_cols = T,scale = "row",
 cluster_num = c(5,3),
 tree_color_rows = c("#3B4992FF","#EE0000FF","#008B45FF","#631879FF","#008280FF"),
 tree_color_cols = c("#1F77B4FF","#FF7F0EFF","#2CA02CFF"),
 annotation_rows = row_metaData,
 annotation_cols = col_metaData,
 annotation_color = col
 )
 
 dat <- data.frame(Glycolysis=sample(c(1,NA),50,replace = T),
 TAC=sample(c(1,NA),50,replace = T),
 gene=rownames(df))
 p1 <- ggplot(dat,aes(x=Glycolysis,y=gene))+
 geom_point(color="#d40749",size=3)+theme_classic()+
 theme(line = element_blank(),axis.text = element_blank(),axis.title.y = element_blank(),
 axis.title.x = element_text(colour ="#d40749",face = "bold",size = 10))+
 xlab("Glycolysis")+scale_x_discrete(position = "top")
 
 p2 <- ggplot(dat,aes(x=TAC,y=gene))+
 geom_point(color="#0092db",size=3)+theme_classic()+
 theme(line = element_blank(),axis.text = element_blank(),axis.title.y = element_blank(),
 axis.title.x = element_text(colour ="#0092db",face = "bold",size = 10))+
 xlab("TAC")+scale_x_discrete(position = "top")
 
 
 ggheatmap%>%insert_right(p1,width = 0.1)%>%insert_right(p2,width = 0.1)
 
 Example1 Example2 Example2 **圖片出處說明:**本圖來源于data.world可視化項目:Energy Use at 10 Downing St in 2017(列為月份,行為日期)(只模仿形,不模仿意)。 用法:用于描述行列元素某一特征的變化趨勢。比如說:如果樣本以我們構建的模型打分的大小排序,同時需要描述特定類型基因的表達情況,那么這樣繪制不免是一個很好的切入點。你也可以把柱形圖換為線圖等等。 代碼實現(xiàn) ggheatmap2<- ggheatmap(df,cluster_rows = T,cluster_cols = T,scale = "row",cluster_num = c(5,3),
 tree_color_rows = c("#3B4992FF","#EE0000FF","#008B45FF","#631879FF","#008280FF"),
 tree_color_cols = c("#1F77B4FF","#FF7F0EFF","#2CA02CFF"),
 annotation_rows = row_metaData,
 annotation_color = col,show_cluster_cols=F
 
 )
 p3 <- ggplot(stackDat, aes(x=sample,y=count, fill=type)) +
 geom_bar(position="stack", stat="identity")+
 scale_fill_manual(values = c("#24c1ff","#ffbd24"))+
 theme_classic()+
 theme(axis.text.x =element_blank(),
 axis.title.x = element_blank(),
 axis.line.x = element_blank(),
 axis.ticks.x = element_blank())+
 scale_y_continuous(expand = c(0,0),position = "right")
 ggheatmap2%>%insert_top(p3,height = 0.1)
 
 Example2 文末友情推薦如果你也恰好是醫(yī)學生,學習R語言也有困難,那么你值得擁有下面的學習班: |