# 安装包
if (!requireNamespace("plot3D", quietly = TRUE)) {
install.packages("plot3D")
}if (!requireNamespace("ggplotify", quietly = TRUE)) {
install.packages("ggplotify")
}
# 加载包
library(ggplot2)
library(stringr)
颜色组柱状图
颜色组柱状图可以用于分组展示数据值,并按顺序标注不同颜色。
环境配置
系统: Cross-platform (Linux/MacOS/Windows)
编程语言: R
依赖包:
plot3D
;ggplotify
数据准备
数据表格(三列):
Term | 条目名称,如 GO/KEGG 通路名称
Count | 条目的数值大小,如某通路富集到的基因数
Type | 该通路所属大类:如 BP/MF/CC/KEGG
# 加载数据
<- read.delim("files/Hiplot/004-barplot-color-group-data.txt", header = T)
data
# 整理数据格式
colnames(data) <- c("term", "count", "type")
"term"] <- str_to_sentence(str_remove(data[,"term"], pattern = "\\w+:\\d+\\W"))
data[,"term"] <- factor(data[,"term"],
data[,levels = data[,"term"][length(data[,"term"]):1])
"type"] <- factor(data[,"type"],
data[,levels = data[!duplicated(data[,"type"]), "type"])
# 查看数据
data
term count type
1 Immune response 20 BP
2 Defense response to bacterium 11 BP
3 Cell chemotaxis 8 BP
4 Cell adhesion 17 BP
5 Complement activation 8 BP
6 Cytokine-mediated signaling pathway 8 MF
7 Phagocytosis, engulfment 5 MF
8 Negative regulation of jak-stat cascade 5 MF
9 Epoxygenase p450 pathway 4 MF
10 Chemokine-mediated signaling pathway 6 MF
11 Negative regulation of leukocyte apoptotic process 3 MF
12 B cell receptor signaling pathway 5 MF
13 Cellular response to tumor necrosis factor 6 CC
14 Positive regulation of chemotaxis 3 CC
15 Positive regulation of angiogenesis 6 CC
16 Collagen fibril organization 4 CC
17 Positive regulation of homotypic cell-cell adhesion 20 KEGG
18 Regulation of cell projection assembly 14 KEGG
19 Prostate epithelial cord elongation 10 KEGG
20 Bile acid catabolic process 8 KEGG
21 Cellular response to drug 7 KEGG
22 Glycosaminoglycan metabolic process 4 KEGG
可视化
# 三维柱状图
<- ggplot(data = data, aes(x = term, y = count, fill = type)) +
p geom_bar(stat = "identity", width = 0.8) +
theme_bw() +
xlab("Count") +
ylab("Term") +
guides(fill = guide_legend(title="Type")) +
ggtitle("Barplot Color Group") +
coord_flip() +
theme_classic() +
scale_fill_manual(values = c("#E64B35FF","#4DBBD5FF","#00A087FF","#3C5488FF")) +
theme(text = element_text(family = "Arial"),
plot.title = element_text(size = 12,hjust = 0.5),
axis.title = element_text(size = 12),
axis.text = element_text(size = 10),
axis.text.x = element_text(angle = 0, hjust = 0.5,vjust = 1),
legend.position = "right",
legend.direction = "vertical",
legend.title = element_text(size = 10),
legend.text = element_text(size = 10))
p

上图展示了 GO/KEGG 通路富集分析结果的可视化。