R:使用d*ply的多个ggplot2绘图

R:使用d*ply的多个ggplot2绘图,r,plot,ggplot2,dplyr,R,Plot,Ggplot2,Dplyr,我知道这个问题上的各种变化已经出现了好几次,但我不知道如何将这些解决方案应用于这一特定挑战: 我想在d*ply调用中使用ggplot来绘制被v3变量分解的数据(下面的数据框dat),并为v1中的3个条件显示一个数值变量v2。我希望将绘图放在一页(pdf)中,因此我认为可以使用dlply将生成的绘图包含在一个列表中,然后可以将该列表提供给在“Cookbook for R”中找到的ggplot2的multiplot包装函数 这里有一种不同的方法,可以避免使用multiplot(),并使用以下技术:

我知道这个问题上的各种变化已经出现了好几次,但我不知道如何将这些解决方案应用于这一特定挑战:

我想在d*ply调用中使用ggplot来绘制被
v3
变量分解的数据(下面的数据框
dat
),并为
v1
中的3个条件显示一个数值变量
v2
。我希望将绘图放在一页(pdf)中,因此我认为可以使用dlply将生成的绘图包含在一个列表中,然后可以将该列表提供给在“Cookbook for R”中找到的ggplot2的multiplot包装函数


这里有一种不同的方法,可以避免使用
multiplot()
,并使用以下技术:

库(ggplot2)
图书馆(dplyr)
结果%
分组依据(v3)%>%
do(plot=ggplot(,aes(v1,v2))+geom_点()
pdf('all.pdf')
不可见(lappy(结果$plot,print))
发展主任()

Edit:p应该在multiplot中为plotlist参数指定-但它仍然不起作用multiplot解决方案有什么问题?我不知道你所说的“分开绘制但不合并”是什么意思。当我运行您的代码时,没有控制台输出。你应该为
dlply
加载
plyr
,而不是
dplyr
@Gregor:d)plyr的速度太快了;它们总是被加载(所以它工作了),当然它需要plyr。3个图显示在图形设备(RStudio)中,但分开(一个接一个)。然后在阅读了您的评论之后,我重新启动了Rstudio,并从multiplot()中获得了源代码,而且它不知从哪里起作用。我不知道那是什么。很高兴听到你的评论。很高兴你能成功!我将投票结束你的问题,因为它不再是一个可重复的问题。另一个评论:如果你不想保留
源代码
,那么
multiplot
功能在几个包中提供,例如,
Rmisc
喷灯
。我喜欢
wq::layOut
,因为它更灵活。是的,一直以来都是如此。但我希望把所有(三)个情节都放在一个页面上,而不是放在几个(3)页面上的一个子地块公关页面上。你能对那些看不见的东西发表评论吗(尽管它清楚地暗示你在里面打印)
# Multiple plot function
#
# ggplot objects can be passed in ..., or to plotlist (as a list of ggplot objects)
# - cols:   Number of columns in layout
# - layout: A matrix specifying the layout. If present, 'cols' is ignored.
#
# If the layout is something like matrix(c(1,2,3,3), nrow=2, byrow=TRUE),
# then plot 1 will go in the upper left, 2 will go in the upper right, and
# 3 will go all the way across the bottom.
#
multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
  require(grid)

  # Make a list from the ... arguments and plotlist
  plots <- c(list(...), plotlist)

  numPlots = length(plots)

  # If layout is NULL, then use 'cols' to determine layout
  if (is.null(layout)) {
    # Make the panel
    # ncol: Number of columns of plots
    # nrow: Number of rows needed, calculated from # of cols
    layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
                    ncol = cols, nrow = ceiling(numPlots/cols))
  }

 if (numPlots==1) {
    print(plots[[1]])

  } else {
    # Set up the page
    grid.newpage()
    pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))

    # Make each plot, in the correct location
    for (i in 1:numPlots) {
      # Get the i,j matrix positions of the regions that contain this subplot
      matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))

      print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
                                      layout.pos.col = matchidx$col))
    }
  }
}
   set.seed(999)
    dat <- data.frame(
      v1 = rep(c("A","B","C"),25),
      v2 = runif(75,-1,2),
      v3 = sample(c("hippo", "smoke", "meat"), 75, replace=T))
require(dplyr)
require(ggplot2)    
p <- dlply(dat, .(v3), function(x){
      ggplot(x,aes(v1, v2)) +
      geom_point()})

multiplot(plotlist=p, cols=2)
library(ggplot2)
library(dplyr)

results <- dat %>%
  group_by(v3) %>%
  do(plot = ggplot(., aes(v1, v2)) + geom_point())

pdf('all.pdf')
invisible(lapply(results$plot, print))
dev.off()