使用ggplot2中的for循环排列多个图形

Gia*_*uca 5 for-loop r ggplot2

我想制作一个显示多个图形的pdf,每个图形一个NetworkTrackingPixelId.我有一个类似于这样的数据框:

> head(data)
  NetworkTrackingPixelId                           Name       Date Impressions
1                   2421                    Rubicon RTB 2014-02-16      168801
2                   2615                     Google RTB 2014-02-16     1215235
3                   3366                      OpenX RTB 2014-02-16      104419
4                   3606                   AppNexus RTB 2014-02-16      170757
5                   3947                   Pubmatic RTB 2014-02-16       68690
6                   4299            Improve Digital RTB 2014-02-16         701
Run Code Online (Sandbox Code Playgroud)

我想使用类似下面的脚本:

# create a vector which stores the NetworkTrackingPixelIds
tp <- data %.%
        group_by(NetworkTrackingPixelId) %.%
        select(NetworkTrackingPixelId)

# create a for loop to print the line graphs
for (i in tp) {
      print(ggplot(data[which(data$NetworkTrackingPixelId == i), ], aes(x = Date, y = Impressions)) + geom_point() + geom_line())
    }
Run Code Online (Sandbox Code Playgroud)

我期待这个命令产生许多图形,每个NetworkTrackingPixelId一个.相反,结果是一个汇总所有NetworkTrackingPixelIds的唯一图形.

我注意到的另一件事是变量tp不是真正的向量.

> is.vector(tp)
[1] FALSE
Run Code Online (Sandbox Code Playgroud)

即使我强迫它..

tp <- as.vector(data %.%
        group_by(NetworkTrackingPixelId) %.%
        select(NetworkTrackingPixelId))
> is.vector(tp)
[1] FALSE
> str(tp)
Classes ‘grouped_df’, ‘tbl_df’, ‘tbl’ and 'data.frame': 1397 obs. of  1 variable:
 $ NetworkTrackingPixelId: int  2421 2615 3366 3606 3947 4299 4429 4786 6046 6286 ...
 - attr(*, "vars")=List of 1
  ..$ : symbol NetworkTrackingPixelId
 - attr(*, "drop")= logi TRUE
 - attr(*, "indices")=List of 63
  ..$ : int  24 69 116 162 205 253 302 351 402 454 ...
  ..$ : int  1 48 94 140 184 232 281 330 380 432 ...

[I've cut a bit this output]

 - attr(*, "group_sizes")= int  29 29 2 16 29 1 29 29 29 29 ...
 - attr(*, "biggest_group_size")= int 29
 - attr(*, "labels")='data.frame':  63 obs. of  1 variable:
  ..$ NetworkTrackingPixelId: int  8799 2615 8854 8869 4786 7007 3947 9109 9126 9137 ...
  ..- attr(*, "vars")=List of 1
  .. ..$ : symbol NetworkTrackingPixelId
Run Code Online (Sandbox Code Playgroud)

Ram*_*ath 12

由于我没有您的数据集,我将使用mtcars数据集来说明如何使用dplyr和执行此操作data.table.这两个包都是split-apply-combinerstats中范例的最好例子.让我解释:

步骤1按齿轮分割数据

  • dplyr 使用该功能 group_by
  • data.table 使用参数 by

第2步:应用功能

  • dplyr用于do传递使用x片段的函数的用法.
  • data.table 在每个部分的上下文中将变量解释为函数.

第3步:结合

这里没有组合步骤,因为我们将创建的图表保存到文件中.

library(dplyr)
mtcars %.%
  group_by(gear) %.%
  do(function(x){ggsave(
    filename = sprintf("gear_%s.pdf", unique(x$gear)), qplot(wt, mpg, data = x)
  )})

library(data.table)
mtcars_dt = data.table(mtcars)
mtcars_dt[,ggsave(
  filename = sprintf("gear_%s.pdf", unique(gear)), qplot(wt, mpg)),
  by = gear
]
Run Code Online (Sandbox Code Playgroud)

更新:要将所有文件保存为一个pdf,这是一个快速的解决方案.

plots = mtcars %.%
  group_by(gear) %.%
  do(function(x) {
    qplot(wt, mpg, data = x)
  })

pdf('all.pdf')
invisible(lapply(plots, print))
dev.off()
Run Code Online (Sandbox Code Playgroud)