aplot
aplot copied to clipboard
Can not get right order when connect ggtree and stack barplot in scRNA analysis
Thank you for developmenting the amazing aplot package, yet I have encountered some trouble when using it in scRNA-seq analysis. I intened to connect a ggtree, which is represent the cluster of the samples in data, and a sample-base cell ratio stack barplot. Howerer, no matter how hard I try, I can not get the right order of the samples between these two figure. Here is my code:
#get cell ratio
Cellratio` <- prop.table(table(sce_sub$CellType, sce_sub$Sample), margin = 2)
Cellratio <- as.data.frame(Cellratio)
colnames(Cellratio) <- c("CellType","id","Abundance")
Cellratio$CellType <- factor(Cellratio$CellType,levels = unique(Cellratio$CellType))
Cellratio <- Cellratio %>% dplyr::select(.,id,Abundance,CellType)
#get tree with ggtree
cellper <- reshape2::dcast(Cellratio,id~CellType, value.var = "Abundance")
rownames(cellper) <- cellper[,1]
cellper <- cellper[,-1]
dis_bray <- vegan::vegdist(cellper, method = 'bray',na.rm = T)
tree <- hclust(dis_bray,method = 'complete')
tree <- ape::as.phylo(tree)
ptest <- ggtree(tree) +
geom_tiplab(as_ylab = T)
#get stack bar plot using ggplot2
library(ggthemes)
library(paletteer)
library(aplot)
library(ggstance)
col <- paletteer_d("ggthemes::Classic_20",n=length(unique(meta$CellType)))
p1 <- ggplot(Cellratio) +
geom_bar(aes(x = id,
y= Abundance,
fill = CellType),
stat = "identity",
alpha = 0.8) +
coord_flip()+
labs(x='',y = 'Relative Ratio',title ="Cell Abundance")+
theme(panel.border = element_blank()) +
scale_fill_manual(values = col)
p11 <- p1 +
scale_y_continuous(expand = c(0,0))+
theme(
panel.background = element_rect(fill = NA),
title = element_text(size = 13,face = "bold"),
axis.text.x = element_text(size = 12),
#axis.text.y = element_blank(),
axis.title = element_text(size = 12,face = "bold"),
axis.title.y = element_blank())
p11
#using insert_left function to connect these two:
p2 <- p11 %>% aplot::insert_left(.,ptest,width = 1.2)
p2
Yet the order of the samples are not correct....:

I have also tried facet_plot function from ggtree package with no ideal result.... I completely don't know what's wrong and wish to get some help from you. Thank you very much~