在ggplot2中使用for循环安排多个图 [英] arrange multiple graphs using a for loop in ggplot2

查看:181
本文介绍了在ggplot2中使用for循环安排多个图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 >头像(数据)
NetworkTrackingPixelId名称日期展示次数
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改进数字RTB 2014-02-16 701

我正在考虑使用类似于下面的脚本:

 #创建一个存储NetworkTrackingPixelIds的元素
tp < - data%。%
group_by(NetworkTrackingPixelId)%。%
select NetworkTrackingPixelId)

#创建一个for循环打印线图
for(i in tp){
print(ggplot(data [w (数据$ NetworkTrackingPixelId == i),],aes(x = Date,y = Impressions))+ geom_point()+ geom_line())
}
pre>

我期待这个命令产生许多图形,每个NetworkTrackingPixelId一个。另一个我注意到的事情是,变量 tp 不是真正的矢量。

 > is.vector(tp)
[1] FALSE

即使我强迫它

  tp < -  as.vector(data%。%
group_by(NetworkTrackingPixelId)%。%
选择(NetworkTrackingPixelId))
> is.vector(tp)
[1] FALSE
> str(tp)
类'grouped_df','tbl_df','tbl'和'data.frame':1397 obs。
$ NetworkTrackingPixelId:int 2421 2615 3366 3606 3947 4299 4429 4786 6046 6286 ...
- attr(*,vars)= 1
列表$:符号NetworkTrackingPixelId
- attr(*,drop)= logi TRUE
- attr(*,indices)= 63
.. $:int 24 69 116 162 205 253 302 351 402 454 ...
.. $:int 1 48 94 140 184 232 281 330 380 432 ...

[我剪掉了这个输出]

- attr(*,group_sizes)= int 29 29 2 16 29 1 29 29 29 29 ...
- attr(*,largest_group_size)= int 29
- attr(*,labels)='data.frame':63 obs。
$ NetworkTrackingPixelId int 8799 2615 8854 8869 4786 7007 3947 9109 9126 9137 ...
.. attr(*,vars)= 1
的列表.. $:symbol NetworkTrackingPixelId


解决方案

没有数据集,我将使用 mtcars 数据集来说明如何使用 dplyr data.table 。这两个包都是rstats中 split-apply-combine 范例的最好例子。让我解释一下:

第1步按照齿轮分割数据


  • dplyr 使用函数 group_by

  • data.table 使用参数 by



第二步:应用一个函数


  • dplyr 使用 do ,你可以传递一个函数来使用这个小块x。
  • data.table








    $ b $步骤3:合并

    $ p












    $ > 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
    ]


    $ b 更新:要将所有文件保存为一个pdf文件,这里有一个快速解决方案。 b $ b

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

    pdf('all.pdf')
    不可见(lapply(plots,print))
    dev.off )


    I want to produce a pdf which shows multiple graphs, one for each NetworkTrackingPixelId. I have a data frame similar to this:

    > 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
    

    I was thinking to use a script similar to the one below:

    # 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())
        }
    

    I was expecting this command to produce many graphs, one for each NetworkTrackingPixelId. Instead the result is an unique graph which aggregate all the NetworkTrackingPixelIds.

    Another thing I've noticed is that the variable tp is not a real vector.

    > is.vector(tp)
    [1] FALSE
    

    Even if I force it..

    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
    

    解决方案

    Since I don't have your dataset, I will use the mtcars dataset to illustrate how to do this using dplyr and data.table. Both packages are the finest examples of the split-apply-combine paradigm in rstats. Let me explain:

    Step 1 Split data by gear

    • dplyr uses the function group_by
    • data.table uses argument by

    Step 2: Apply a function

    • dplyr uses do to which you can pass a function that uses the pieces x.
    • data.table interprets the variables to the function in context of each piece.

    Step 3: Combine

    There is no combine step here, since we are saving the charts created to file.

    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
    ]
    

    UPDATE: To save all files into one pdf, here is a quick solution.

    plots = mtcars %.%
      group_by(gear) %.%
      do(function(x) {
        qplot(wt, mpg, data = x)
      })
    
    pdf('all.pdf')
    invisible(lapply(plots, print))
    dev.off()
    

    这篇关于在ggplot2中使用for循环安排多个图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆