平时在阅读文献时经常会看到一些加标签的图,比如下面这张:
那如何来方便地绘制呢?下面就介绍下如何使用R包ggpubr来绘制带有文本标签的直方图和密度图。
下面将使用ggpubr包中的gene_citation数据集,它包含通过使用两个关键词( i)基因名称+ b细胞分化和 ii)基因名称+浆细胞分化)评估PubMed中摘要和注释筛选的66个基因的平均引用指数。每个基因的引文数作为使用两个关键词获得的平均引文数,仅保留平均引文数>=3的基因。
绘制基因引文索引的条形图:
library(ggpubr)
# 加载数据
data(gene_citation)
head(gene_citation)
## gene citation_index
## 2 CASP3 68.0
## 4 CDK6 10.5
## 7 CCND2 10.0
## 8 SCD 8.5
## 10 SLAMF6 4.5
## 11 BCL2L1 56.5
ggbarplot(gene_citation, x = "gene", y = "citation_index",
fill = "lightgray",
xlab = "Gene name", ylab = "Citation index",
sort.val = "desc", # 按降序排序
top = 20, # 选择前20个基因
x.text.angle = 45 # x轴标签旋转45度
)
上图显示了一些已知的与浆细胞分化有关的关键基因。
# 一些关键基因
key.gns <- c("MYC", "PRDM1", "CD69", "IRF4", "CASP3",
"BCL2L1", "MYB", "BACH2", "BIM1", "PTEN",
"KRAS", "FOXP1", "IGF1R", "KLF4", "CDK6", "CCND2",
"IGF1", "TNFAIP3", "SMAD3", "SMAD7",
"BMPR2", "RB1", "IGF2R", "ARNT")
# 绘制带标签的直方图
gghistogram(gene_citation, x = "citation_index", y = "..count..",
xlab = "Number of citation",
ylab = "Number of genes",
binwidth = 5,
fill = "lightgray", color = "black",
label = "gene", label.select = key.gns, repel = TRUE,
font.label = list(color= "citation_index"),
xticks.by = 20, # x轴刻度步长值
gradient.cols = c("blue", "red"),
legend = c(0.7, 0.6),
legend.title = "" # 隐藏图例标题
)
# 带标签的密度图
ggdensity(gene_citation, x = "citation_index", y = "..count..",
xlab = "Number of citation",
ylab = "Number of genes",
fill = "lightgray", color = "black",
label = "gene", label.select = key.gns, repel = TRUE,
font.label = list(color= "citation_index"),
xticks.by = 20, # x轴刻度步长值
gradient.cols = c("blue", "red"),
legend = c(0.7, 0.6),
legend.title = "" # 隐藏图例标题
)