- 加载R包
library(DOSE)
library(clusterProfiler)
library(org.Hs.eg.db)
library(stringr)
library(forcats)
library(ggplot2)
library(ggh4x)
library(RColorBrewer)
library(tidyverse)
library(ggthemes)
library(ggsci)
- 加载数据集
data(geneList)
gene <- names(geneList)[1:2000]
- KEGG富集分析
eK <- enrichKEGG(gene = gene,
organism = 'hsa',
pvalueCutoff = 0.05,
qvalueCutoff = 0.05)
- 结果转换
eK_res <- as.data.frame(eK)
- 选取前20项进行可视化
eK_data <- eK_res[1:min(20, nrow(eK_res)), ]
- 给各通路加上对应map号
eK_data$label <- sprintf("%s\n(%s)", eK_data$Description, eK_data$ID)
- ggplot2柱状图可视化
#转换成因子
eK_data1 <- mutate(eK_data, label = factor(label, levels = unique(label)))
#计算富集因子
eK_data1 <- eK_data1 %>%
separate(GeneRatio, into = c("GeneHitsInSelectedSet",
"AllGenesInSelectedSet"), sep = "/") %>%
separate(BgRatio, into = c("GeneHitsInBackground",
"AllGenesInBackground"), sep = "/") %>%
mutate(across(c(GeneHitsInSelectedSet,AllGenesInSelectedSet,
GeneHitsInBackground, AllGenesInBackground), as.numeric)) %>%
mutate(enrichFactor = (GeneHitsInSelectedSet * AllGenesInBackground) /
(AllGenesInSelectedSet * GeneHitsInBackground))
#ggplot2可视化:按显著性排序的柱状图
eK_bar <-
ggplot(eK_data1, aes(x = Count, y = label, fill = p.adjust)) +
geom_bar(stat = "identity", color = "black", linewidth = 0.4, alpha = 0.8,
width = 0.75) +
scale_x_continuous(expand = c(0.025,0)) +
#scale_fill_viridis_c(option = "mako") +
scale_fill_gradient(low="white",high="#d73026",name="p.adjust") +
labs(x = "Count", y= NULL) +
theme_minimal(base_size = 14) +
theme(axis.text = element_text(color = "black")) +
theme(panel.grid.minor = element_blank(),
panel.grid.major = element_blank()) +
theme(axis.line.x = element_line(color = "black", linewidth = 0.35),
axis.ticks.x = element_line(color = "black", linewidth = 0.35),
axis.ticks.length = unit(0.3,"cm")) +
guides(x = "axis_truncated", y = "axis_truncated")
eK_bar
按显著性排序的柱状图.png
- ggplot2气泡图可视化
#ggplot2可视化:按显著性和富集因子排序的气泡图
eK_dot <-
ggplot(eK_data1,aes(x = enrichFactor, y = label)) +
geom_point(aes(size = Count, fill = p.adjust),
alpha = 1,
shape = 21,
stroke=0.5,
) +
scale_x_continuous(expand = c(0.05,0)) +
scale_fill_gradient(low="white",high="#e1415f",name="p.adjust") +
labs(color=expression(p.adjust),
size="Count",
x="enrichFactor",
y=" ") +
guides(fill = NULL,
size = guide_legend(order=1)) +
theme_linedraw(base_size = 14) +
theme(axis.text = element_text(color="black"),
axis.ticks.length = unit(0.25,"cm")) +
theme(panel.grid.minor = element_blank(),
panel.grid.major = element_line(color = "black", linewidth = 0.25),
panel.border = element_rect(fill=NA,linewidth = 1),
legend.background = element_blank()
)
eK_dot
富集气泡图.png
- ggplot2分类柱状图可视化
#按照分类重新排序
eK_data2 <- eK_data %>%
mutate(label = fct_reorder(label, category))
#ggplot2可视化:按分类排序的柱状图
eK_col <-
ggplot(eK_data2, aes(x = Count,
y = label,
fill = category))+
geom_bar(stat="identity",color = "black", linewidth = 0.4,
alpha = 0.8,width = 0.75,
key_glyph = "point") +
labs(x = NULL, y= NULL,
fill = "Category") +
scale_x_continuous(expand = c(0.025,0)) +
scale_fill_viridis_d() +
theme_minimal(base_size = 13) +
theme(axis.text = element_text(color = "black")) +
theme(panel.grid.minor = element_blank(),
panel.grid.major = element_blank()) +
theme(axis.line.x = element_line(color = "black", linewidth = 0.35),
axis.ticks.x = element_line(color = "black", linewidth = 0.35),
axis.ticks.length = unit(0.3,"cm")) +
guides(x = "axis_truncated", y = "axis_truncated") +
guides(fill = guide_legend(override.aes = list(shape = 21, size = 5,
color = "white")))
eK_col
按分类排序的柱状图.png