R-如何使用参数列表过滤数据以生成多个数据框和图形 [英] R - how to filter data with a list of arguments to produce multiple data frames and graphs

查看:105
本文介绍了R-如何使用参数列表过滤数据以生成多个数据框和图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种使用过滤器参数列表来生成不同对象的方法.我有一个要为其制作多个图形的数​​据集.但是,我希望所有这些图都基于数据集的子集.为了说明目的,我提供了以下数据.

I am looking for a way to use a list of filter arguments to produce different objects. I have a data set for which I want to make several graphs. However, I want all these graphs based on subsets of the dataset. For illustrative purposes I have made the following data.

df <- data.frame(type = c("b1", "b2", "b1", "b2"),
                 yield = c("15", "10", "5", "0"),
                 temperature = c("2", "21", "26", "13"),
                 Season = c("Winter", "Summer", "Summer", "Autumn"),
                 profit = c(TRUE, TRUE, FALSE, FALSE))

此外,我还有一个过滤器参数列表.

Also, I have a list of filter arguments.

filters <- c("brand=='b1'",
             "profit",
             "Season=='Summer'",
             "profit==FALSE",
             "yield >= 10",
             "")

我想要的是我可以使用for循环让所有这些过滤器生成具有过滤数据的对象,然后绘制图形.我已经按照以下方式尝试过.

What I would want is that I could use a for loop to have all these filters produce objects with the filtered data, and subsequently plot graphs. I have tried this in the following way.

for(i in 1:length(filters)){
  assign(paste0("df", i), filter(df, factor(filters[i])))
  assign(paste0("plot", i), ggplot(database, aes(x = temperature, y = yield)) + geom_point())
}

但是,这没有用,因为 filter()函数不接受< fct> 作为参数,也不接受< chr> (例如"brand =='b1'" ).我想要的是 brand =='b1',所以 filter()接受它作为参数.有人有这样做的主意吗?

However, this did not work because the filter() function does not accept <fct> as an argument, nor <chr> (e.g., "brand=='b1'"). What I would want is brand=='b1', so filter() accepts it as an argument. Does anybody have an idea to do this?

此外,还有一个问题,我想使整个过程自动化,并以组合图结束,因此最后 grid.arrange().当然,我可以对 ncol nrow 进行自动化,而对 length(filters)进行一些设置.但是,如何在 grid.arrange()中获取所有生成的图?这可能应该在for循环之外,对吗?这里有什么想法吗?

Also, as an additional question, I would like to automate the whole process and end with an combined graph, so grid.arrange() at the end. Of course I could automate the ncol and nrow with some devision of length(filters). But how to I get all the produced plots in the grid.arrange()? This should probably be outside the for loop, right? Any ideas here?

推荐答案

您可以使用 eval parse 来实现.

此外,自定义函数上的 lapply 听起来比带有 assign for 循环更合理.结果是 ggplot 对象的列表.

Also, a lapply over a custom function sounds more reasonable than a for loop with assign. The result is a list of ggplot objects.

要同时设置所有图表,请使用 gridExtra 包中的 grid.arrange .您只需要将图表列表分配给名为 grobs 的参数即可.

To set all charts all together grid.arrange from the gridExtra package works fine. You just need to assign the list of your charts to the argument called grobs.

library(dplyr)
library(ggplot2)

df <- data.frame(type = c("b1", "b2", "b1", "b2"),
                 yield = c(15, 10, 5, 0),
                 temperature = c("2", "21", "26", "13"),
                 Season = c("Winter", "Summer", "Summer", "Autumn"),
                 profit = c(TRUE, TRUE, FALSE, FALSE))

filters <- list("type=='b1'",
                "profit",
                "Season=='Summer'",
                "profit==FALSE",
                "yield >= 10",
                "TRUE")


myfun <- function(fltr, df){

  df <- filter(df, eval(parse(text = fltr)))
  ggplot(df, aes(x = temperature, y = yield)) + geom_point()

}


ggs <- lapply(filters, myfun, df = df)

gridExtra::grid.arrange(grobs = ggs)

我对您的数据进行了几处更改:yield必须为数字,因为您使用的过滤器仅适用于数字矢量,并且最后一个过滤器(为空)现在等于"TRUE"(我想您想要考虑一切]

I made a couple of changes in your data: yield must be a numeric since you are using a filter applicable only to numeric vectors and the last filter (which was empty) is now equal to "TRUE" [I supposed you wanted to take everything in consideration]

这篇关于R-如何使用参数列表过滤数据以生成多个数据框和图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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