查看原文
其他

xlim_tree: set x axis limits for only Tree panel

2016-12-28 Y叔 biobabble

搬一篇博客文过来,明日再来解密这个小把戏,因为有人问了怎么给特定的分面加图层,这真是个好问题,敬请期待🙈


A ggtree user recently asked me the following question in google group:


I try to plot long tip labels in ggtree and usually adjust them using xlim(), however when creating a facet_plot xlim affects all plots and minimizes them.

Is it possible to work around this and only affect the tree and it's tip labels leaving the other plots in facet_plot unaffected?



This is indeed a desire feature, as `ggplot2` can't automatically adjust `xlim` for text since the units are in two different spaces (data and pixel).


Here is an example, the tip labels are truncated.


    library(ggtree)
    set.seed(2016-10-31)
    tr =rtree(50)
    tr$tip.label = paste(tr$tip.label, tr$tip.label, sep="_")
    p <- ggtree(tr) + geom_tiplab(align=TRUE) + theme_tree2()
    d = data.frame(id = tr$tip.label, v= rnorm(50))

    facet_plot(p, geom=geom_point, data=d, mapping=aes(x=v), panel='dot') +
            ggtitle("truncated tip labels")



If we only visualize the tree, it is easy to solve by using `xlim()` to allocate more space for the lables. But `xlim()` works for all panels. Combination of `facet_plot()` and `xlim()` will produce figure with many spaces.


    facet_plot(p+xlim(NA, 6), geom=geom_point, data=d, mapping=aes(x=v), panel='dot') +
            ggtitle("xlim applies to all panels")



To overcome this issue, `ggtree` provides `xlim_tree` to set `x` axis limits for only the `Tree` panel.


  facet_plot(p, geom=geom_point, data=d, mapping=aes(x=v), panel='dot') + xlim_tree(6)



Actually, you can set `xlim` for selected panel by using `+xlim_expand(xlim, panel)`.



您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存