使用do.call为多个图表进行映射

时间:2015-06-03 10:53:12

标签: r graph ggplot2

我试图用mapply创建一些情节。我需要从列表coordList输入标记每个图形,所以我不得不从lapply切换。

plotList<-mapply(function(listPart,rangeName)
                    {

                            resultsul<-unlist(results)
                                gName<-names(results[1])

                                ggplot(listPart, aes(x = factor(Var2), y=value)) + geom_violin()+
                                ggtitle(gName)+xlab(rangeName)+ylab("Coverage")+
                                stat_summary(fun.y = median, geom = "point", position = position_dodge(width = .9),
                                size = 6, shape = 4, show_guide = F)
                    }, listPart=results,rangeName=coordList)
do.call(grid.arrange, c(plotList, ncol=colnum))

不幸的是,mapply似乎与do.call相处并不像lapply。我收到了错误

Error in arrangeGrob(..., as.table = as.table, clip = clip, main = main,  : 
input must be grobs! 

无论如何解决这个问题?感谢

编辑:

以下是结果的输入

structure(list(uc001aaa.3 = structure(list(Var1 = c(1L, 2L, 3L, 
1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), Var2 = c(1L, 1L, 
1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 
7L, 7L, 7L, 8L, 8L, 8L, 9L, 9L, 9L, 10L, 10L, 10L), value = c(878.186968838527, 
0, 1473.06397306397, 0, 0, 858.585858585859, 0, 0, 858.585858585859, 
0, 0, 286.195286195286, 0, 0, 732.323232323232, 0, 0, 1001.6835016835, 
0, 0, 538.720538720539, 0, 0, 3417.50841750842, 0, 0, 656.565656565657, 
0, 0, 622.895622895623)), .Names = c("Var1", "Var2", "value"), row.names = c(NA, 
-30L), class = "data.frame"), uc001aal.1 = structure(list(Var1 = c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), Var2 = 1:10, value =c(98.1461286804798, 
370.774263904035, 229.007633587786, 556.161395856052, 719.738276990185, 
468.920392584515, 970.556161395856, 828.789531079607, 577.971646673937, 
1461.28680479826)), .Names = c("Var1", "Var2", "value"), row.names = c(NA, 
-10L), class = "data.frame")), .Names = c("uc001aaa.3", "uc001aal.1"
))

这是coordList的输入

c("chr1 11874 14409", "chr1 69091 70008")

以下是在将其输入到mapply代码之前的结果(每个$是一个小的删节部分)

$uc001aaa.3
   Var1 Var2     value
1     1    1  878.1870
2     2    1    0.0000
3     3    1 1473.0640
4     1    2    0.0000



$uc001aal.1
   Var1 Var2      value
1     1    1   98.14613
2     1    2  370.77426
3     1    3  229.00763

这是我尝试使用循环打印出所有图的另一种策略,仍然给出了grobs错误。

plotList<-list()

for (i in 1:length(results))
                    {
resultsul<-unlist(results)
gName<-names(resultsul[i])



#trying to plot multiple plots on one figure
plotList[i]<-ggplot(results[[i]], aes(x = factor(Var2), y=value)) + geom_violin()+ggtitle("gName")+xlab(coordList[i])+ylab("Coverage")+
stat_summary(fun.y = median, geom = "point", position = position_dodge(width = .9), size = 6, shape = 4, show_guide = F)


}
do.call(grid.arrange, c(list(plotList), ncol=colnum))

0 个答案:

没有答案