ggtree icon indicating copy to clipboard operation
ggtree copied to clipboard

Only one category appearing with geom_fruit

Open JSSaini opened this issue 1 year ago • 8 comments

Hi, thank you for this amazing tool. I would like to put multiple categories/sample on the x-axis. However, only one is appearing for now. My purpose is to put geom_tile plot instead of bar but with multiple samples on x-axis. I would be grateful with your help.

Following code I used:

treen_A3_tpm + geom_fruit(geom=geom_bar,
           mapping=aes(y=label, fill=sample, x=tpm_log),
           pwidth=0.38, 
           orientation="y", 
           stat="identity")

image

JSSaini avatar Apr 19 '23 14:04 JSSaini

I want to layer multiple sample in series, something like this: image

JSSaini avatar Apr 19 '23 14:04 JSSaini

Anyone?

JSSaini avatar Apr 21 '23 07:04 JSSaini

My purpose is to put geom_tile plot instead of bar but with multiple samples on x-axis.

Please use geom_tile instead of geom_bar in geom_fruit. And you can map the abundance to the alpha or color character of geom_title.

You can refer to the following pseudocodes

treen_A3_tpm + 
   geom_fruit(geom=geom_tile,
           mapping=aes(y=label, fill=tmp_log, x=sample),
           pwidth=0.38
          )

xiangpin avatar Apr 21 '23 08:04 xiangpin

Thank you for your kind reply. I have different types of samples on x-axis which are missing. Please see the figure attached. I have also attached the dataset and the tree file. I would also like to show bootstrap values >80. I am grateful for your kind assitance.

treen_A<- read.tree("D:/R_files_lbe/metag/phylogenomic-tree-ribo-all-outgroup.txt")

treen_A1<- ggtree(treen_A, size=1)+
  geom_label2((aes(label=label)))

treen_A2_new<- ggtree(treen_A, layout='fan', open.angle=30)+
  geom_label2 (aes(label=label, 
                   subset = !is.na(as.numeric(label)) & as.numeric(label) > 80), color="black", size=4, hjust=1.25)

tree. treen_A3_tpm<- treen_A2_new %<+% tpm_data_old_MAG


treen_A3_tpm + 
    geom_fruit(geom=geom_tile,
                               mapping=aes(y=label, fill=tpm, x=sample),
                               pwidth=0.38) 

image phylogenomic-ribosomal-tree_20_04.txt tpm_data_old_MAG.txt

JSSaini avatar Apr 21 '23 09:04 JSSaini

The x axis should show different sample types layered in form of tiles.

JSSaini avatar Apr 21 '23 09:04 JSSaini

You can following the grammer of ggplot2 to visualize it, refering to the following codes.

library(ggtree)                                                                                                                                                                                         library(ggplot2)
library(ggtreeExtra)
library(treeio)
library(forcats)

tr <- read.newick('./phylogenomic-ribosomal-tree_20_04.txt', node.label = 'support')

da <- read.table('./tpm_data_old_MAG.txt', sep='\t', header=T)

p1 <- ggplot() +
    geom_tile(
      data = da %>% dplyr::filter(tpm > 0),
      mapping= aes(x = fct_reorder(sample, Inf., .fun = min), y=MAG_ID, fill = tpm)
    ) +
    scale_fill_viridis_c(option = 'H')

p2 <- ggplot() +
      geom_tile(
        data = da %>% dplyr::filter(tpm > 0),
        mapping = aes(x =  fct_reorder(sample, Inf., .fun = min), y=MAG_ID, fill = Inf., alpha = tpm)
      ) +
      scale_fill_manual(values = c('orange', 'deepskyblue', 'red')) +
      scale_alpha_continuous(range = c(0.1, 1), breaks = da %>% dplyr::pull(tpm) %>% pretty(n=8))

p1 + p2

捕获

The p1 and p2 is built with ggplot2, the x axis reprents the sample, and the y axis reprents the different MAG which not be clusted.

f1 <- ggtree(tr, layout = 'circular') +
      geom_fruit(
        geom = geom_tile,
        data = da %>% dplyr::filter(tpm > 0),
        mapping = aes(x = fct_reorder(sample, Inf., .fun = min), y=MAG_ID, fill = tpm),
        pwidth = .4
      ) +
      scale_fill_viridis_c(option = 'H')
f1

捕获2


f2 <- ggtree(tr, layout = 'circular') +
      geom_fruit(
        geom = geom_tile,
        data = da %>% dplyr::filter(tpm > 0),
        mapping = aes(x = fct_reorder(sample, Inf., .fun = min), y=MAG_ID, fill = Inf., alpha = tpm),
        pwidth = .4
      ) +
      scale_fill_manual(values = c('orange', 'deepskyblue', 'red')) +
      scale_alpha_continuous(range = c(0.1, 1), breaks = da %>% dplyr::pull(tpm) %>% pretty(n=8))

f2                                                                                          

捕获3

The f1 and f2 was built with ggtree, you can found the diffrences bewtween them (p1 and f1, p2 and f2), the f1 and f2 just used the geom_tile in geom_fruit, p1 and p2 used the geom_tile directly, but the data and mapping argument are the same.

Moreover, you also can replace geom_tile using geom_point to visualize the result.

xiangpin avatar Apr 23 '23 04:04 xiangpin

That's exactly what I was looking for. Thanks a lot for our kind help. This will go a long way!

JSSaini avatar Apr 23 '23 07:04 JSSaini

Dear Yu,

I have used the code for the circular tree with additional data annotation (Fig. 10.2, https://yulab-smu.top/treedata-book/chapter10.html) but I am having issues with the overlap of the inner heatmap rings and the outer bartplots. See figure attached.

I have played with the different parameters in the code (also attached), but so far I have not been successful to either (1) reduce the size of the inner heatmap rings (I have only two categories) or (2) move the barplots further out, so they do not overlap.

I am wondering if you could give me some guidance with this issue.

Thanks in advance!

`tree_nema_18S <- read.tree("data/wofa-asv-nematodes-rep-sequences.tree") dat1_nema <- read.csv("exported_tables/combined/wofa_nematode_ludox_tippoint_attr.csv") dat2_nema <- read.csv("exported_tables/combined/wofa_nematode_ludox_ringheatmap_attr.csv") dat3_nema <- read.csv("exported_tables/combined/wofa_nematode_ludox_barplot_attr.csv")

dat2_nema$Group <- factor(dat2_nema$Group, levels=c("Gp1", "Gp2"))

dat3_nema$Group <- factor(dat3_nema$Group, levels=c("Gp1", "Gp2"))

p_nematoda <- ggtree(tree_nema_18S, layout="fan", size=0.15, open.angle=5) p_nematoda

p_nematoda <- p_nematoda %<+% dat1_nema + geom_fruit(geom=geom_star, mapping=aes(fill=Family, size=Size), position="identity",starstroke=0.1)+ scale_fill_manual(values=c("#FFC125","#87CEFA","#7B68EE","#808080","#EE6A50", "#9ACD32","#D15FEE","#FFC0CB","#800080","#8DEEEE", "#006400","#800000","#f58231","#191970","#9a6324", "#46f0f0","#1F78B4","#bcf60c","#e6beff","#4363d8", "#008080","#e6194b","#f032e6","#aaffc3", "lightgreen", "#ffe119","#000075","#fffac8","#9a6324","#FB9A99", "#33A02C","#fabebe","#6A3D9A", "gray", "lightblue", "darkblue", "mediumspringgreen"), guide=guide_legend(keywidth = 0.5, keyheight = 0.5, order=1, override.aes=list(starshape=15)), na.translate=FALSE)+ scale_starshape_manual(values=c(15, 1, 3, 5, 7, 2, 10, 9), guide=guide_legend(keywidth = 0.5, keyheight = 0.5, order=2), na.translate=FALSE)+ scale_size_continuous(range = c(1, 2.5), guide = guide_legend(keywidth = 0.5, keyheight = 0.5, order=3, override.aes=list(starshape=15)))+ new_scale_fill()+ geom_fruit(data=dat2_nema, geom=geom_tile, mapping=aes(y=ID, x=Group, alpha=Abundance, fill=Group), color = "grey50", offset = 0.04, size = 0.02)+ scale_alpha_continuous(range=c(0, 1), guide=guide_legend(keywidth = 0.3, keyheight = 0.3, order=5)) + geom_fruit(data=dat3_nema, geom=geom_bar, mapping=aes(y=ID, x=HigherAbundance, fill=Group), pwidth=0.38, orientation="y", stat="identity")+ scale_fill_manual(values=c("#000075", "#46f0f0"), guide=guide_legend(keywidth = 0.3, keyheight = 0.3, order=4))+ geom_treescale(fontsize=1.2, linesize=0.3, x=0.1, y=0.0001) + theme(legend.position=c(1.2, 0.5), legend.background=element_rect(fill=NA), legend.title=element_text(size=5), legend.text=element_text(size=4), legend.spacing.y = unit(0.02, "cm")) p_nematoda`

p_nematoda_ludox.pdf

tiagojp avatar Jan 23 '24 14:01 tiagojp