2013-07-22 47 views
4

我正试图将一个系统发育树排列到显示一组相关生物体的生理数据的图上。像下面的图片。这是从2个独立的图形放在一起。我想它可以完成这项工作,但我希望创建一个单一的图像,我认为这将更容易格式化为一个文档。我能够使用ggplot2生成我想要的图形,并使用ape导入树形图。我认为应该有一种方法将树保存为图形对象,然后使用gridExtra中的gridarrange函数将其与图形进行排列。问题是,猿不会让我的树保存为图形对象,例如,将系统发育树与x,y图结合

p2<-plot(tree, dir = "u", show.tip.label = FALSE) 

只绘出了树,当你调用P2它只是给出的参数列表。我想知道是否有人有任何提示。

谢谢!

enter image description here

+0

长相[与此类似(http://stackoverflow.com/questions/17370853/align-ggplot2-plots-vertically/17371177#17371177) – baptiste

+0

为了澄清:您的顶部图像来自ggplot,而系统发育树是使用基本绘图系统绘制的。所以你想在同一个图像上结合ggplot和base plot。我认为如果有的话,答案会比它的价值更麻烦。 –

+0

顶部图像来自猿猴的ggplot2树。如果有一种将树导入ggplot2的方式,那很好,我只是没有意识到它。 – user2055130

回答

3

我不知道这是否会与gtable工作从CRAN

require(ggplot2) 
require(gridBase) 
require(gtable) 

p <- qplot(1,1) 
g <- ggplotGrob(p) 

g <- gtable_add_rows(g, unit(2,"in"), nrow(g)) 
g <- gtable_add_grob(g, rectGrob(), 
        t = 7, l=4, b=7, r=4) 

grid.newpage() 
grid.draw(g) 

#grid.force() 
#grid.ls(grobs=F, viewports=T) 
seekViewport("layout.7-4-7-4") 
par(plt=gridPLT(), new=TRUE) 
plot(rtree(10), "c", FALSE, direction = "u") 
upViewport() 

enter image description here

+0

这工作得很好,但你知道如何去除底部桌子周围的黑色边框吗? – user2055130

+0

不确定,但您可以随时将rectGrob设置为使用透明颜色 – baptiste

+0

我试过“g < - gtable_add_grob(g,rectGrob(gp = gpar(fill =”white“), t = 7,l = 4,b = 7,r = 4))“ 但出现错误。对不起,我对gtable – user2055130

1

首先我想对于解决大多数他所有的多个答案感谢baptiste我的问题与ggplot2。

秒,我有一个类似的问题,其中包括一个一棵树与ggplot2获得的热图。巴蒂斯特做了我的一天,虽然我的简化版本可以提供帮助。 我只使用了对我有用的东西(删除了gg_rows的添加)。

library(ape) 
tr <- read.tree("mytree.tree") 
# heat is the heatmap ggplot, using geom_tile 
g <- ggplotGrob(heat) 
grid.newpage() 
grid.draw(g) 
# use oma to reduce the tree so it fits 
par(new = TRUE, oma = c(5, 4, 5, 38)) 
plot(tr) 
nodelabels(tr$node.label, cex = 1, frame = "none", col = "black", adj = c(-0.3, 0.5)) 
add.scale.bar() 
# use dev.copy2pdf and not ggsave 
dev.copy2pdf(file = "heatmap_prob.pdf") 

结果是这里 heatmaptree