Re: Axis in the geom_fruit and its orientation in dendrogram

878 views
Skip to first unread message
Message has been deleted

xshua...@163.com

unread,
Sep 2, 2020, 5:57:04 AM9/2/20
to ggtree

I think you can use ggtree and ggtreeExtra to plot the tree and barplot, then other layers can be visualized by ggplot2, and all layers can be assembled using aplot or patchwork. If you use geom_fruit of ggtreeExtra to add external boxplot on dendrogram tree, you can set the offset (control the distance between the layers). I hope the following codes can help it. (Note: update the ggtreeExtra to newest version github)

library(ggtree)                                                                                                                                                                                                    
library(phyloseq)
library(dplyr)
library(ggplot2)
library(ggtreeExtra)

data("GlobalPatterns")
GP <- GlobalPatterns
GP <- prune_taxa(taxa_sums(GP) > 600, GP)
sample_data(GP)$human <- get_variable(GP, "SampleType") %in%
  c("Feces", "Skin")

mergedGP <- merge_samples(GP, "SampleType")
mergedGP <- rarefy_even_depth(mergedGP,rngseed=394582)
mergedGP <- tax_glom(mergedGP,"Order")

melt_simple <- psmelt(mergedGP) %>%
  filter(Abundance < 120) %>%
  select(OTU, val=Abundance)

head(melt_simple)

p <- ggtree(mergedGP) + layout_dendrogram() +
     geom_tippoint(aes(color=Phylum),
                   show.legend=FALSE,
                   size=1.5)

p1 <- p +
      geom_fruit(
         data=melt_simple,
         geom=geom_boxplot,
         mapping = aes(
                     y=OTU,
                     x=val,
                     fill=Phylum,
                   ),
         lwd=.2,
         outlier.size=0.2,
         outlier.stroke=0.05,
         outlier.shape=21,
         offset=-1.01,
         axis.params=list(add.axis=TRUE,
                          hjust=1,
                          text.angle=0),
         grid.params=list(add.grid=TRUE,
                          add.vline=FALSE,
                          size=0.2)
     ) +
     scale_fill_discrete(
         name="Phyla",
         guide=guide_legend(keywidth=0.5, keyheight=0.5, ncol=1),
     ) +
     theme(
         legend.title=element_text(size=9),
         legend.text=element_text(size=6),
     )

p1
在2020年9月2日星期三 UTC+8 下午2:23:09<fari...@gmail.com> 写道:
Hi guys,

Does anyone know how one can produce such this plot? Is it even possible with ggtree? Especially I'm my focus is the bottom one where there are y and x-axis present in the plot. In addition, the orientation is from bottom to top. I personally try to do it with geom_fruit but the issue is 1) I cannot add y and x-axis 2) the orientation of the boxplot is from top to bottom. Is there any suggestion or help? Here is my code:

p <- ggtree(tree) + layout_dendrogram()
p <- p + geom_tiplab(align=TRUE, linesize=.1, angle=-90, hjust=-0.1, size=3)
p <- p + xlim_tree(10)
p <- p + geom_fruit(data=info1,
                    geom=geom_boxplot,
                    width=0.5,
                    mapping=aes(y=clone, x=survival, color=group, fill=group),
                    offset=-2.5, outlier.alpha=0.1,
                    axis.params=list(add.axis=TRUE, hjust=0, text.size=1, nbreak=4, line.size=0.2)) +
    scale_color_manual(values=c("#D92347","#FFEF12","#009949","#812C7C","#0067A5"),
                       guide=guide_legend(keywidth=0.5, keyheight=0.5, order=6)) +
    scale_fill_manual(values=c("#D92347","#FFEF12","#009949","#812C7C","#0067A5"),
                      guide=guide_legend(keywidth=0.5, keyheight=0.5, order=6)) +
    theme(legend.position=c(0.98, 0.5),
          legend.title=element_text(size=6),
          legend.text=element_text(size=6),
          legend.spacing.y = unit(0.02, "cm"))

Thank you,

Farid
Message has been deleted

xshua...@163.com

unread,
Sep 2, 2020, 10:22:02 PM9/2/20
to ggtree
You can add `grid.params=list(add.grid=TRUE)` to add  x-axis of the bottom plot. The orientation of the plot  has not been reversed, I will update it in next version.

在2020年9月2日星期三 UTC+8 下午11:52:43<fari...@gmail.com> 写道:
Many thanks for your help and response. In fact the issues I have are as follow:

1) The orientation of the plot is from top to bottom but I want to be from bottom to top. I tried scale_x_reverse() but it's not working. Do you have any idea?
2) I don't know how I can add x-axis to the bottom plot?

Here's the code I used and the plot is attached:

p <- ggtree(tree) + layout_dendrogram()
p <- p + geom_tiplab(align=TRUE, linesize=.1, angle=-90, hjust=-0.1, size=3)
p <- p + geom_fruit(
                    data=info1,
                    geom=geom_boxplot,
                    width=0.5,
                    pwidth=1,
                    mapping=aes(y=clone, x=survival, color=group, fill=group),
                    offset=-1.5,
                    size=0.3,
                    outlier.size=0.5,
                    outlier.stroke=0.2,
                    outlier.alpha=0.1,
                    axis.params=list(add.axis=TRUE, hjust=1, text.size=2, nbreak=4, line.size=0.2, line.color='black'),
        ) +
        scale_color_manual(values=c("#D92347","#FFEF12","#009949","#812C7C","#0067A5"),
                           guide=guide_legend(keywidth=0.5, keyheight=0.5, order=6)) +
        scale_fill_manual(values=c("#D92347","#FFEF12","#009949","#812C7C","#0067A5"),
                          guide=guide_legend(keywidth=0.5, keyheight=0.5, order=6)) +
        theme(legend.position=c(0.98, 0.5),
              legend.title=element_text(size=6),
              legend.text=element_text(size=6),
              legend.spacing.y = unit(0.02, "cm"))

Best,
Farid

Message has been deleted

xshua...@163.com

unread,
Sep 3, 2020, 9:50:52 PM9/3/20
to ggtree

The v0.99.15 version in github has support it. You can use axis.params=list(add.axis=TRUE, add.another.axis=TRUE) to display it. The orientation of the plot is from bottom to top in dendrogram layout. You can refer the following codes.

library(ggtree)
library(ggplot2)
library(ggtreeExtra)

set.seed(1024)
tr <- rtree(10)

df <- data.frame(id=tr$tip.label, group=rep(c("A", "B"),5))                                                                                                                                                        
dat <- data.frame(id=rep(tr$tip.label, 8), value=abs(rnorm(80, 30, 10)))

p1 <- ggtree(tr, layout="dendrogram") %<+% df +
      geom_tiplab(
          align=TRUE,
          linesize=.1,
          angle=-90,
          hjust=-0.1,
          size=3
      )

p4 <- p1 +
      geom_fruit(
          data=dat,
          geom=geom_boxplot,
          mapping=aes(x=value, y=id, fill=group),
          offset=-2.1,  # control the distance between layers. 
          pwidth=0.8, # control width of the layer.
          axis.params=list(add.axis=TRUE, 
                               add.another.axis=TRUE,
                               text.size =2,
                               hjust=1
                               ),
          grid.params=list(add.grid=FALSE)
          )

More documents can refer the vignettes and chaper10 of the online book.

PS: For dendrogram, rectangular, slanted layout, you can also use geom_facet in ggtree or aplot to visualize it. The upper exmaple also can be produced using aplot.

library(ggtree)                                                                                                                                                                                                    
library(aplot)
library(ggplot2)

set.seed(1024)
tr <- rtree(10)

df <- data.frame(id=tr$tip.label, group=rep(c("A", "B"),5))
dat <- data.frame(id=rep(tr$tip.label, 8), value=abs(rnorm(80, 30, 10)))
dt <- merge(dat, df, by.x="id", by.y="id")

### The tree layer

p1 <- ggtree(tr, layout="dendrogram") %<+% df +
      geom_tiplab(
          align=TRUE,
          linesize=.1,
          angle=-90,
          hjust=-0.1,
          size=3
      )

### using aplot to plot it

dd <- p1$data[match(dt$id, p1$data$label),]
dt$id_index <- dd$y

p2 <- ggplot(data=dt) +
      geom_boxplot(aes(x=id_index, group=id_index, y=value, fill=group))

p3 <- p1 %>% insert_bottom(p2, height=0.8)
在2020年9月3日星期四 UTC+8 上午10:44:32<fari...@gmail.com> 写道:
Thanks a lot for your help and your support. This option adds a grid to the bottom plot based on the breaks but isn't there an option to add the actual x-axis line?

Best,
Farid

ggtreeExtra_tree.png
aplot_tree.png
Message has been deleted
Message has been deleted

xshua...@163.com

unread,
Sep 7, 2020, 11:18:45 PM9/7/20
to ggtree
  1. The margin of plot in aplot will automatically be set to zero.
  2. I think you can use facet_grid to display the bar plot, so the distance between the barplot can be adjusted by panel.spacing or panel.spacing.y in theme of ggplot2.
    then you can use aplot to group the bar plot and tree plot.
    you can refer the following codes
library(aplot)                                                                                                                                                                                                     
library(ggtree)
library(ggplot2)

set.seed(1024)

tr <- rtree(10)

df <- data.frame(trt = rep(tr$tip.label,2),
                 outcome = abs(rnorm(20, mean=20, sd=8)),
                 group=c(rep("A",10),rep("B",10)))

p1 <- ggtree(tr, layout="dendrogram") +
      geom_tiplab(
          align=TRUE,
          linesize=.1,
          angle=-90,
          hjust=-0.1,
          size=3
      )

dd <- p1$data[match(df$trt, p1$data$label),]
df$idx <- dd$y
df

p2 <- ggplot(data=df) +
      geom_bar(mapping=aes(y=idx, x=outcome, fill=group),
               stat="identity",
               orientation="y"
               ) +
      facet_grid(group~.) +
      coord_flip()

p3 <- p1 %>% insert_bottom(p2, height=0.8)
在2020年9月6日星期日 UTC+8 上午1:07:41<fari...@gmail.com> 写道:
Hi,

I faced some questions with respect to your suggestion of using aplot api. Do you know how I can fix the following (I have mentioned them in red color in the attached file):

1) How I can reduce the expansion of the y-asix? It seems it's automatically expanded.
2) How I can increase the distance between each plot that I'm adding by aplot?

What I'm doing is using the following code to add each geom_bar to the bottom of the tree:

p <- p %>% insert_bottom(p2, height=1)

Thank you,

Farid
On Friday, September 4, 2020 at 12:38:38 PM UTC-4 Farid Rashidi wrote:
Wow, how fast! Thanks a lot for your support. It's working like a charm.

Best,
Farid

facet_bar_aplot.png
Reply all
Reply to author
Forward
0 new messages