❝今天在帮朋友做数据分析的时候,由于基因数目比较多聚类后图非常臃肿,突然产生一个想法如何只对图形进行聚类但不显示聚类树,下面通过一个小例子来展示 ❞
加载R包
主要使用ggdendro
中的函数dendro_data
来提取聚类标签
library(tidyverse)
# install.packages("ggdendro")
library(ggdendro)
library(aplot)
导入数据
代码语言:javascript复制df <- read_tsv("data.xls") %>% column_to_rownames(var="Pathway")
df2 <- df %>% rownames_to_column(var="Pathway") %>% pivot_longer(-Pathway)
# 提取行列聚类标签信息
hrdata <- hclust(dist(df)) %>% dendro_data(.,type = "rectangle")
hcdata <- hclust(dist(t(df))) %>% dendro_data(.,type = "rectangle")
定义因子
代码语言:javascript复制df2$Pathway <- factor(df2$Pathway,levels = hrdata$labels %>% select(label) %>% pull())
df2$name <- factor(df2$name,levels = hcdata$labels %>% select(label) %>% pull())
绘制热图
代码语言:javascript复制heatmap <- df2 %>% ggplot(aes(name,Pathway,fill=value))
geom_tile()
labs(x=NULL,y=NULL)
scale_y_discrete(expand=c(0,0),position="left")
scale_x_discrete(expand=c(0,0))
scale_fill_gradientn(colours = rev(RColorBrewer::brewer.pal(11,"RdBu")))
theme(axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks = element_blank(),
legend.title = element_blank(),
legend.text = element_text(size=8,color="black"))
guides(fill=guide_colorbar(direction = "vertical",reverse = F,barwidth = unit(.5, "cm"),
barheight = unit(10,"cm")))
绘制分组条图
代码语言:javascript复制group1 <- read_tsv("group.xls") %>% mutate(type="A") %>%
ggplot(aes(Sample_id,type,fill=`Subtype-1`))
geom_tile()
scale_fill_manual(values=c("#3B9AB2", "#78B7C5"))
theme_void()
theme(legend.title = element_blank(),
legend.spacing.x=unit(0.1,'cm'),
legend.key.width=unit(0.5,'cm'),
legend.key.height=unit(0.5,'cm'),
legend.background=element_blank(),
legend.text = element_text(size=8,color="black"))
拼图
代码语言:javascript复制heatmap %>% insert_top(group1,height = 0.04)