customizing x axis

849 views
Skip to first unread message

Agnieszka Sitko

unread,
Mar 1, 2017, 3:30:41 AM3/1/17
to ggtree
Hi, All, 

is there any pleasant way to customize x axis in ggtree object passed through gheatmap? 

Below is what I am trying to achieve done with grid.arranging two separate ggplot objects (ggtree and heatmap).




I have defined my own formatter:

rootShifter <- function(x, shift) {
    lab <- round(x + as.numeric(shift), 2)
}

and used 
scale_x_continuous(label = function(x) { rootShifter(x, shift) })

on ggtree.










And here is my output while scaling gheatmap:























As you may see heatmaps labels were removed from the plot.

Please help me!

Agnieszka

Yu, Guangchuang

unread,
Mar 1, 2017, 3:51:40 AM3/1/17
to Agnieszka Sitko, ggtree

you need to provide a reproducible example, as I mentioned here, https://guangchuangyu.github.io/2016/07/how-to-bug-author/.

basically, gheatmap is not implemented by grid.arrange which have problem of aligning two ggplot objects like the first figure you showed here.


--
You received this message because you are subscribed to the Google Groups "ggtree" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bioc-ggtree+unsubscribe@googlegroups.com.
To post to this group, send email to bioc-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bioc-ggtree/dcc2bb56-d75e-4c0a-b130-65e8dbd257cb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
--~--~---------~--~----~------------~-------~--~----~
Guangchuang Yu, PhD Candidate
State Key Laboratory of Emerging Infectious Diseases
School of Public Health
The University of Hong Kong
Hong Kong SAR, China
-~----------~----~----~----~------~----~------~--~---

Agnieszka Sitko

unread,
Mar 1, 2017, 12:04:31 PM3/1/17
to ggtree
Hi, Guangchuang Yu,

below you may find my code. Any tips are welcomed.

library(ggplot2)
library(ggtree)
library(dplyr)

# data preparation
txt <- "(C: 2, (A: 1, B:1): 1);"
df <- matrix(rnorm(9), nrow = 3) %>% as.data.frame()
rownames(df) <- c("A", "B", "C")

# an original tree with gheatmap is rooted in zero
((ggtree(read.tree(text = txt)) + geom_tiplab(align = TRUE) +
    theme_tree2()) %>%
    gheatmap(df, offset = 0, width = 0.5, colnames = FALSE)) %>%
    scale_x_ggtree()

Plot without scaling has labels under heatmap but it is also rooted in zero. Let's start shifting. I cannot use both `scale_x_ggtree` and `scale_x_continuous`. 
So now labels under the heatmap vanish.
rootShifter <- function(x, shift) {
    lab <- round(x + as.numeric(shift), 2)
}

shift <- 2
(ggtree(read.tree(text = txt)) + geom_tiplab(align = TRUE) +
      theme_tree2()) %>%
        gheatmap(df, offset = 0, width = 0.5, colnames = FALSE) +

Agnieszka Sitko

unread,
Mar 1, 2017, 12:18:20 PM3/1/17
to ggtree
And I have just came up with the solution. I am not sure if it's the most gentle one, but I attach it below.

library(ape)
shift <- 2
breaksAndLabelsCalc <- function(tr, shift, gridLength) {
    trHeight <- max(node.depth.edgelength(tr))
    br <- seq(0, trHeight, trHeight / gridLength)
    return(
        list(
            breaks = br,
            labels = as.character(round(br + shift), 2)
        )
    )
}

tree <- read.tree(text = txt)
brLab <- breaksAndLabelsCalc(tree, shift, 6)

((ggtree(tree) + geom_tiplab(align = TRUE) +
    theme_tree2()) %>%
    gheatmap(df, offset = 0, width = 0.5, colnames = FALSE)) %>%
    scale_x_ggtree(breaks = brLab$breaks, labels = brLab$labels)



Yu, Guangchuang

unread,
Mar 1, 2017, 10:42:26 PM3/1/17
to Agnieszka Sitko, ggtree
p = ggtree(read.tree(text = txt)) + geom_tiplab(align = TRUE) + theme_tree2()
p$data$x = p$data$x +2
p %>%
    gheatmap(df, offset = 0, width = 0.5, colnames = FALSE) %>%
    scale_x_ggtree()

--
You received this message because you are subscribed to the Google Groups "ggtree" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bioc-ggtree+unsubscribe@googlegroups.com.
To post to this group, send email to bioc-...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Agnieszka Sitko

unread,
Mar 2, 2017, 3:41:48 AM3/2/17
to ggtree
Oh, thank you. That is so much easier!
To unsubscribe from this group and stop receiving emails from it, send an email to bioc-ggtree...@googlegroups.com.

To post to this group, send email to bioc-...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages