CELL_discret.jpg
前言
多組學(xué)文章經(jīng)常出現(xiàn)非連續(xù)變量的熱圖或者叫格子圖。舉幾個例子:
Snipaste_2021-11-25_22-38-06
Snipaste_2021-11-25_22-39-29
以上兩個圖都來自2021.09
的一篇Cell
,標(biāo)題是Proteogenomic characterization of pancreatic ductal adenocarcinoma
。今天就不細(xì)講這兩幅圖了。這種圖給我們展示離散/分類變量的差異提供了一個思路。今天就簡單介紹幾種常用的畫這種圖的方法。
22
常用方法
構(gòu)建一個分類變量組成的示例數(shù)據(jù)。
library(ggplot2)
library(tidyverse)
library(reshape2)
library(RColorBrewer)
clinical.df=data.frame(
patient=paste("P",seq(1:15),sep = ""),
age=sample(c("young","old"),15,replace = T),
gender=sample(c("male","female"),15,replace = T),
symptom=sample(c("mild","moderate","severe"),15,replace = T),
RNAseq=sample(c("yes","no"),15,replace = T),
WES=sample(c("yes","no"),15,replace = T)
)
head(clinical.df)
> head(clinical.df)
patient age gender symptom RNAseq WES
1 P1 old female moderate yes no
2 P2 old male moderate yes no
3 P3 old male moderate yes yes
4 P4 young female severe yes yes
5 P5 old female moderate no no
6 P6 young male moderate no no
# 長寬轉(zhuǎn)換 已備作圖
clinical.df2=melt(clinical.df,id="patient")
head(clinical.df2)
> head(clinical.df2)
patient variable value
1 P1 age old
2 P2 age old
3 P3 age old
4 P4 age young
5 P5 age old
6 P6 age young
geom_tile
Color<-brewer.pal(9, "Set3") # 設(shè)置顏色
# 設(shè)置因子順序
clinical.df2$patient=factor(clinical.df2$patient,levels = paste("P",seq(1:15),sep = ""))
clinical.df2$variable=factor(clinical.df2$variable,levels = c("WES","RNAseq","symptom","gender","age"))
ggplot(clinical.df2, aes(x = patient, y = variable, fill = value)) +
geom_tile(color = "white", size = 0.25) +
scale_fill_manual(name = "Category",
#labels = names(sort_table),
values = Color)+
theme(#panel.border = element_rect(fill=NA,size = 2),
panel.background = element_blank(),
plot.title = element_text(size = rel(1.2)),
axis.title = element_blank(),
axis.ticks = element_blank(),
legend.title = element_blank(),
legend.position = "right")
image-20211125225237494
ggwaffle
devtools::install_github("liamgilbey/ggwaffle") # 下載包
library(ggwaffle)
ggplot(clinical.df2, aes(patient, variable, fill = value)) +
geom_waffle()+
scale_fill_manual(name = "Category",
#labels = names(sort_table),
values = Color)+
theme(#panel.border = element_rect(fill=NA,size = 2),
panel.background = element_blank(),
plot.title = element_text(size = rel(1.2)),
axis.title = element_blank(),
axis.ticks = element_blank(),
legend.title = element_blank(),
legend.position = "right")
和geom_tile
異曲同工。
image-20211125225912215
ComplexHeatmap
ComplexHeatmap
應(yīng)該是最能還原本文前言圖的包,不過我這里暫時還沒時間搞定,后續(xù)發(fā)復(fù)現(xiàn)版本的代碼。
row.names(clinical.df) <- clinical.df[,1]
clinical.df <- clinical.df[,-1]
clinical.df3 <- data.frame(t(clinical.df))
# 上面的代碼為了將數(shù)據(jù)轉(zhuǎn)為熱圖矩陣
library(ComplexHeatmap)
Heatmap(clinical.df3)
image-20211125230958807
未經(jīng)雕飾的圖確實(shí)不是很美觀。
總結(jié)
以上就是我所知的幾種常用的畫離散變量的熱圖的方法。如果大家有更巧妙的想法,歡迎在后臺留言互相學(xué)習(xí)交流。
參考
R繪圖(2): 離散/分類變量如何畫熱圖/方塊圖 - 簡書 (jianshu.com)
往期
- 跟著Nature學(xué)作圖 | 配對啞鈴圖+分組擬合曲線+分類變量熱圖
- (免費(fèi)教程+代碼領(lǐng)取)|跟著Cell學(xué)作圖系列合集
- 跟著Nat Commun學(xué)作圖 | 1.批量箱線圖+散點(diǎn)+差異分析
- 跟著Nat Commun學(xué)作圖 | 2.時間線圖
- 跟著Nat Commun學(xué)作圖 | 3.物種豐度堆積柱狀圖
- 跟著Nat Commun學(xué)作圖 | 4.配對箱線圖+差異分析