Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can not get right order when connect ggtree and stack barplot in scRNA analysis #25

Closed
BIOBRICK opened this issue Apr 9, 2023 · 4 comments

Comments

@BIOBRICK
Copy link

BIOBRICK commented Apr 9, 2023

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....:
Rplot1
Rplot2

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~

@RaSieb
Copy link

RaSieb commented Feb 5, 2025

I have a similar issue.
I have been able to boil it down to this case based on your example from the documentation, but with a dataset created by pivot_longer to include more values (a very common use case) and using geom_text() for p2.

library(ggtree)
library(ggstance)
library(aplot)
set.seed(2020-03-27)
x <- rtree(10)
d <- data.frame(
  taxa = x$tip.label, 
  value = abs(rnorm(10)),
  value2 = abs(rnorm(10))
  )
d2 <- pivot_longer(d, cols = c(value, value2) )

p <- ggtree(x) %<+% d2
p <- p + geom_tiplab(align = TRUE) + xlim(NA, 4)

p2 <- ggplot(d2) +
  geom_text(aes(x = name, y = taxa, label=value), hjust=0, size=2) 

p2 %>% insert_left(p)

This results in the following figure:

example image

Please help, I'd really like to use aplot, but it is currently not possible due to this issue.

Using aplot version aplot_0.2.4 and ggtree version ggtree_3.14.0

@GuangchuangYu
Copy link
Member

p2 = ggplot(d2, aes(name, taxa)) + geom_text(aes(label=value), hjust=0, size=2)
p2 %>% insert_left(p)

should works

@GuangchuangYu
Copy link
Member

insert_XXX() extracts x and y variable from aes() in ggplot().

@RaSieb
Copy link

RaSieb commented Feb 5, 2025 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants