
dotplot气泡图.png
#object是seurat对象,features是需要展示在横坐标轴上的genes
library(dplyr);library(ggtree);library(cowplot)
df <- DotPlot(object,features = features)$data

head(df).png
#获取气泡图中的相关信息,为构建层次聚类图作准备
mat <- df %>% dplyr::select(-pct.exp, -avg.exp.scaled) %>% tidyr::pivot_wider
(names_from = features.plot, values_from = avg.exp ) %>% data.frame()
row.names(mat) <- mat$id
mat <- mat[,-1]

head(mat).png
#计算不同细胞类型之间的相关性
clust <- hclust(dist(mat %>% as.matrix()))
ddgram <- as.dendrogram(clust)
#绘制层次聚类图
ggtree_plot <- ggtree::ggtree(ddgram,branch.length="none")+
geom_tippoint(color="#FDAC4F", shape=8, size=3)
#绘制气泡图总图
df$id <- factor(df$id , levels = clust$labels[clust$order])
dotplot <- ggplot(df,aes(x=features.plot,y = id,size = pct.exp, color = avg.exp.scaled))+
geom_point() +
scale_size("% detected", range = c(0,6)) +
scale_y_discrete(position = "right") +
scale_color_gradientn(colours = viridis::viridis(20),
guide = guide_colorbar(ticks.colour = "black",frame.colour = "black"),
name = "Average\nexpression") +
cowplot::theme_cowplot() +
ylab("") + xlab("Markers") + theme_bw() +
theme(
axis.text.x = element_text(size=10, angle=0, hjust=0.5, color="black"),
axis.text.y = element_text(size=12, color="black"),
axis.title = element_text(size=14)
)
ggtree_plot_yset <- ggtree_plot + aplot::ylim2(dotplot)
p <- plot_grid(ggtree_plot_yset,NULL,dotplot, nrow = 1,
rel_widths = c(0.5,-0.025, 2),
align = 'h')
#保存图片
pdf("dotplot.pdf",height=8,width=8)
print(p)
dev.off()
完整代码>>>github.com
网友评论