使用 mapply 将参数传递给 geom_point2

passing arguments to geom_point2 with mapply

我的 objective 使用 lapply 或类似的 mapply 将列表作为参数传递给函数 geom_point2。在类似的情况下,我成功地将一个列表(或多个列表)传递给 geom_cladelabel,如:

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里面,我做不到。通常我不会收到任何错误消息,但它不起作用。

objective 是为了让这个例子工作,但是使用 mapply 而不是写两次 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)))))

这是调用 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-environment 而不是传递给绘图有关。

PS:抱歉我之前用 do.call() 回答得太快了。很有用,但是跑题了。