❝本节分享如何基于差异基因分析的结果来绘制热图,主要还是基于ggplot2体系来实现,针对以往的代码风格,这次小编通过拆分数据定义每一部分的函数来编写新的代码,希望对各位观众老爷能有新的帮助,数据和代码已经被打包并上传到小编的2023VIP会员交流群。已经加群的朋友可以自行下载。如果你需要,可以参考文末的方式进行购买。 ❞
关注下方公众号下回更新不迷路
加载R包
代码语言:javascript复制library(tidyverse)
library(ggtree)
library(aplot)
library(scico)
读取并处理数据
代码语言:javascript复制process_data <- function() {
gene <- read_tsv("diff.txt") %>%
arrange(desc(abs(logFC))) %>%
head(11) %>%
pull(id)
df <- read_tsv("diffGeneExp.txt") %>%
pivot_longer(-id) %>%
filter(id %in% gene) %>%
pivot_wider(., names_from=name, values_from=value) %>%
column_to_rownames(var="id")
dff <- df %>%
scale() %>%
as.data.frame() %>%
rownames_to_column(var="id") %>%
pivot_longer(-id) %>%
mutate(type = map_chr(name, ~str_split(.x, "_")[[1]][length(str_split(.x, "_")[[1]])]))
list(df = df, dff = dff)
}
生成热图
代码语言:javascript复制generate_heatmap <- function(dff) {
ggplot(dff, aes(name, id, fill=value))
geom_tile()
scale_y_discrete(position = "right", expand = c(0,0))
scale_fill_scico(palette = 'cork')
labs(x=NULL, y=NULL)
theme(axis.text.x=element_blank(),
axis.text.y=element_text(color="black", size=8),
axis.ticks = element_blank(),
panel.background = element_blank(),
plot.background = element_blank(),
legend.background = element_blank(),
legend.title =element_blank())
}
生成树状图
代码语言:javascript复制generate_tree <- function(df) {
hclust(dist(df, method = "binary")) %>%
ggtree(layout="rectangular", branch.length="none")
theme_void()
}
生成类型图
代码语言:javascript复制generate_type_plot <- function(dff) {
dff %>%
mutate(Type='Type') %>%
ggplot(aes(name, Type, fill=type))
geom_tile()
scale_y_discrete(position = "right", expand = c(0,0))
scale_fill_manual(values=c("#3B9AB2","#78B7C5"))
theme(panel.background = element_blank(),
axis.ticks = element_blank(),
axis.title = element_blank(),
axis.text.x = element_blank(),
plot.background = element_blank())
}
主函数
代码语言:javascript复制main <- function() {
data <- process_data()
heatmap <- generate_heatmap(data$dff)
phr <- generate_tree(data$df)
type_plot <- generate_type_plot(data$dff)
heatmap %>%
insert_left(phr, width = 0.2) %>%
insert_top(type_plot, height = 0.08)
}
绘图
代码语言:javascript复制main()
❝本节内容介绍到此结束,过程仅供参考;