deeptime icon indicating copy to clipboard operation
deeptime copied to clipboard

Questions about deeptime one tree (tiplabel & grey/white colors)

Open StromTroopers opened this issue 2 years ago • 4 comments

Hello, first of all thank you very much for the development of this package, it will be useful for many people.

I just have some questions about the tutorial:

data(mammal.tree)
p <- ggtree(mammal.tree) +
  coord_geo(xlim = c(-75,0), ylim = c(-2,Ntip(mammal.tree)), neg = TRUE, abbrv = FALSE) +
  scale_x_continuous(breaks=seq(-80,0,20), labels=abs(seq(-80,0,20))) +
  theme_tree2()
revts(p)

With that tutorial we are able to draw this figure:

Capture d’écran 2021-09-14 à 09 33 12

Here I my 2 questions:

  • Is it possible to add tiplabel ? I tried with the ggtree function geom_tiplab() but It does not work.
  • Is it possible as in the following figure: Capture d’écran 2021-09-14 à 09 35 55

to add between each geological period a grey and blank colors ?

Thanks a lot and have a nice day.

StromTroopers avatar Sep 14 '21 07:09 StromTroopers

Thanks for reaching out! You can achieve both of these requests with something like the following:

periods_cust <- periods # get the periods object from deeptime
periods_cust$box_fill <- c("grey", "white") # add alternating colors to the data frame

data(mammal.tree)
p <- ggtree(mammal.tree) +
    geom_tiplab() + # add tip labels
    geom_rect(data = periods_cust, aes(xmin = -min_age, xmax = -max_age, ymin = -Inf, ymax = Inf),
              fill = periods_cust$box_fill, inherit.aes = FALSE) + # add rectangles
    coord_geo(xlim = c(-75,15), ylim = c(0,Ntip(mammal.tree) + 1),
              neg = TRUE, abbrv = FALSE) + # change limits to show tip labels
    scale_x_continuous(breaks=seq(-80,0,20), labels=abs(seq(-80,0,20))) +
    theme_tree2() +
    theme(axis.line.x = element_blank()) # hide the x axis so it doesn't show up under the tip labels
p$layers <- rev(p$layers) # put the rectangles behind the tree
revts(p)

image

willgearty avatar Sep 14 '21 15:09 willgearty

Thanks a lot for the quick answer, Now I found another question in order to had facet to the plot as explained in this tutorial :

https://yulab-smu.top/treedata-book/chapter12.html

Here is a reproductible example of the issue :


#Load the data 
data(mammal.tree)

#Set so values to ad as bars 
value <- runif(49, 0,1)
df <- data.frame(mammal.tree$tip.label, value)

#Create the ggplot tree figure 
periods_cust <- periods # get the periods object from deeptime
periods_cust$box_fill <- c("grey", "white") # add alternating colors to the data frame

p <- ggtree(mammal.tree) +
  geom_tiplab() + # add tip labels
  geom_rect(data = periods_cust, aes(xmin = -min_age, xmax = -max_age, ymin = -Inf, ymax = Inf),
            fill = periods_cust$box_fill, inherit.aes = FALSE) + # add rectangles
  coord_geo(xlim = c(-75,15), ylim = c(0,Ntip(mammal.tree) + 1),
            neg = TRUE, abbrv = FALSE) + # change limits to show tip labels
  scale_x_continuous(breaks=seq(-80,0,20), labels=abs(seq(-80,0,20))) +
  theme_tree2() +
  theme(axis.line.x = element_blank()) # hide the x axis so it doesn't show up under the tip labels
p$layers <- rev(p$layers) # put the rectangles behind the tree
p<-revts(p)

#Add the geombarh facet 
p2 <- facet_plot(p + xlim_tree(8), panel = 'bar', data = df, geom = geom_barh, 
                 mapping = aes(x = value, fill ="black"), 
                 width = 0.8, stat='identity') + xlim_tree(9)

But when I do the last part I get the following error message :

Error : Aesthetics must be either length 1 or the same as the data (44): fill
Run `rlang::last_error()` to see where the error occurred.

Do you have an idea of what is going on ?

The issue seems to come from the code in the deeptime package since when I only run p<- ggtree(mammal.tree) the facet id added in the next part...

StromTroopers avatar Sep 15 '21 07:09 StromTroopers

Ah, since you are using a facetting approach, we would need to specify the fill values in a different way:

p <- ggtree(mammal.tree) +
    geom_tiplab() + # add tip labels
    geom_rect(data = periods_cust, aes(xmin = -min_age, xmax = -max_age, ymin = -Inf, ymax = Inf,
              fill = box_fill), inherit.aes = FALSE, show.legend = FALSE) +
    coord_geo(xlim = c(-75,15), ylim = c(0,Ntip(mammal.tree) + 1),
              neg = TRUE, abbrv = FALSE) +
    scale_x_continuous(breaks=seq(-80,0,20), labels=abs(seq(-80,0,20))) +
    scale_fill_manual(values = c("grey" = "grey", "white" = "white")) +
    theme_tree2() +
    theme(axis.line.x = element_blank())
p$layers <- rev(p$layers)
p<-revts(p)

However, it doesn't look like facet_plot plays very well with coord_geo(): image

Maybe @GuangchuangYu knows if it's possible to have different x axis limits with facet_plot? (like the scales argument for facet_wrap())

willgearty avatar Sep 15 '21 13:09 willgearty

don't know the details of the coord_geo, I think you can try the aplot package for this purpose.

GuangchuangYu avatar Sep 16 '21 05:09 GuangchuangYu

I'm going to close this issue, but please feel free to open a new issue if you run into any other problems @StromTroopers.

willgearty avatar Oct 26 '22 16:10 willgearty