2017-08-03 56 views
0

我的目标是通过列表作为函数geom_point2使用lapply或类似的mapply的参数。在类似的情况下,我必须成功通过列表(或列表)来geom_cladelabel为:将参数传递给geom_point2与地图

mapply(function (x,y,z,w,v,u,t,s) geom_cladelabel(node=x, label=y, 
align=F, etc. # Where x y z etc are lists. 

问题与内部geom_point2使用aes。 (不在geom_cladelabel中):

在geom_point2的情况下,节点信息在aes之内,我不能这样做。通常我不会收到任何错误消息,但它不起作用。

其目的是让这个例子工作,但使用了两次,而不是写geom_point2的应用程序。

# source("https://bioconductor.org/biocLite.R") 
# biocLite("ggtree") 
library(ggtree) 
library(ape) 
#standard code 
newstree<-rtree(10) 
node1<-getMRCA(newstree,c(1,2)) 
node2<-getMRCA(newstree,c(3,4)) 
ggtree(newstree)+ 
geom_point2(aes(subset=(node ==node1)), fill="black", size=3, shape=23)+ 
geom_point2(aes(subset=(node ==node2)), fill="green", size=3, shape=23) 

#desire to substitute the geom_point2 layers with mapply or lapply: 
#lapply(c(node1,node2), function (x) geom_point2(aes(subset=(node=x))))) 

enter image description here

+2

如果您提供样本输入数据的[可重现的示例](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example),则更容易为您提供帮助我们可以运行和测试代码。 – MrFlick

+0

getMRCA函数来自哪里? – MrFlick

+0

它是在包猿 – Ferroao

回答

2

这里是主叫geom_point2 USIG mapply溶液:

library(ggtree) 
ggtree(rtree(10)) + 
    mapply(function(x, y, z) 
    geom_point2(
     aes_string(subset=paste("node ==", x)), 
     fill=y, 
     size=10, 
     shape=z 
    ), 
    x=c(11,12), 
    y=c("green", "firebrick"), 
    z=c(23,24) 
) + 
    geom_text2(aes(subset=!isTip, label=node)) 

的解决方案是在aes_string(),其直接在美学写入的x值。默认aes()不会传递x的值,而只是字符串"x"。绘图时,ggtree然后查找称为“x”的节点,并以空节点列表结束。 我想这与变量被存储在mapply-环境中并没有被传递到图上有关。 PS:对不起,我之前用do.call()回答太快了。这是有用的,但在这里不重要。

+0

请尝试添加一些解释旁边的答案。这可能是一个很好的代码,提供了完美的解决方案,但没有任何解释,它缺乏任何支持证据,并使其他人难以了解正在发生的事情,甚至一般学习以供将来参考。 –

+1

感谢@PhillHealey指出了这一点。第一个答案是一个快速猜测。现在调整,希望这适合更好。 – RaphiS