facet_plot 在关联数据上,可谓非常方便,当然有个槽点,就是它利用了facet_grid ,从而也就没办法设置不同panel之间的宽度比例了,所以你画的图,所有的panel都是一样大。这篇文章就来突破一下这个限制。
library(ggplot2) library(ggstance) library(ggtree) library(reshape2)
set.seed(123) tree <- rtree(30)
p <- ggtree(tree, branch.length = 'none') + geom_tiplab()
a <- runif(30, 0,1) b <- 1 - a df <- data.frame(tree$tip.label, a, b) df <- melt(df, id = 'tree.tip.label')
p2 <- facet_plot(p + xlim_tree(8), panel = 'bar', data = df, geom = geom_barh, mapping = aes(x = value, fill = as.factor(variable)), width = 0.8, stat='identity')
假设我们画了p2 这个图,在ggtree 新版中,我又加入了一个函数facet_widths 用于设置不同panel的相对宽度,比如: facet_widths(p2, widths = c(1, 2))
当然参数也允许用named vector,你可以指定某个panel,比如: facet_widths(p2, c(Tree = .5))
最主要的是它还能用于其它的ggplot 对象: p <- ggplot(iris, aes(Sepal.Width, Petal.Length)) + geom_point() + facet_grid(.~Species) facet_widths(p, c(setosa = .5))
当然这个由于不是ggplot2 所支持,得借助于grid 来实现,继而输出的就不是原来的ggplot 对象了,所以你最好在最后一步才用它。不像之前介绍的《facet_plot更改panel label》,则是在原来的ggplot 对象中稍做修改而来。 比如你要联用facet_widths 和facet_labeller 话,只能先facet_labeller 再facet_widths 而反之则不行。 facet_labeller(p2, c(Tree = 'phylogeny')) %>% facet_widths(c(Tree = .4))
|